Skip to content

Instantly share code, notes, and snippets.

@weidenfreak
Created October 31, 2012 14:23
Show Gist options
  • Select an option

  • Save weidenfreak/3987304 to your computer and use it in GitHub Desktop.

Select an option

Save weidenfreak/3987304 to your computer and use it in GitHub Desktop.

Revisions

  1. weidenfreak revised this gist Oct 31, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    >> Time.new(2012, 9, 10)
    => 2012-09-10 00:00:00 +0200

    # calculations across time zones are also wrong with DateTime (as one might think...)
    # calculations across time zones (summer/winter) are also wrong with DateTime (as one might think...)

    # DateTime with offset + 01:00
    >> DateTime.now
  2. weidenfreak created this gist Oct 31, 2012.
    40 changes: 40 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    $ 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 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