dongtiannai0654
2019-03-11 21:02
浏览 625
已采纳

将json格式的时间戳转换为golangs time.Time

I have a datetime string in json with the below format

 /Date(315513000000+0530)/ 

I would like to convert this into golangs time.Time format. I tried passing this string to the below function

func parseDateField(dateInput string) (int64, error) {
    startIdx := strings.Index(dateInput, "(")
    if startIdx == -1 {
        return 0, errors.New("Error parsing quote Date. '(' symbol not found")
    }

    endIdx := strings.Index(dateInput, "+")
    if endIdx == -1 {
    return 0, errors.New("Error parsing quote Date. '+' symbol not found")
     }
    dateStr := dateInput[startIdx+1 : endIdx]
    date, err := strconv.ParseInt(dateStr, 10, 64)
    if err != nil {
        fmt.Printf(" err : 
 %+v 
", err)
        return 0, err
     }
     tm := time.Unix(date, 0)
     fmt.Printf("
 time  : 
 %+v 
", tm)
     dateAsEpoch := int64(date / 1000)
     fmt.Printf("
 dateAsEpoch  : 
 %+v 
", dateAsEpoch)
     return dateAsEpoch, nil
}

I'm getting the below outputs

 time  : 
 11968-03-18 01:30:00 +0530 IST 


 dateAsEpoch  : 
 315513000 

I'm guessing the parsing isnt done right - what am i doing wrong?

图片转代码服务由CSDN问答提供 功能建议

我在json中使用以下格式的日期时间字符串

  / Date(315513000000 + 0530)/ 
   
 
 

我想将其转换为golangs time.Time 格式。 我尝试将此字符串传递给以下函数

  func parseDateField(dateInput string)(int64,error){
 startIdx:= strings.Index(dateInput,“(”)  
如果startIdx == -1 {
返回0,errors.New(“错误分析报价日期。'('符号未找到”“)
} 
 
 endIdx:= strings.Index(dateInput,” +  “)
如果endIdx == -1 {
返回0,errors.New(”错误分析报价日期。找不到'+'符号“)
} 
 dateStr:= dateInput [startIdx + 1:endIdx]  
 date,err:= strconv.ParseInt(dateStr,10,64)
如果err!= nil {
 fmt.Printf(“ err:
%+ v 
”,err)
返回0,  err 
} 
 tm:= time.Unix(date,0)
 fmt.Printf(“ 
 time:
%+ v 
”,tm)
 dateAsEpoch:= int64(date / 1000)  
 fmt.Printf(“ 
 dateAsEpoch:
%+ v 
”,dateAsEpoch)
返回dateAsEpoch,nil 
} 
   
 
 

I' m获得以下输出

 时间:
 11968-03-18 01:30:00 +0530 IST 
 
 
 dateAsEpoch:
 315513000 
 <  /代码>  
 
 

我猜测解析未正确完成-我在做什么错了?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzuan4860 2019-03-11 22:20
    已采纳

    That time format seems to be the elapsed milliseconds since epoch and a signed 4-digit zone offset (hour and min).

    Easiest would be to use fmt.Sscanf() to parse it, something like this:

    _, err = fmt.Sscanf(s, "/Date(%d+%02d%02d)/", &ms, &zoneHour, &zoneMin)
    

    Of course if the zone offset is negative, the format will not match. That can be handled by a second attempt of parsing, + replaced by a - in the format string.

    Once you have the ms, multiply it by a million to get nanoseconds, so you can pass that to time.Unix() (it is valid to pass a nanoseconds value outside of the [0, 999999999] range).

    Last you can use time.FixedZone() to obtain a zone corresponding to the zone offset in the input, and then use Time.In() to "switch over" to this zone.

    Note that the milliseconds since epoch is zone-independent (it is given in UTC zone). Switching over to the source's zone offset is only required so that when you print the time (or you access its "fields" like year, month, day), you will see the intended date and time, as that would be different in another zone. For example the instance you have, it designates the date 1980-01-01 and 0 time in the day. Without the +0530 zone, that would be a different day, it would be 1979-12-31 18:30:00 +0000 UTC.

    Here's an example doing it:

    func parse(s string) (t time.Time, err error) {
        var ms int64
        var zhour, zmin int
    
        if _, err = fmt.Sscanf(s, "/Date(%d+%02d%02d)/", &ms, &zhour, &zmin); err != nil {
            if _, err = fmt.Sscanf(s, "/Date(%d-%02d%02d)/", &ms, &zhour, &zmin); err != nil {
                return time.Time{}, fmt.Errorf("Failed to parse: %v", err)
            }
            zhour = -zhour
            zmin = -zmin
        }
    
        t = time.Unix(0, ms*1000000)
        t = t.In(time.FixedZone("", zhour*3600+zmin*60))
        return
    }
    

    Testing it:

    fmt.Println(parse("/Date(315513000000+0530)/"))
    fmt.Println(parse("/Date(315513000000-0530)/"))
    fmt.Println(parse("invalid"))
    

    Output (try it on the Go Playground):

    1980-01-01 00:00:00 +0530 +0530 <nil>
    1979-12-31 13:00:00 -0530 -0530 <nil>
    0001-01-01 00:00:00 +0000 UTC Failed to parse: input does not match format
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题