What is DateTime used for?

I've been using ruby for years, but I've never had a good use for DateTime. Since there's so much discussion about it recently, maybe I could ask again, why does it exist?

I always avoid using DateTime because:

1. It looks like a Time, but behaves like a Date:

     plus_one_sec = Time.now + 1
     plus_one_day = DateTime.now + 1

If it's really meant to be a Date plus fractional values for the time of day, then I think it should ignore time zones and utc offset completely because it's ambiguous. Date does not have a zone: only when you compare times should you compare zones.

2. It ignores tzinfo, so moving over daylight savings time doesn't change the offset:

     winter_time = Time.local(2014, 3, 9)
     summer_time = winter_time + (24 * 60 * 60)

     winter_time.zone
     #=> "PST"
     summer_time.zone
     #=> "PDT"

     winter_dt = winter_time.to_datetime
     summer_dt = winter_dt + 1

     winter_dt.zone
     #=> "-08:00"
     summer_dt.zone
     #=> "-08:00"

Here, +1 adds 24 hours to the local time, when the actual difference should be 23 (with the offset change).

3. Its "zone" has no useful value.

     dt = DateTime.new(2012,12,6, 1, 0, 0, "-07:00")
     dt.zone # => "-07:00"
     dt.utc? # => NoMethodError: undefined method `utc?'
     dt.dst? # => NoMethodError: undefined method `dst?'
     dt.utc_offset # => NoMethodError: undefined method `utc_offset'

I'm not sure what to think of the "%s %z" patch for DateTime.strptime since I never use it. It makes sense that it should be consistent with Time, but maybe DateTime itself is wrong-headed to begin with?

Andrew Vit

It's like a slower version of Time, written in pure Ruby, for people who
hate speed

···

On Sun, May 4, 2014 at 2:03 PM, Andrew Vit <andrew@avit.ca> wrote:

I've been using ruby for years, but I've never had a good use for
DateTime. Since there's so much discussion about it recently, maybe I could
ask again, why does it exist?

I always avoid using DateTime because:

1. It looks like a Time, but behaves like a Date:

    plus_one_sec = Time.now + 1
    plus_one_day = DateTime.now + 1

If it's really meant to be a Date plus fractional values for the time of
day, then I think it should ignore time zones and utc offset completely
because it's ambiguous. Date does not have a zone: only when you compare
times should you compare zones.

2. It ignores tzinfo, so moving over daylight savings time doesn't change
the offset:

    winter_time = Time.local(2014, 3, 9)
    summer_time = winter_time + (24 * 60 * 60)

    winter_time.zone
    #=> "PST"
    summer_time.zone
    #=> "PDT"

    winter_dt = winter_time.to_datetime
    summer_dt = winter_dt + 1

    winter_dt.zone
    #=> "-08:00"
    summer_dt.zone
    #=> "-08:00"

Here, +1 adds 24 hours to the local time, when the actual difference
should be 23 (with the offset change).

3. Its "zone" has no useful value.

    dt = DateTime.new(2012,12,6, 1, 0, 0, "-07:00")
    dt.zone # => "-07:00"
    dt.utc? # => NoMethodError: undefined method `utc?'
    dt.dst? # => NoMethodError: undefined method `dst?'
    dt.utc_offset # => NoMethodError: undefined method `utc_offset'

I'm not sure what to think of the "%s %z" patch for DateTime.strptime
since I never use it. It makes sense that it should be consistent with
Time, but maybe DateTime itself is wrong-headed to begin with?

Andrew Vit

--
Tony Arcieri

Pure Ruby?

ext/date/date_core.c
ext/date/date_parse.c
ext/date/date_strftime.c
ext/date/date_strptime.c
ext/date/date_tmx.h
ext/date/depend
ext/date/extconf.rb
ext/date/lib/date.rb
ext/date/lib/date/format.rb

It seems to me there's more Ruby code in lib/time.rb than in
ext/date/lib/date.rb.

That said, I do not understand why have both Time and DateTime.

···

On Sun, May 4, 2014 at 4:24 PM, Tony Arcieri <bascule@gmail.com> wrote:

It's like a slower version of Time, written in pure Ruby, for people who
hate speed

--
Felipe Contreras