将int64类型(来自UnixNano)转换为位置时间字符串。

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

Go convert int64 (from UnixNano) to location time string

问题

我有一个int64类型的数值:

> 1502712864232

这是通过REST GET请求获得的结果。我可以很容易地将其转换为字符串。它是一个Unix纳秒级时间戳。

我真正需要的是将其转换为与时区相关的字符串,例如"Europe/London",如下所示:

> "14/08/2017, 13:14:24"

就像这个实用工具所生成的一样:
http://www.freeformatter.com/epoch-timestamp-to-date-converter.html

非常感谢任何帮助。

==> 更新。

感谢@evanmcdonnal提供如此有用的答案。非常感谢。
事实证明,我拥有的数据根本不是Unix纳秒级时间戳(抱歉),而是自纪元以来的毫秒数。源自Jenkins的时间戳...

因此...我编写了以下辅助函数来获得我所需的结果:

// 参数1是表示自纪元以来的毫秒数的int64类型
// 参数2是时区。例如:"Europe/London"
// 参数3是与返回的字符串格式相关的整数
// 需要time包。显然。

func getFormattedTimeFromEpochMillis(z int64, zone string, style int) string {
	var x string
	secondsSinceEpoch := z / 1000
	unixTime := time.Unix(secondsSinceEpoch, 0)
	timeZoneLocation, err := time.LoadLocation(zone)
	if err != nil {
		fmt.Println("加载时区出错:", err)
	}

	timeInZone := unixTime.In(timeZoneLocation)

	switch style {
	    case 1:
		    timeInZoneStyleOne := timeInZone.Format("Mon Jan 2 15:04:05")
		    //Mon Aug 14 13:36:02
		    return timeInZoneStyleOne
	    case 2:
		    timeInZoneStyleTwo := timeInZone.Format("02-01-2006 15:04:05")
		    //14-08-2017 13:36:02
		    return timeInZoneStyleTwo
	    case 3:
		    timeInZoneStyleThree := timeInZone.Format("2006-02-01 15:04:05")
		    //2017-14-08 13:36:02
		    return timeInZoneStyleThree
	}
	return x
}
英文:

I have an int64 like:

> 1502712864232

Which is the result of a REST GET to a service. I can convert it to a string easily enough. It is a Unixnano timestamp.

What I really need is to convert it to a string which is related to a timezone like "Europe/London". Such as:-

> "14/08/2017, 13:14:24"

Such as produced by this handy utility:
http://www.freeformatter.com/epoch-timestamp-to-date-converter.html

Would very much appreciate any assistance.

==> Update.

Thanks to @evanmcdonnal for such a useful answer. Much appreciated.
Turns out that the data I had was not UnixNano at all (sorry) it was milliseconds from Epoch. Source is a Jenkins timestamp....

So... I wrote the following helper function to get what I need:

// Arg 1 is an int64 representing the millis since Epoch
// Arg 2 is a timezome. Eg: "Europe/London"
// Arg 3 is an int relating to the formatting of the returned string
// Needs the time package. Obviously.

func getFormattedTimeFromEpochMillis(z int64, zone string, style int) string {
	var x string
	secondsSinceEpoch := z / 1000
	unixTime := time.Unix(secondsSinceEpoch, 0)
	timeZoneLocation, err := time.LoadLocation(zone)
	if err != nil {
		fmt.Println("Error loading timezone:", err)
	}

	timeInZone := unixTime.In(timeZoneLocation)

	switch style {
	    case 1:
		    timeInZoneStyleOne := timeInZone.Format("Mon Jan 2 15:04:05")
		    //Mon Aug 14 13:36:02
		    return timeInZoneStyleOne
	    case 2:
		    timeInZoneStyleTwo := timeInZone.Format("02-01-2006 15:04:05")
		    //14-08-2017 13:36:02
		    return timeInZoneStyleTwo
	    case 3:
		    timeInZoneStyleThree := timeInZone.Format("2006-02-01 15:04:05")
		    //2017-14-08 13:36:02
		    return timeInZoneStyleThree
	}
	return x
}

答案1

得分: 14

不要担心,我会为你翻译这段代码。以下是翻译的结果:

// 导入必要的包
import "time"
import "fmt"

// 将时间戳转换为Time对象
t := time.Unix(0, 1502712864232)

// 将Time对象格式化为字符串并打印输出
fmt.Println(t.Format("02/01/2006, 15:04:05"))

请注意,根据你提供的时间戳值,无论是以纳秒还是以秒为单位,生成的时间值都与预期的相差很大。上面的代码仍然演示了你想要实现的基本思路,但似乎需要额外的步骤,或者你提供的示例int64与提供的字符串不对应。

相关文档:

https://golang.org/pkg/time/#Unix

https://golang.org/pkg/fmt/

英文:

Rather than converting this to a string, you'll want to convert it to a time.Time and from there to a string. You can use the handy Unix method to get a Time object back for that time stamp.

import "time"
import "fmt"

t := time.Unix(0, 1502712864232)
fmt.Println(t.Format("02/01/2006, 15:04:05"))

Edit: added format to the println - note, testing your unix stamp in go playground, that value is neither nano seconds nor seconds, in both cases the time value produced is way off of what it should be. The code above still demonstrates the basic idea of what you want to do but it seems an additional step is necessary or the sample int64 you gave just does not correspond to the string you provided.

relevant docs:

https://golang.org/pkg/time/#Unix

https://golang.org/pkg/fmt/

huangapple
  • 本文由 发表于 2017年8月15日 01:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/45679529.html
匿名

发表评论

匿名网友

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

确定