Javier Valencia
Javier Valencia

Reputation: 695

Unexpected change in timezone (Ruby/Rails)

I started rails console and I wrote:

irb(main):001:0> fd=Time.now
=> 2014-03-14 13:58:35 +0100
irb(main):002:0> td=fd.next_month
=> 2014-04-14 13:58:35 +0200

Why the change in timezone? I don't get it.

Upvotes: 0

Views: 69

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74596

I don't know what your timezone is, so I can only assume that in one month's time you have entered Daylight Savings Time. You can check like this:

irb(main):039:0> t = Time.new
=> 2014-03-14 13:19:34 +0000
irb(main):040:0> t.dst?
=> false
irb(main):041:0> t += 30 * 24 * 3600
=> 2014-04-13 14:19:34 +0100
irb(main):042:0> t.dst?
=> true

I couldn't use the Time.next_month method (possibly because I'm using irb, not the rails console). In order to use it, I had to use a DateTime object instead:

irb(main):062:0> dt = DateTime.now
=> #<DateTime: 2014-03-14T13:28:08+00:00 ((2456731j,48488s,325503993n),+0s,2299161j)>
irb(main):063:0> dt.to_time.dst?
=> false
irb(main):064:0> dt.next_month.to_time.dst?
=> true

Upvotes: 3

Related Questions