Skip to content

Instantly share code, notes, and snippets.

@weidenfreak
Created October 31, 2012 14:23
Show Gist options
  • Save weidenfreak/3987304 to your computer and use it in GitHub Desktop.
Save weidenfreak/3987304 to your computer and use it in GitHub Desktop.
DateTime has wrong offset
$ irb
>> require 'date'
=> true
# DateTime ignores time zone
>> DateTime.new(2012,9,10)
=> #<DateTime: 2012-09-10T00:00:00+00:00 ((2456181j,0s,0n),+0s,2299161j)>
# Time handles offset correctly
>> Time.new(2012, 9, 10)
=> 2012-09-10 00:00:00 +0200
# calculations across time zones (summer/winter) are also wrong with DateTime (as one might think...)
# DateTime with offset + 01:00
>> DateTime.now
=> #<DateTime: 2012-10-29T15:07:10+01:00 ((2456230j,50830s,490918000n),+3600s,2299161j)>
# and 10 days ago still with + 01:00 offset
>> DateTime.now - 10
=> #<DateTime: 2012-10-19T15:07:20+01:00 ((2456220j,50840s,290852000n),+3600s,2299161j)>
>> Time.now
=> 2012-10-29 15:07:34 +0100
>> Time.now - 864000
=> 2012-10-19 16:08:23 +0200
# it's also wrong in Rails
$ rails c
Loading development environment (Rails 3.2.8)
>> DateTime.now
=> Mon, 29 Oct 2012 16:55:07 +0100
>> DateTime.now - 10.days
=> Fri, 19 Oct 2012 16:55:13 +0100
>> Time.now
=> 2012-10-29 16:55:16 +0100
>> Time.now - 10.days
=> 2012-10-19 16:55:21 +0200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment