Go中的C#DateTimeOffset等价物是什么

Pra*_*ant 0 c# datetime go

我有以下代码,它将一个字符串作为输入转换为UNIX时间戳.我想在golang中做同样的事情,但我无法识别结构或函数,它将在Go中提供相当于DateTimeOffset的结构.

class Program
{
    static void Main(string[] args)
    {
        var date = GetUtcTimestampFromAttribute();
        Console.WriteLine(date);
        if (date != null)
        {
            Console.WriteLine(ToUnixTimeStamp(date.Value));
        }

        Console.ReadKey();
    }

    public static DateTimeOffset? GetUtcTimestampFromAttribute()
    {
        var ticks = long.Parse("7036640000000");
        Console.WriteLine(ticks);
        return GetUtcTimestampFromTicks(ticks);
    }

    public static DateTimeOffset? GetUtcTimestampFromTicks(long ticks)
    {
        Console.WriteLine(new DateTimeOffset(ticks, TimeSpan.Zero));
        return ticks != 0 ?
            new DateTimeOffset(ticks, TimeSpan.Zero) :
            (DateTimeOffset?)null;
    }

    public static long ToUnixTimeStamp(DateTimeOffset timeStamp)
    {
        var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
        return Convert.ToInt64((timeStamp - epoch).TotalSeconds);
    }
}
Run Code Online (Sandbox Code Playgroud)

例如:

输入:635804753769100000

产量:1444878577

UTC的对应时间:2015年10月15日上午03:09:36 +00:00

有人可以帮我解决方法,以获得上述结果.

谢谢

Not*_*fer 6

我相信这个time软件包有你需要的一切,IMO是我用过任何语言的最好的时间库.例:

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)))

}
Run Code Online (Sandbox Code Playgroud)

编辑将时间对象转换回unix时间戳很简单:

t.Unix()
Run Code Online (Sandbox Code Playgroud)

要么

t.UnixNano()
Run Code Online (Sandbox Code Playgroud)