英文:
How to parse a timestamp used by PRTG
问题
我有一个日期时间字符串,格式为44340.5416666667
,但我想将其转换为5/24/2021 3:00:00 PM - 4:00:00 PM
的格式。我该如何使用Golang解析它?我尝试了一些转换函数,但没有成功。
英文:
I have a datetime string this format
44340.5416666667
but i want to convert this to 5/24/2021 3:00:00 PM - 4:00:00 PM
format. How can i parse that with golang? I tried some convert function but it didn't work.
答案1
得分: 3
根据https://kb.paessler.com/en/topic/1313-how-do-i-translate-prtg-timestamp-values-format-to-normal-time-format,PRTG使用的时间戳格式似乎被定义为自1899年12月30日以来的天数值。
根据上述链接,以下Go代码应将时间戳转换为Go的Time
实例:
prtg := 44340.5416666667
// 减去1899年12月30日和1970年1月1日之间的天数,并转换为毫秒
millis := int64((prtg - 25569) * 86400 * 1000)
t := time.Unix(0, millis*int64(time.Millisecond))
println(t.Format("1/2/2006 03:04:05 PM"))
英文:
According to https://kb.paessler.com/en/topic/1313-how-do-i-translate-prtg-timestamp-values-format-to-normal-time-format, the timestamp format used by PRTG seems to be defined as the value of days since Dec 30, 1899.
Following the above link, the following Go code should convert the timestamp into a Go Time
instance:
prtg := 44340.5416666667
// substract number of days between Dec 30, 1899 and Jan 1, 1970 and convert to millis
millis := int64((prtg - 25569) * 86400 * 1000)
t := time.Unix(0, millis*int64(time.Millisecond))
println(t.Format("1/2/2006 03:04:05 PM"))
答案2
得分: 1
根据Gregor Zurowski的评论中提到的PRTG时间戳,将您的时间转换为纳秒(时间的最小单位,以提高准确性),然后加上1899-12-30 12:00午夜的Unix纳秒时间。然后将其重新转换为时间,并按以下格式进行格式化:
package main
import (
"fmt"
"time"
)
func main() {
startDate := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC).UnixNano()
timeVar := 44340.5416666667 //您的时间变量
duration := startDate + int64(float64(24*60*60) * timeVar * 1e9) //从起始日期开始的持续时间(以纳秒为单位)
fmt.Println(time.Unix(0, duration).Format("1/2/2006 03:04:05 PM"))
}
英文:
According to prtg timestamp mentioned in Gregor Zurowski's comment,
convert your time to nano seconds
(minimum unit in time to more accurate) and add unix nano of 1899-12-30 12.00 midnight.
re convert it to time and format it as below
package main
import (
"fmt"
"time"
)
func main() {
startDate := time.Date(1899, 12, 30, 0, 0, 0, 0, time.UTC).UnixNano()
timeVar := 44340.5416666667 //your time variable
duration := startDate + int64(float64(24*60*60) * timeVar * 1e9) //duration since start date in nanoseconds
fmt.Println(time.Unix(0, duration).Format("1/2/2006 03:04:05 PM"))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论