C#中的DateTimeOffset在Go语言中的等价物是什么?

huangapple go评论129阅读模式
英文:

What is the C# DateTimeOffset equivalent in Go

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. date := GetUtcTimestampFromAttribute()
  8. fmt.Println(date)
  9. if date != nil {
  10. fmt.Println(ToUnixTimeStamp(*date))
  11. }
  12. }
  13. func GetUtcTimestampFromAttribute() *time.Time {
  14. ticks := int64(7036640000000)
  15. fmt.Println(ticks)
  16. return GetUtcTimestampFromTicks(ticks)
  17. }
  18. func GetUtcTimestampFromTicks(ticks int64) *time.Time {
  19. fmt.Println(time.Unix(0, ticks))
  20. if ticks != 0 {
  21. t := time.Unix(0, ticks)
  22. return &t
  23. } else {
  24. return nil
  25. }
  26. }
  27. func ToUnixTimeStamp(timeStamp time.Time) int64 {
  28. epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
  29. return timeStamp.Sub(epoch).Seconds()
  30. }

例如:

输入:635804753769100000

输出:1444878577

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

请问有人可以帮我找到上述结果的解决方法吗?

谢谢

英文:

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.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. var date = GetUtcTimestampFromAttribute();
  6. Console.WriteLine(date);
  7. if (date != null)
  8. {
  9. Console.WriteLine(ToUnixTimeStamp(date.Value));
  10. }
  11. Console.ReadKey();
  12. }
  13. public static DateTimeOffset? GetUtcTimestampFromAttribute()
  14. {
  15. var ticks = long.Parse("7036640000000");
  16. Console.WriteLine(ticks);
  17. return GetUtcTimestampFromTicks(ticks);
  18. }
  19. public static DateTimeOffset? GetUtcTimestampFromTicks(long ticks)
  20. {
  21. Console.WriteLine(new DateTimeOffset(ticks, TimeSpan.Zero));
  22. return ticks != 0 ?
  23. new DateTimeOffset(ticks, TimeSpan.Zero) :
  24. (DateTimeOffset?)null;
  25. }
  26. public static long ToUnixTimeStamp(DateTimeOffset timeStamp)
  27. {
  28. var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
  29. return Convert.ToInt64((timeStamp - epoch).TotalSeconds);
  30. }
  31. }

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

答案1

得分: 6

我相信time包拥有你所需的一切,并且在我使用过的所有语言中,它是我认为最好的时间库。示例代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 这是如何解析一个Unix时间戳
  8. t := time.Unix(1444902545, 0)
  9. // 获取UTC时间
  10. fmt.Println("转换为UTC时间:", t.UTC())
  11. // 将其转换为任何时区:FixedZone可以接受UTC偏移和时区名称
  12. fmt.Println(t.In(time.FixedZone("IST", 7200)))
  13. }

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

  1. t.Unix()

或者

  1. t.UnixNano()
英文:

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:

  1. package main
  2. import(
  3. "fmt"
  4. "time"
  5. )
  6. func main(){
  7. // this is how you parse a unix timestamp
  8. t := time.Unix(1444902545, 0)
  9. // get the UTC time
  10. fmt.Println("The time converted to UTC:", t.UTC())
  11. // convert it to any zone: FixedZone can take a utc offset and zone name
  12. fmt.Println(t.In(time.FixedZone("IST", 7200)))
  13. }

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

  1. t.Unix()

or

  1. t.UnixNano()

答案2

得分: 2

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func TimeFromTicks(ticks int64) time.Time {
  7. base := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  8. return time.Unix(ticks/10000000+base, ticks%10000000).UTC()
  9. }
  10. func main() {
  11. fmt.Println(TimeFromTicks(635804753769100000))
  12. }

输出:

  1. 2015-10-15 03:09:36.0091 +0000 UTC
英文:

For example,

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func TimeFromTicks(ticks int64) time.Time {
  7. base := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  8. return time.Unix(ticks/10000000+base, ticks%10000000).UTC()
  9. }
  10. func main() {
  11. fmt.Println(TimeFromTicks(635804753769100000))
  12. }

Output:

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

huangapple
  • 本文由 发表于 2015年10月15日 17:37:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/33144967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定