What is the C# DateTimeOffset equivalent in Go

蓝咒 提交于 2019-12-01 14:39:58

For example,

package main

import (
    "fmt"
    "time"
)

func TimeFromTicks(ticks int64) time.Time {
    base := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
    return time.Unix(ticks/10000000+base, ticks%10000000).UTC()
}

func main() {
    fmt.Println(TimeFromTicks(635804753769100000))
}

Output:

2015-10-15 03:09:36.0091 +0000 UTC

I believe the time package has everything you need and it is IMO the best time library I've worked with in any language. Example:

package main 

import(
    "fmt"
    "time"
)

func main(){

    // this is how you parse a unix timestamp    
    t := time.Unix(1444902545, 0)

    // get the UTC time
    fmt.Println("The time converted to UTC:", t.UTC())

    // convert it to any zone: FixedZone can take a utc offset and zone name
    fmt.Println(t.In(time.FixedZone("IST", 7200)))

}

EDIT Converting the time object back to a unix timestamp is simple:

t.Unix()

or

t.UnixNano()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!