当我尝试将此json转换为时间时,出现错误
输出:
HERE ------------------------------------ 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC
原始:
{
"tenantID": "9e266e1a750e45f8862e83341a5d0970",
"startTime": "2019-4-1T11:45:26.371Z",
"endTime": "2019-4-10T11:45:26.371Z"
}
控制器:
var bodyBytes []byte
raw := make(map[string]interface{})
jsons := make(map[string]interface{})
if h.Ctx.Request.Body != nil {
bodyBytes, _ = ioutil.ReadAll(h.Ctx.Request.Body)
}
json.Unmarshal(bodyBytes, &raw)
time1, err := time.Parse(time.RFC3339,raw["startTime"].(string));
time2, err := time.Parse(time.RFC3339,raw["endTime"].(string));
fmt.Println("HERE------------------------------------",time1,time2)
usageSingle, err := compute.UsageSingleTenant(raw["tenantID"].(string), time1, time2)
答案 0 :(得分:5)
您正在使用RFC3339,它需要以下时间格式:
2019-04-01T11:45:26.371Z
不是
2019-4-1T11:45:26.371Z
当我在代码中添加零填充时,它可以正常工作。或者,如果我没有指定RFC时间,而是使用:
time1, err := time.Parse("2006-2-1T15:04:05.999Z",raw["startTime"].(string));