英文:
What is the C# DateTimeOffset equivalent in Go
问题
我有以下代码,它接受一个字符串作为输入,将其转换为UNIX时间戳。我想在Go中做同样的事情,但我无法识别出相当于Go中DateTimeOffset结构或函数的结构或函数。
package main
import (
"fmt"
"time"
)
func main() {
date := GetUtcTimestampFromAttribute()
fmt.Println(date)
if date != nil {
fmt.Println(ToUnixTimeStamp(*date))
}
}
func GetUtcTimestampFromAttribute() *time.Time {
ticks := int64(7036640000000)
fmt.Println(ticks)
return GetUtcTimestampFromTicks(ticks)
}
func GetUtcTimestampFromTicks(ticks int64) *time.Time {
fmt.Println(time.Unix(0, ticks))
if ticks != 0 {
t := time.Unix(0, ticks)
return &t
} else {
return nil
}
}
func ToUnixTimeStamp(timeStamp time.Time) int64 {
epoch := time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
return timeStamp.Sub(epoch).Seconds()
}
例如:
输入: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.
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
答案1
得分: 6
我相信time
包拥有你所需的一切,并且在我使用过的所有语言中,它是我认为最好的时间库。示例代码如下:
package main
import (
"fmt"
"time"
)
func main() {
// 这是如何解析一个Unix时间戳
t := time.Unix(1444902545, 0)
// 获取UTC时间
fmt.Println("转换为UTC时间:", t.UTC())
// 将其转换为任何时区:FixedZone可以接受UTC偏移和时区名称
fmt.Println(t.In(time.FixedZone("IST", 7200)))
}
编辑 将时间对象转换回Unix时间戳很简单:
t.Unix()
或者
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:
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()
答案2
得分: 2
例如,
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论