为什么要使用UTC? [英] Why should I use UTC?

查看:325
本文介绍了为什么要使用UTC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用当前使用本地时间而不是UTC.我知道使用UTC很重要,但是我一生都无法记住原因.

Our app currently uses a local time rather than UTC. I know that it's important to use UTC, but cannot for the life of me remember why.

假定将DateTimes存储为偏移量,则在将本地时间与UTC时间进行比较时,或与其他时区不同的时区进行比较时,无疑任何值得使用的库都将知道不同的时区并将这两个对象变异为某种东西可以比较吗?

Assuming that the DateTimes are stored with an offset, when comparing a local time to a UTC time, or with another time with a different timezone, surely any library worth using will know about the different timezones and mutate the two objects into something that can be compared?

只要偏移量与DateTime一起传递(例如,它将使用对象而不是字符串),我就看不出它为什么重要.为什么我应该处理2014-09-01T13:44:13+00:00而不是2014-09-01T14:44:13+01:00?实际上,将其存储为UTC会丢失偏移信息(声明时间的本地时间).

As long as the offset is passed around with the DateTime (which it will be using objects rather than strings, say), I do not see why it matters. Why should I deal with 2014-09-01T13:44:13+00:00 rather than 2014-09-01T14:44:13+01:00? In fact, storing as UTC looses the offset information (the local time when the time was declared).

我在这里想念什么?

上下文:我们遇到了一些限制,出现了一个样式错误,我以为啊哈:将所有内容移至UTC",但随后我意识到我只是在经历转换一堆DateTime的代码反对使用UTC时区,这让我很浪费时间.

推荐答案

您的用例允许存储带偏移量的本地时间. IMO无需存储在UTC中,因为您始终可以将时间点转换为任何其他本地时间.实际上,它还为您提供了更多信息,因为您还可以获得有关当地时间的信息.

Your use case allows storing a local time with an offset. IMO there is no need to store in UTC, as you can always translate the point in time into any other local time. In fact it gives you even more, as you have additionally the information about the local time.

从技术上讲,将日期分别存储为UTC和偏移量有一个优势:您可以对字符串进行排序,这在例如不支持带偏移量的日期字段的DBS(例如MySQL).

Technically there is an advantage in storing the date as UTC and the offset separately: You can sort on a string, which gives you an advantage in e.g. DBS which don't support date fields with offsets properly (e.g. MySQL).

如果您考虑不允许存储偏移量的系统,则参与者必须在一个公共时区上达成一致. UTC似乎是非常稳定的候选人,并具有良好的时区翻译支持.

If you consider systems which don't allow storing an offset, the actors will have to agree on a common timezone. UTC seems to be a very stable candidate with good timezone translation support.

例如, Exif 时区信息.另一个示例是多操作系统计算机中的硬件时钟.如果演员不同意相同的时区,您会头疼(Linux和Windows在DST切换期间会变得很有趣).

One example for this is Exif where you can only store a local date without any timezone information. Another example is the hardware clock in a multi OS computer. If the actors don't agree on a common timezone, you will get headaches (Linux & Windows can get funny during DST switch).

这篇关于为什么要使用UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆