Using UTC with Sequel?

社会主义新天地 提交于 2019-12-05 00:11:32

问题


I'd like to not store times in my local timezone, but Sequel is making it really tough on me. I can set them to UTC before I put them in there (a bit of a pain), but then when I take them back out it assumes that they are local dates and then they are all 8 hours in the future. Is this something that hasn't been implemented yet? And if so, are there any workarounds? Thanks!


回答1:


The easiest way to try to put them in as UTC is to override literal_datetime and/or literal_time for the dataset class you are using to return a literal string of the UTC time.

Getting them out in UTC depends on the adapter you are using. For example, the postgres adapter calls Sequel.string_to_datetime, which just calls parse on the Sequel.datetime_class (Time by default). If the datetime column includes the timezone information, things should work fine. If it doesn't include timezone information, Time.parse is going to assume it is given a local time. In that case, you may want to override Sequel.string_to_datetime to make sure it always returns the time with the UTC offset (maybe by calling Time.parse(s).gmtime).




回答2:


This is a bit outdated at this point but I believe the best solution here has changed since the original answer was posted. If you set

Sequel.default_timezone = :utc

sequel will treat all times as UTC and wont exhibit the behavior described in the question.

Find more info at http://sequel.jeremyevans.net/rdoc/classes/Sequel/Timezones.html




回答3:


Just had a very similar issue myself.

This information has been taken from the Sequel RDoc

Sequel can use either Time or DateTime for times returned from the database. It defaults to Time. To change it to DateTime, use:

Sequel.datetime_class = DateTime

Also make sure your not storing the timezone information in your database. I'm using Postgres and the column type is timestamp without time zone.

This should result in the displayed Date/Time being in UTC. It has worked for me when passing in a Date/Time of 2009-07-13T03:22:53Z the result is displayed as 2009-07-13T03:22:53+00:00



来源:https://stackoverflow.com/questions/1114725/using-utc-with-sequel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!