Rails ignore daylight savings time

2019-06-06 14:47发布

I am having trouble understanding Rails' time zone support when it comes to daylight savings time.

I am storing all database times in UTC. User timezones are stored so that they map directly back to ActiveSupport::TimeZone values (i.e. Central Time (US & Canada)).

I want to completely ignore daylight savings time. If an event starts at 5:30pm, it always starts at 5:30pm whether daylight savings time is in effect or not.

Is it possible, considering all times are stored uniformly, to retrieve database times and display them locally so that daylight savings is completely ignored? Are there any problems I am going to run into ignoring daylight savings?

2条回答
我想做一个坏孩纸
2楼-- · 2019-06-06 15:25

Rails is going to convert your dates/times to the application configured timezone or the user timezone (assuming you have setup some sort of filter to use the user.timezone); and this WILL include manipulation based on DST.

You will need to override this behavior and there are probably a couple of options:

  1. Look into using Time.dst? in order to subtract / add time to whatever is being pulled out of the database
  2. Look into using skip_time_zone_conversion_for_attributes within AR and do the conversion yourself via virtual attributes
  3. Store the time as something other than a date/time; possibly as a separate field from the date in just a normal string format
查看更多
Deceive 欺骗
3楼-- · 2019-06-06 15:48

I don't mean to sound pedantic, but...

I want to completely ignore daylight savings time.

Well, you will be on your own then. Despite our best hopes and wishes, much of the real world uses daylight saving time. You can get a quick primer here.

If an event starts at 5:30pm, it always starts at 5:30pm whether daylight savings time is in effect or not.

5:30 for who? If you're saying 5:30 UTC, then sure. But if you're saying 5:30 in US Central Time, then you have to take DST into account. Otherwise, half of the year people will show up at your event at what they think is 5:30 and you think is 6:30.

Is it possible, considering all times are stored uniformly, to retrieve database times and display them locally so that daylight savings is completely ignored?

You're storing the times in UTC, which is good. When you display them locally, you should not ignore DST.

Are there any problems I am going to run into ignoring daylight savings?

Yes, people don't commonly understand this. It's generally expected that if you refer to a local time, that you mean a time that is local for them. If you don't include DST in that calculation, then you will have a disagreement about what time you are talking about.

Another word of advice, you might want to consider using the TZInfo gem instead of ActiveSupport::TimeZone. Then you would store time zone selection using the IANA identifiers such as America/Chicago. These are recognizable outside of Rails.

For some unexplained reason, the ActiveSupport folks thought they should limit time zones to the 146 values they felt were "meaningful". But they didn't explain their process, and they don't seem to be on top of maintenance. I've asked why, but haven't gotten much of a detailed response.

You may also wish to review the timezone tag wiki.

查看更多
登录 后发表回答