Rails timezone differences between Time and DateTime

Posted by kjs3 on Stack Overflow See other posts from Stack Overflow or by kjs3
Published on 2012-11-12T04:24:28Z Indexed on 2012/11/12 5:00 UTC
Read the original article Hit count: 414

I have the timezone set. config.time_zone = 'Mountain Time (US & Canada)'

Creating a Christmas event from the console...

c = Event.new(:title => "Christmas")

using Time:

c.start = Time.new(2012,12,25)
=> 2012-12-25 00:00:00 -0700 #has correct offset
c.end = Time.new(2012,12,25).end_of_day
=> 2012-12-25 23:59:59 -0700 #same deal

using DateTime:

c.start = DateTime.new(2012,12,25)
=> Tue, 25 Dec 2012 00:00:00 +0000 # no offset
c.end = DateTime.new(2012,12,25).end_of_day
=> Tue, 25 Dec 2012 23:59:59 +0000 # same

I've carelessly been using DateTime thinking that input was assumed to be in the config.time_zone but there's no conversion when this gets saved to the db. It's stored just the same as the return values (formatted for the db).

Using Time is really no big deal but do I need to manually offset anytime I'm using DateTime and want it to be in the correct zone?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby