我需要在数据库中保存什么才能转换为 UTC?
我的问题是:我有很多包含日期时间(以自己的当地时间)的数据,我需要将其转换为 UTC。看起来是这样:
class Data
{
int id; //Some id for database
DateTime time; //Time in local time, i need to convert it to utc
}
//Converting:
1)Find this id in database and extract some info (which excatly?)
2)Convert time to UTC
我的问题是:我需要在数据库中保留哪种数据才能正确转换时间?
我的版本是:保持当地时间和UTC之间的整数小时差,并通过将其添加到当地时间进行转换。但这是合适的解决方案吗?
My problem is: I have a lot of data containg a date time (in own local time) and I need to convert It to UTC. It looks so:
class Data
{
int id; //Some id for database
DateTime time; //Time in local time, i need to convert it to utc
}
//Converting:
1)Find this id in database and extract some info (which excatly?)
2)Convert time to UTC
My question is: which sort of data I need to keep in database in order to convert time correctly?
My version is: to keep an integer difference in hours between local time and utc and convert by adding It to local time. But Is this appropriate solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
普遍接受的解决方案是始终存储 UTC 日期时间,然后在加载或显示给用户时将其转换为本地时间
The generally accepted solution is to always store the UTC date time and then convert it into local time when loading or displaying it to the user
保留整数差异是不够的,因为这不能区分夏令时 (DST) 和非 DST。
最好的方法是保留时区。通用且支持良好的时区系统是 Olson Tz。
它们是代表时区的大城市名称的引用。例如,太平洋时间为 America\Los_Angeles。请注意,这包括基于存储日期的日光和非日光。因此,您无需知道日光何时开始 - 这已纳入奥尔森时区。
也请查看此处。
Keeping an integer difference is not enough, because this doesn't differentiate between say daylight saving time (DST) and non-DST.
The best way is to keep a time zone. Universal and very well supported time zone system is Olson Tz.
They are references by names of big cities, which are representative of the time zone. For example Pasific Time would be America\Los_Angeles. Note that this includes both daylight and non-daylight based on what the stored date is. So you don't need to know when daylight caomes into play - this is incorporated in the olson time zone.
Look here too.