dspym82000 2017-08-14 17:06 采纳率: 100%
浏览 264
已采纳

将int64(从UnixNano)转换为位置时间字符串

I have an int64 like:

1502712864232

Which is the result of a REST GET to a service. I can convert it to a string easily enough. It is a Unixnano timestamp.

What I really need is to convert it to a string which is related to a timezone like "Europe/London". Such as:-

"14/08/2017, 13:14:24"

Such as produced by this handy utility: http://www.freeformatter.com/epoch-timestamp-to-date-converter.html

Would very much appreciate any assistance.

==> Update.

Thanks to @evanmcdonnal for such a useful answer. Much appreciated. Turns out that the data I had was not UnixNano at all (sorry) it was milliseconds from Epoch. Source is a Jenkins timestamp....

So... I wrote the following helper function to get what I need:

// Arg 1 is an int64 representing the millis since Epoch
// Arg 2 is a timezome. Eg: "Europe/London"
// Arg 3 is an int relating to the formatting of the returned string
// Needs the time package. Obviously.

func getFormattedTimeFromEpochMillis(z int64, zone string, style int) string {
    var x string
    secondsSinceEpoch := z / 1000
    unixTime := time.Unix(secondsSinceEpoch, 0)
    timeZoneLocation, err := time.LoadLocation(zone)
    if err != nil {
        fmt.Println("Error loading timezone:", err)
    }

    timeInZone := unixTime.In(timeZoneLocation)

    switch style {
        case 1:
            timeInZoneStyleOne := timeInZone.Format("Mon Jan 2 15:04:05")
            //Mon Aug 14 13:36:02
            return timeInZoneStyleOne
        case 2:
            timeInZoneStyleTwo := timeInZone.Format("02-01-2006 15:04:05")
            //14-08-2017 13:36:02
            return timeInZoneStyleTwo
        case 3:
            timeInZoneStyleThree := timeInZone.Format("2006-02-01 15:04:05")
            //2017-14-08 13:36:02
            return timeInZoneStyleThree
    }
    return x
}
  • 写回答

1条回答 默认 最新

  • duanbin3021 2017-08-14 17:13
    关注

    Rather than converting this to a string, you'll want to convert it to a time.Time and from there to a string. You can use the handy Unix method to get a Time object back for that time stamp.

    import "time"
    import "fmt"
    
    t := time.Unix(0, 1502712864232)
    fmt.Println(t.Format("02/01/2006, 15:04:05"))
    

    Edit: added format to the println - note, testing your unix stamp in go playground, that value is neither nano seconds nor seconds, in both cases the time value produced is way off of what it should be. The code above still demonstrates the basic idea of what you want to do but it seems an additional step is necessary or the sample int64 you gave just does not correspond to the string you provided.

    relevant docs:

    https://golang.org/pkg/time/#Unix

    https://golang.org/pkg/fmt/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!