This project is archived and is in readonly mode.
Inconsistant time zone handling
-
Geoff Buesing
- Milestone cleared.
- Assigned user set to Geoff Buesing
- State changed from new to open
I agree this is inconsistent -- TimeWithZone and Time.utc instances report themselves to the database as UTC, whereas Time.local instances report themselves in system local time.
Ideally, all of these objects would intelligently report themselves to the database either as UTC or system local time, depending upon the ActiveRecord::Base.default_timezone setting.
to_time isn't the place to add this kind of logic, given that it's used in contexts other than database serialization -- #to_s(:db) would be a better choice.
Another possibility -- maybe the cleanest choice -- would be to add logic to quoted_date to coerce the time object to utc or local before the #to_s(:db) call.
Of course, this kind of change could break some apps, if the app, say, uses Time.local instances as container objects to send utc values to the db -- kind of wonky, but there's nothing stopping you from doing this now.
Interested in hearing thoughts on this.
-
JackC
I added this line in an initializer and it seems to solve the problem.
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:db] = lambda { |time| time.utc.strftime( "%Y-%m-%d %H:%M:%S" ) }This gives Time and TimeWithZone the same behavior and the behavior exists in to_s(:db) for both of them.
-
Geoff Buesing
That would work. Though you should use the non-destructive #getutc instead of #utc, otherwise you might get some unexpected behavior.
-
JackC
I've been running this code with the change to #getutc for the last month in production without any issues. Any chance of getting this made the default?
-
Fernando Guillen
The @JackC fix doesn't work for me.
I had to define a new time_format:
Time::DATE_FORMATS[:m] = "%Y-%m-%d %H:%M:%S"And use it on my views
-
JackC
I don't see how just a format string would would also change a local time to UTC time.
Anyway this is the code I'm loading in an initializer and it is definitely sending UTC to the database.
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:db] = lambda { |time| time.getutc.strftime( "%Y-%m-%d %H:%M:%S" ) } -
Geoff Buesing
- State changed from open to wontfix
We're going to go forward with a different approach: quoted_date will do the conversion, instead of TimwWithZone#to_s(:db), see https://rails.lighthouseapp.com/projects/8994/tickets/2946-quoted_d...