Rick Schumeyer
2009-Mar-31 14:08 UTC
TimeWithZone seems in rails 2.3 seems broken...is this the correct behavior?
My environment.rb contains config.time_zone = ''UTC'' If my understanding is correct, rails should assume time values coming from the database are UTC, and since config.time_zone is set to ''UTC'', it should not try to convert the times. Instead, rails is assuming that the db values are Eastern (my local zone) and is incorrectly adding four hours to convert to what it thinks is UTC. Either my understanding of how this is supposed to work is wrong, or something needs to be fixed. I created a test table in a SQL Server database with one row. The table is called events and contains an "edate" column. The value is "2009-03-30 12:00", and is a UTC time. The output from script/console is below. (results are identical to using a browser) What else do I need to do to convince rails to leave the time value alone, since it *is* a UTC value? I can get the correct value by extracting the "Time" with no zone, and then recreating a new TimeWithZone. But that can''t be the best way! Loading development environment (Rails 2.3.2)>> Time.zone=> #<ActiveSupport::TimeZone:0xb795cbc8 @tzinfo=nil, @utc_offset=0, @name="UTC">>> event=Event.find(1)=> #<Event id: 1, edate: "2009-03-30 12:00:00">>> event.edate=> Mon, 30 Mar 2009 16:00:00 UTC +00:00>> zutc= ActiveSupport::TimeZone.new(''UTC'')=> #<ActiveSupport::TimeZone:0xb795cbc8 @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=0, @name="UTC">>> correct_time = ActiveSupport::TimeWithZone.new(event.edate.localtime,zutc)=> Mon, 30 Mar 2009 12:00:00 UTC +00:00 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Colin Law
2009-Mar-31 15:34 UTC
Re: TimeWithZone seems in rails 2.3 seems broken...is this the correct behavior?
I am no expert on this but Timezone works ok for me on Rails 2.2.2 so my comments below may help. Which version of Rails are you using? 2009/3/31 Rick Schumeyer <rschumeyer-EkmVulN54Sk@public.gmane.org>> > My environment.rb contains > config.time_zone = ''UTC''Where is this located in environent.rb. Mine is inside the do |config| .. end block. Also I have ''London'' rather than ''UTC'' though I doubt if this is the problem. Anything is worth trying though.> > > If my understanding is correct, rails should assume time values coming > from the database are UTC, and since config.time_zone is set to ''UTC'', > it should not try to convert the times. > > Instead, rails is assuming that the db values are Eastern (my local > zone) and is incorrectly adding four hours to convert to what it > thinks is UTC. Either my understanding of how this is supposed to > work is wrong, or something needs to be fixed. > > I created a test table in a SQL Server database with one row. The > table is called events and > contains an "edate" column. The value is "2009-03-30 12:00", and is a > UTC time.Are you sure SQL Server is not adjusting the value for you before it gets to Rails, thinking that you want it in local time? Have you tried picking the query up out of the rails log and entering that directly into SQL server to see what you get?> > > The output from script/console is below. (results are identical to > using a browser) > > What else do I need to do to convince rails to leave the time value > alone, since it *is* a UTC value? > > I can get the correct value by extracting the "Time" with no zone, and > then recreating a new TimeWithZone. But that can''t be the best way! > > Loading development environment (Rails 2.3.2) > >> Time.zone > > => #<ActiveSupport::TimeZone:0xb795cbc8 @tzinfo=nil, @utc_offset=0, > @name="UTC"> > >> event=Event.find(1) > > => #<Event id: 1, edate: "2009-03-30 12:00:00"> > >> event.edate > > => Mon, 30 Mar 2009 16:00:00 UTC +00:00The fact that this is showing the wrong time but the one above shows apparently the right one does suggest that Rails thinks that the zone is your local zone for some reason, as you have surmised.> >> zutc= ActiveSupport::TimeZone.new(''UTC'') > > => #<ActiveSupport::TimeZone:0xb795cbc8 > @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=0, @name="UTC"> > >> correct_time > ActiveSupport::TimeWithZone.new(event.edate.localtime,zutc) > > => Mon, 30 Mar 2009 12:00:00 UTC +00:00 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Geoff Buesing
2009-Apr-01 03:37 UTC
Re: TimeWithZone seems in rails 2.3 seems broken...is this the correct behavior?
Indeed, this isn''t the expected behavior. My best guess here is, maybe ActiveRecord::Base.default_timezone is being set to :local somewhere? If that were the case, then all times returned from the database would be shifted based on your system local UTC offset, and thus mess things up. If you run Event.default_timezone in the console, what does it return? On Mar 31, 9:08 am, Rick Schumeyer <rschume...-EkmVulN54Sk@public.gmane.org> wrote:> My environment.rb contains > config.time_zone = ''UTC'' > > If my understanding is correct, rails should assumetimevalues coming > from the database are UTC, and since config.time_zone is set to ''UTC'', > it should not try to convert the times. > > Instead, rails is assuming that the db values are Eastern (my localzone) and is incorrectly adding four hours to convert to what it > thinks is UTC. Either my understanding of how this is supposed to > work is wrong, or something needs to be fixed. > > I created a test table in a SQL Server database with one row. The > table is called events and > contains an "edate" column. The value is "2009-03-30 12:00", and is a > UTCtime. > > The output from script/console is below. (results are identical to > using a browser) > > What else do I need to do to convince rails to leave thetimevalue > alone, since it *is* a UTC value? > > I can get the correct value by extracting the "Time" with nozone, and > then recreating a new TimeWithZone. But that can''t be the best way! > > Loading development environment (Rails 2.3.2) > > >>Time.zone > > => #<ActiveSupport::TimeZone:0xb795cbc8 @tzinfo=nil, @utc_offset=0, > @name="UTC"> > > >> event=Event.find(1) > > => #<Event id: 1, edate: "2009-03-30 12:00:00"> > > >> event.edate > > => Mon, 30 Mar 2009 16:00:00 UTC +00:00 > > >> zutc= ActiveSupport::TimeZone.new(''UTC'') > > => #<ActiveSupport::TimeZone:0xb795cbc8 > @tzinfo=#<TZInfo::DataTimezone: Etc/UTC>, @utc_offset=0, @name="UTC"> > > >> correct_time = ActiveSupport::TimeWithZone.new(event.edate.localtime,zutc) > > => Mon, 30 Mar 2009 12:00:00 UTC +00:00--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---