本文介绍了什么是C#的DateTimeOffset相当于围棋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码,它接受一个字符串作为输入被转换成UNIX时间戳。我想要做相同的golang但我不能够识别的结构或功能,这将给围棋的DateTimeOffset结构的等效。

I have the following code which takes a string as input which is converted into UNIX time stamp. I want to do the same in golang but I am not able to recognize the struct or function which will give an equivalent of DateTimeOffset structure in Go.

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

例如:

输入:635804753769100000

Input: 635804753769100000

输出:1444878577

Output: 1444878577

相应时间在UTC:10 /二千零十五​​分之十五上午03点09分36秒+00:00

Corresponding Time in UTC : 10/15/2015 3:09:36 AM +00:00

有人可以帮我上一种变通方法,以获得上述结果。

Can someone please help me on a workaround to get the above result.

感谢

推荐答案

例如,

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

输出:

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

这篇关于什么是C#的DateTimeOffset相当于围棋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 20:14