Saving datetime in UTC isn't accurate sometimes

后端 未结 3 1581
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 21:34

In general, best practice when dealing with dates is to store them in UTC and convert back to whatever the user expects within the application layer.

That doesn\'t neces

3条回答
  •  走了就别回头了
    2021-02-01 22:06

    I vote the simplest route and that's saving to UTC. Create a column for country of origin, set up a google alert for "daylight savings time", and if Chile decides to stop using daylight savings or alter it in some crazy way, you can adapt by querying your database for Chilean users and adjusting their dates accordingly with a script. Then, you can use Time.parse with the date, time and timezone. To illustrate, here are the results the day before daylight savings and after daylight savings on 3/13/2016:

    Time.parse("2016-03-12 12:00:00 Pacific Time (US & Canada)").utc
    => 2016-03-12 20:00:00 UTC
    Time.parse("2016-03-14 12:00:00 Pacific Time (US & Canada)").utc
    => 2016-03-14 19:00:00 UTC
    

    This will get you a list of the accepted time zone names:

    timezones = ActiveSupport::TimeZone.zones_map.values.collect{|tz| tz.name}
    

提交回复
热议问题