database - 如何将 +0000 UTC 转换为 +0100 CET

标签 database parsing go time nextcloud

我编写了一个基于当前时间返回摘要的程序,但是我遇到了一个问题,即当前时间是我的本地格式,但是我从数据库中获取的开始和结束时间在不同的时区,所以在示例中而不是事件持续时间为 16:00 至 23:59 持续时间为 17:00 至 00:59(时差一小时)

currenttime 2020-02-12 19:59:56.161262078 +0100 CET m=+0.001121940
eventstart 2019-02-13 16:00:00 +0000 UTC
eventend 2019-02-13 23:59:00 +0000 UTC

有没有办法“解析” eventstart 和 eventend 在同一个时区?

最佳答案

如果事件存储在某个地方(在数据库中),它将被保存为来自 UTC 的时间戳。当你想显示 本地 时间在不同的时区,你可以使用这样的东西:

startTime := time.Now()
// startTime will be in UTC and the timezone
// will be set to whatever your local timezone is

// load the location you want to display the time in 
loc, _ := time.LoadLocation("Europe/Berlin")

// set timezone and display:
timeToDisplay := startTime.In(loc).String()
fmt.Println(timeToDisplay)

关于database - 如何将 +0000 UTC 转换为 +0100 CET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60203373/

相关文章:

php - WordPress 安装显示索引/

SQL Server 2005 : Wrapping Tables by Views - Pros and Cons

.net - 将文本字符串解析为 F# 代码

javascript - 表达式解析 : how to tokenize

javascript - 尽管格式看似正确,但 d3.js 无法将我的字符串解析为日期? (v4)

go - 为什么标准库bufio.Read(p [] byte)(int,err)在Go中检查len(p)而不是cap(p)

html - 如何对齐和距离 td 值

sql-server - GUID 是否应该用作许多表的外键

go - 本地主机 UDP 客户端未从 UDP 代理接收数据包

go - 有没有一种简单的方法可以在 Golang 中将数据库行转换为 JSON