使用 moment.js 防止重复发生的事件的 DST 转变 [英] preventing DST shift with moment.js for recurring events

查看:30
本文介绍了使用 moment.js 防止重复发生的事件的 DST 转变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建的系统有一个事件组件,其中一部分是创建重复事件的能力.在我的数据库中,我以 UTC 格式存储所有事件.当重复事件显示在用户的日历上时,它应始终显示在墙上时间"中.因此,例如,如果我在每周三下午 1:00 创建一个重复事件,无论夏令时如何,它都应该始终在下午 1:00.

The system I'm building has an events component and part of that is the ability to create recurring events. In my database, I'm storing all events in UTC. When a recurring event is displayed on the user's calendar, it should always be displayed in "wall time." So for example, if I create a recurring event every Wednesday at 1:00PM, regardless of the daylight savings time shift, it should always be at 1:00PM.

我遇到的问题是,每当我尝试使用 Moment.js 格式化此日期时,Moment 总是会考虑 DST 转换并相应地更新事件.以我之前的示例为例,在 2016 年,DST 班次发生在 3 月 13 日,因此如果我的约会是在 2016 年 2 月预订的,那么直到 3 月 13 日的每个约会都会正确输出为下午 1:00.3 月 13 日之后,将应用夏令时轮班,现在我的所有活动都提前了一个小时.

The issue that I'm running into is that whenever I attempt to format this date using Moment.js, Moment is always accounting for the DST shift and updating the event accordingly. Taking my previous example, in 2016, the DST shift occurs on March 13th, so if my appointment was booked in February of 2016 then every appointment until March 13th is correctly output as 1:00PM. After March 13th, the DST shift is applied and all my events are now an hour ahead.

有没有推荐的方法来处理这个问题?我似乎找不到让 Moment忽略"夏令时的方法,也没有在搜索时找到合适的解决方案.

Is there a recommended way to handle this? I can't seem to find a way to have Moment "ignore" DST nor have I found a proper solution when searching.

不确定这有多重要,但我在输出日期时使用 Moment Timezone 转换为用户的本地时区.

Not sure how important this is, but I am using Moment Timezone to convert to the user's local timezone when outputting the date.

谢谢!

推荐答案

作为最佳实践,未来的日期应该存储在本地时间.这是因为,正如评论中所指出的,时区规则可以而且确实会改变.因此,您无法提前正确计算本地时间的正确 UTC 日期.

As a matter of best practice, future dates should be stored in local time. This is because, as pointed out in the comments, time zone rules can and do change. As such, you cannot correctly compute the right UTC date for a local time ahead of time.

当您存储未来日期时,您应该将其与用户期望事件时间所在的 IANA 时区一起存储.然后您实际上会执行您现在正在执行的转换的相反操作.如果您需要知道事件在全球时间线上的确切时间点,您可以将本地时间转换为 UTC.

When you store the future date, you should store it with the IANA time zone that the user is expecting the event time to be in. Then you would actually do the reverse of the conversion you are doing now. You would convert from local time to UTC if you needed to know the event's exact point on the global timeline.

请注意,IANA 时区数据库会频繁更新.它现在是 2016d 版本,这意味着它今年已经更新了四次.如果您的应用程序将在多个国家/地区使用,则您需要非常勤奋地不断更新时刻时区以跟上变化.

Be aware that the IANA timezone database updates frequently. It is on version 2016d right now, which means that it has already updated four times this year. If you have an application that is going to be used in multiple countries, you will need to be very diligent about continually updating moment timezone to keep up with the changes.

至于为什么您现在看到 UTC 日期没有正确更改 - 我认为您正在使用预定日期,转换为 UTC,然后计算重复发生的时间.当您在 UTC 中进行日期计算时,它不会正确解释 DST 变化,因为它不知道它们.因此,当 moment 将该日期转换回本地时,它将关闭一个小时.如果您要在本地时间进行日期计算,然后转换为 UTC 进行存储,那么您的转换返回将是正确的,前提是时区规则没有改变.不过不要这样做 - 以当地时间存储日期.

As far as why you are seeing your UTC dates not change correctly right now - I think you are taking your scheduled date, converting to UTC, and then calculating the time of your recurrence. When you do your date math in UTC, it will not correctly account for the DST shifts because it has no knowledge of them. As such, when moment converts that date back to local, it will be off by an hour. If you were to do your date math in local time, and then convert to UTC to store, your conversion back would be correct PROVIDED that the timezone rules have not changed. Don't do this though - store the dates in local time.

这篇关于使用 moment.js 防止重复发生的事件的 DST 转变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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