What is the C# DateTimeOffset equivalent in Go

Prashant :

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

For example:

Input: 635804753769100000

Output: 1444878577

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.

Thanks

peterSO :

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

What is the Linux equivalent of Windows Startup?

分類Dev

What is the apt equivalent to these dselect commands?

分類Dev

What is equivalent of php $_REQUEST in golang?

分類Dev

Goで対応するC#のDateTimeOffsetとは

分類Dev

What is the Go equivalent of C++ initializer lists?

分類Dev

WTForms equivalent for Go?

分類Dev

Go equivalent of PHP's 'implode'

分類Dev

What is the PostgreSQL equivalent for ISNULL()

分類Dev

What is the equivalent in Swift to NSParameterAssert?

分類Dev

What is the swift equivalent of this cast?

分類Dev

What is the equivalent of QVariant in C++?

分類Dev

What is the proper equivalent of "while(true)" in plain C?

分類Dev

What is the Matlab equivalent to Python's `not in`?

分類Dev

What is the Swift equivalent of C#/.NET/LINQ's Enumerable.All method?

分類Dev

What's the Swift equivalent of Objective-C's "#ifdef __IPHONE_11_0"?

分類Dev

What is the MutableLiveData equivalent in RxJava?

分類Dev

What is the equivalent of Android LiveData in Flutter?

分類Dev

What is the nodejs equivalent of PHP trait

分類Dev

What is the idiomatic F# equivalent to declaring a variable for later use in C#

分類Dev

What is the equivalent javascript closure in c#?

分類Dev

What is the regs() equivalent in Elixir?

分類Dev

What is the python equivalent of grep?

分類Dev

What is the equivalent of MATLAB?

分類Dev

what is the equivalent UpdateSystemActivity on windows

分類Dev

What are the go-to tools for finding errors in C code?

分類Dev

What is the java equivalent bounded wildcard in C# properties?

分類Dev

What does SSMS show for a DateTimeOffset(7)?

分類Dev

What is the C# equivalent code for C programming statement?

分類Dev

What would be the C equivalent of rlwinm (PPC Instruction)

Related 関連記事

  1. 1

    What is the Linux equivalent of Windows Startup?

  2. 2

    What is the apt equivalent to these dselect commands?

  3. 3

    What is equivalent of php $_REQUEST in golang?

  4. 4

    Goで対応するC#のDateTimeOffsetとは

  5. 5

    What is the Go equivalent of C++ initializer lists?

  6. 6

    WTForms equivalent for Go?

  7. 7

    Go equivalent of PHP's 'implode'

  8. 8

    What is the PostgreSQL equivalent for ISNULL()

  9. 9

    What is the equivalent in Swift to NSParameterAssert?

  10. 10

    What is the swift equivalent of this cast?

  11. 11

    What is the equivalent of QVariant in C++?

  12. 12

    What is the proper equivalent of "while(true)" in plain C?

  13. 13

    What is the Matlab equivalent to Python's `not in`?

  14. 14

    What is the Swift equivalent of C#/.NET/LINQ's Enumerable.All method?

  15. 15

    What's the Swift equivalent of Objective-C's "#ifdef __IPHONE_11_0"?

  16. 16

    What is the MutableLiveData equivalent in RxJava?

  17. 17

    What is the equivalent of Android LiveData in Flutter?

  18. 18

    What is the nodejs equivalent of PHP trait

  19. 19

    What is the idiomatic F# equivalent to declaring a variable for later use in C#

  20. 20

    What is the equivalent javascript closure in c#?

  21. 21

    What is the regs() equivalent in Elixir?

  22. 22

    What is the python equivalent of grep?

  23. 23

    What is the equivalent of MATLAB?

  24. 24

    what is the equivalent UpdateSystemActivity on windows

  25. 25

    What are the go-to tools for finding errors in C code?

  26. 26

    What is the java equivalent bounded wildcard in C# properties?

  27. 27

    What does SSMS show for a DateTimeOffset(7)?

  28. 28

    What is the C# equivalent code for C programming statement?

  29. 29

    What would be the C equivalent of rlwinm (PPC Instruction)

ホットタグ

アーカイブ