Golang 日期时间结构
Golang date time struct
我正在尝试构建 golang 日期时间结构,但遇到了一些问题。请帮我建立模型。
再次感谢
"effectiveDates" : {
"startDate" : {
"_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl",
"year" : NumberInt(2008),
"month" : NumberInt(10),
"day" : NumberInt(16),
"timezone" : NumberInt(-300),
"hour" : NumberInt(9),
"minute" : NumberInt(50),
"second" : NumberInt(13)
}
}
这应该足以让您继续前进。有几个怪癖:
- NumberInt(n) 值无效 JSON,因此我假设它们作为数字从 GET 调用中传入
- 需要将时区 NumberInt 转换为字符串,并修改为 -0700 形式。我把它留给你作为练习
这是我的解决方案:
package main
import (
"encoding/json"
"fmt"
"time"
)
type StartDate struct {
Class string `json:"_class"`
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Timezone string `json:"timezone"`
Hour int `json:"hour"`
Minute int `json:"minute"`
Second int `json:"second"`
}
type EffectiveDates struct {
StartDate `json:"startDate"`
}
type Payload struct {
EffectiveDates `json:"effectiveDates"`
}
var input = `{
"effectiveDates" : {
"startDate" : {
"_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl",
"year" : 2008,
"month" : 10,
"day" : 16,
"timezone" : "-0300",
"hour" : 9,
"minute" : 50,
"second" : 13
}
}
}`
func main() {
var p Payload
var t time.Time
err := json.Unmarshal([]byte(input[:]), &p)
if err != nil {
fmt.Println("error:", err)
}
timeStr := fmt.Sprintf("%d-%d-%d %d:%d:%d %s", p.Year, p.Month, p.Day, p.Hour, p.Minute, p.Second, p.Timezone)
if t, err = time.Parse("2006-01-02 3:04:05 -0700", timeStr); err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%s", t.Format(time.RFC3339))
}
我正在尝试构建 golang 日期时间结构,但遇到了一些问题。请帮我建立模型。
再次感谢
"effectiveDates" : {
"startDate" : {
"_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl",
"year" : NumberInt(2008),
"month" : NumberInt(10),
"day" : NumberInt(16),
"timezone" : NumberInt(-300),
"hour" : NumberInt(9),
"minute" : NumberInt(50),
"second" : NumberInt(13)
}
}
这应该足以让您继续前进。有几个怪癖:
- NumberInt(n) 值无效 JSON,因此我假设它们作为数字从 GET 调用中传入
- 需要将时区 NumberInt 转换为字符串,并修改为 -0700 形式。我把它留给你作为练习
这是我的解决方案:
package main
import (
"encoding/json"
"fmt"
"time"
)
type StartDate struct {
Class string `json:"_class"`
Year int `json:"year"`
Month int `json:"month"`
Day int `json:"day"`
Timezone string `json:"timezone"`
Hour int `json:"hour"`
Minute int `json:"minute"`
Second int `json:"second"`
}
type EffectiveDates struct {
StartDate `json:"startDate"`
}
type Payload struct {
EffectiveDates `json:"effectiveDates"`
}
var input = `{
"effectiveDates" : {
"startDate" : {
"_class" : "com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl",
"year" : 2008,
"month" : 10,
"day" : 16,
"timezone" : "-0300",
"hour" : 9,
"minute" : 50,
"second" : 13
}
}
}`
func main() {
var p Payload
var t time.Time
err := json.Unmarshal([]byte(input[:]), &p)
if err != nil {
fmt.Println("error:", err)
}
timeStr := fmt.Sprintf("%d-%d-%d %d:%d:%d %s", p.Year, p.Month, p.Day, p.Hour, p.Minute, p.Second, p.Timezone)
if t, err = time.Parse("2006-01-02 3:04:05 -0700", timeStr); err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%s", t.Format(time.RFC3339))
}