什么时候可以将日期时间存储为本地时间而不是UTC? [英] When is it ok to store datetimes as local time rathen than UTC?

查看:57
本文介绍了什么时候可以将日期时间存储为本地时间而不是UTC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个与

This is a question similar to this one.

我真的很想在我的应用程序中将日期时间存储为本地时间而不是UTC(这被认为是最佳做法).在应用程序中,我发生了许多事件,每个事件都分配给给定位置.总是在向用户显示它们时,我想显示事件的本地时间.即:

I'm really tempted to store datetimes in my app as local time rather than as UTC (which is considered a best practice). In the app I have a number of events happening, each assigned to a given location. Always when I display them to the user, I want to show the local time of the event. I.e.:

====================================================================================
Event time (with TZ)   | As UTC           | As local time    | To be displayed  |
====================================================================================
2014-01-15 22:30 GMT   | 2014-01-15 22:30 | 2014-01-15 22:30 | 2014-01-15 22:30 |
2014-01-15 23:30 GMT+1 | 2014-01-15 22:30 | 2014-01-15 23:30 | 2014-01-15 23:30 |
2014-01-16 00:30 GMT+2 | 2014-01-15 22:30 | 2014-01-16 00:30 | 2014-01-16 00:30 |
====================================================================================

如果我决定将活动时间存储在UTC中:

If I decide to store the event times in UTC:

  • 将很难显示它们(因为每个事件我都需要具有事件的时区并格式化该时区的日期).

  • it will be difficult to display them (because with each event I need to have the event's timezone and format the date for that timezone).

很难查询它们(即,如果我想显示2014年1月15日当地时间发生的所有事件,我需要为每个事件将事件的时间与'2014-01-15'表示该事件所在的时区.不确定在SQL中是否甚至可以实现...)

it will be very difficult to query them (i.e. if I want to show all events that happened on 2014-01-15 local time I need for each event to compare that event's time with what '2014-01-15' means in that event's timezone. Not sure if this is even possible in SQL...)

如果我决定将事件时间存储为本地时间:

If I decide to store the event times as local times:

  • 我将无法比较不同位置的活动时间(但这对我来说是可以的)

由于在应用程序中绝大多数情况下,我对本地时间(通常显示电视时间")很感兴趣,所以在很多情况下我都创建新的datetime对象(例如我需要使用位置时区),在这种情况下,我认为将日期时间保存为本地时间是合理的.

Since in the vast majority of cases in the app I'm interested in the local time (usually displaying the, so called "television time") and there aren't many cases where I'm creating new datetime objects (for which I need the location timezone), I believe saving datetimes as local time is justified in this case.

您认为是吗?存储当地时间还有哪些其他弊端?

Do you think it is? What are other downsides of storing local times?

在此先感谢您的关注和帮助.

Thanks in advance for your attention and help.

推荐答案

使用UTC的最常见的情况是安排未来时间的调度-尤其是以重复模式.

The most commonly overlooked case of when not to use UTC is for scheduling of future times - especially in recurrence patterns.

想象一下,如果您的闹钟是UTC安排的.假设您将其设置为每天7:00 AM.DST转换后的第二天,您将在6:00 AM或8:00 AM醒来,具体取决于转换的方向.

Imagine if you're alarm clock was scheduled by UTC. Say you set it for 7:00 AM daily. On the day after a DST transition, you'd either wake up at 6:00 AM or 8:00 AM depending on which direction the transition was.

此外,我们确定时区偏移量和夏时制更改的规则始终都在更新!因此,在没有保留本地时间本身的情况下,您不能占用将来的当地时间并将其转换为UTC.否则,当事情发生变化时,您将没有原始的真理来源,而您所有的时间都会再次消失.

Also, the rules by which we determine time zone offsets and daylight saving time changes get updated all the time! So you can't take some future local time and convert it to UTC without also retaining the local time itself. Otherwise, when things change, you won't have an original source of truth and again all your times will be off.

我已发布在 时间 当然,一旦事件发生,那么您当然希望在UTC或使用 DateTimeOffset 记录时间.a>.

Of course, once the event has occurred, then you certainly want to record the time either at UTC, or using a DateTimeOffset.

另一个常见的用例是没有时间的日期,尤其是生日和其他周年纪念日.这些值应始终仅存储为年,月,日(例如,在大多数数据库中以 date 字段类型存储)-在时区或UTC之间不进行转换.

Another common use case is for dates without times, especially birth dates and other anniversary dates. These should always just be stored as year, month, day (such as in a date field type in most databases) - with no conversion between time zones or UTC.

关于您的具体观点:

如果我决定将事件时间存储在UTC中...将很难显示它们...

If I decide to store the event times in UTC ... it will be difficult to display them...

实际上,这很容易.几乎每个编程环境都可以轻松做到这一点.唯一困难的地方是在非本地时区的JavaScript中,并且有相应的库可以解决这个问题.

That is quite easy actually. Almost every programming environment can do this easily. The only place this is difficult is in JavaScript for non-local time zones, and there are libraries to cope with that.

如果我决定将事件时间存储在UTC中...将很难查询它们(即,如果我想显示2014年1月15日当地时间发生的所有事件,我需要对每个事件进行比较该事件的时间与该事件的时区中的"2014-01-15"的含义有关.不确定在SQL中是否甚至可以做到这一点...)

If I decide to store the event times in UTC ... it will be very difficult to query them (i.e. if I want to show all events that happened on 2014-01-15 local time I need for each event to compare that event's time with what '2014-01-15' means in that event's timezone. Not sure if this is even possible in SQL...)

是的.每个人的今天"都不一样.如果您需要将数据绑定到浮动的日",那么这就是不存储在UTC中的另一种情况.但是, DateTimeOffset 将为您提供两者的优势.

That is true. Everybody's "today" is different. If you need data tied to a floating "day" then that is another case for not storing in UTC. However, a DateTimeOffset would give you the advantages of both.

如果我决定将事件时间存储为本地时间...我将无法比较不同位置的事件时间(但这对我来说是可以的)

If I decide to store the event times as local times ... I will not be able to compare times of events for different locations (but this is ok for me)

不仅如此.没有偏移的本地DateTime可能与夏令时后备转换不明确.因此,您可能只有一个本地值,无法区分它对应的两个时间点中的哪一个.

It's more than that. A local DateTime without an Offset may be ambiguous to to daylight saving time fall-back transitions. So you could have a single local value that you can't distinguish which of two points in time it corresponds to.

关于电视时间"-将该概念保留为真正浮动"的内容.示例:一家公司在世界范围内的办事处都始于8:00 AM.那是一个浮动的时间.

Regarding "television time" - reserve that concept for things that are truly "floating". Example: A company has offices all over the world that all start at 8:00 AM. That's a floating time.

这篇关于什么时候可以将日期时间存储为本地时间而不是UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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