I''m trying to do some queries on the database of a Rails 2.3.2 app configured with config.time_zone = ''Eastern Time (US & Canada)''. I was under the impression that if I were to query the db for entries that fall within a time range, that the time would automatically be converted to UTC before executing the SQL, but that is not what I am seeing. For example:>> my_time = ''2009-04-03 21:00:00''.to_time(:local) >> Record.create(:time => my_time) >> Record.find_by_time(my_time)** Returns nothing ** Shouldn''t that work? When I look at the SQL, it is querying for a time of ''2009-04-03 21:00:00'' even though the time object was not UTC? Thanks, Tom --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you are using postgresql database, then the Solution is below :- ALTER DATABASE <database name> SET timezone TO "UTC"; -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If anyone else runs into this issue, it appears to be an inconsistency in the way Rails handles a time. ''2009-04-03 21:00:00:00''.to_time(:local) => Fri Apr 03 21:00:00 -0400 2009 ''2009-04-03 21:00:00:00''.to_time.in_time_zone => Fri, 03 Apr 2009 17:00:00 EDT -04:00 Notice with .in_time_zone, it actually put the GMT offset as well as a timezone identifier. If you run queries with the first time object, it will disregard the gmt offset and assume the time is local. If you run queries with the second time object, it will convert the time to UTC first. On Apr 3, 10:00 pm, TomRossi7 <t...-5bxIUPmzHicFraO2wh7vUA@public.gmane.org> wrote:> I''m trying to do some queries on the database of a Rails 2.3.2 app > configured with config.time_zone = ''Eastern Time (US & Canada)''. I > was under the impression that if I were to query the db for entries > that fall within a time range, that the time would automatically be > converted to UTC before executing the SQL, but that is not what I am > seeing. > > For example:>> my_time = ''2009-04-03 21:00:00''.to_time(:local) > >> Record.create(:time => my_time) > >> Record.find_by_time(my_time) > > ** Returns nothing ** > > Shouldn''t that work? When I look at the SQL, it is querying for a > time of ''2009-04-03 21:00:00'' even though the time object was not UTC? > > Thanks, > Tom--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Sorry, I should have used ''2009-04-03 21:00:00:00''.to_time (:local).in_time_zone. On Apr 8, 3:40 pm, tomrossi7 <t...-5bxIUPmzHicFraO2wh7vUA@public.gmane.org> wrote:> If anyone else runs into this issue, it appears to be an inconsistency > in the way Rails handles a time. > > ''2009-04-03 21:00:00:00''.to_time(:local) > => Fri Apr 03 21:00:00 -0400 2009 > > ''2009-04-03 21:00:00:00''.to_time.in_time_zone > => Fri, 03 Apr 2009 17:00:00 EDT -04:00 > > Notice with .in_time_zone, it actually put the GMT offset as well as a > timezone identifier. > > If you run queries with the first time object, it will disregard the > gmt offset and assume the time is local. If you run queries with the > second time object, it will convert the time to UTC first. > > On Apr 3, 10:00 pm, TomRossi7 <t...-5bxIUPmzHicFraO2wh7vUA@public.gmane.org> wrote: > > > I''m trying to do some queries on the database of a Rails 2.3.2 app > > configured with config.time_zone = ''Eastern Time (US & Canada)''. I > > was under the impression that if I were to query the db for entries > > that fall within a time range, that the time would automatically be > > converted to UTC before executing the SQL, but that is not what I am > > seeing. > > > For example:>> my_time = ''2009-04-03 21:00:00''.to_time(:local) > > >> Record.create(:time => my_time) > > >> Record.find_by_time(my_time) > > > ** Returns nothing ** > > > Shouldn''t that work? When I look at the SQL, it is querying for a > > time of ''2009-04-03 21:00:00'' even though the time object was not UTC? > > > Thanks, > > Tom--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---