Forum: Ruby What is DateTime used for?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F0987c97234fa9c6b26f796bdbdab037?d=identicon&s=25 Andrew Vit (Guest)
on 2014-05-04 23:04
(Received via mailing list)
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 = + 1
     plus_one_day = + 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)
     #=> "PST"
     #=> "PDT"

     winter_dt = winter_time.to_datetime
     summer_dt = winter_dt + 1
     #=> "-08:00"
     #=> "-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 =,12,6, 1, 0, 0, "-07:00") # => "-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
4131d2f57a0db2a2b4d9a62bd389fd44?d=identicon&s=25 Tony Arcieri (Guest)
on 2014-05-04 23:26
(Received via mailing list)
It's like a slower version of Time, written in pure Ruby, for people who
hate speed
1bb1c1a41b92d9f18e2890c3f6d33970?d=identicon&s=25 Felipe Contreras (Guest)
on 2014-05-05 00:04
(Received via mailing list)
On Sun, May 4, 2014 at 4:24 PM, Tony Arcieri <> wrote:
> It's like a slower version of Time, written in pure Ruby, for people who
> hate speed

Pure Ruby?


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

That said, I do not understand why have both Time and DateTime.
B5c20f697f141bfc00b1b57e581101c9?d=identicon&s=25 Benjamin F. (benjamin_f)
on 2014-05-05 04:51
Andrew Vit wrote in post #1144945:
> , why does it exist?

I agree with the sentiment of always avoiding it.
re: pure ruby, it was

it isn't anymore

On Rogues Parley you once linked to
which linked to

Stefan Rusterholz (apeiros) on 2008-03-16 02:05
> Time is a wrapper around Unix-Epoch.
> Date (and DateTime) use rational and a "day zero" for storage. So Time
is faster but the upper and lower bounds are tied to epoch time (which
for 32bit epoch times is something around 1970-2040 - not even enough to
represent the birthday of my parents) while Date (and DateTime) have an
almost infinite range  but are *terribly* slow

The pickaxe Ruby says:

> The date library implements classes Date and DateTime, which provide a
comprehensive set of facilities for storing, manipulating, and
converting dates
with or without time components. The classes can represent and
manipulate civil,
ordinal, commercial, Julian, and standard dates, starting January 1,
4713 BCE. The
DateTime class extends Date with hours, minutes, seconds, and fractional
and it provides some support for time zones. The classes also provide
support for
parsing and formatting date and datetime strings.

It appears this date/datetime code was written by Tadayoshi Funaba
1998-present and documented by William Webber

I didn't see any benchmarks in trunk about speed increases, but I didn't
look very carefully.

This topic is locked and can not be replied to.