The DateTime class seems redundant, and after reading this bit in the documentation for the Rails extension of the class, it also seems potentially dangerous:
DateTimes aren’t aware of DST rules, so use a consistent non-DST offset when creating a DateTime with an offset in the local zone
There’s also this, in the Rails documentation for DateTime#to_time:
Attempts to convert self to a Ruby Time object; returns self if out of range of Ruby Time class. If self has an offset other than 0, self will just be returned unaltered, since there’s no clean way to map it to a Time.
I assume that last part is due to DateTime not recognizing DST.
It seems to me we have this:
- The
Dateclass represents a simple date without a time. - The
Timeclass represents a specific point in time, which implicitly includes the date. - The
DateTimeclass is the same as theTimeclass, but doesn’t understand DST and sometimes can’t convert to a regularTimeclass.
So should DateTime just be banished from the code base or does it serve a useful purpose which I am missing?
DateTimehad an advantage overTimeon 32 bit machines in Rubies < 1.9.2 –Timewas a victim of the Y2K38 problem and limited to a 32 bit range. This problem is solved either on 64 bit machines and/or in recent Ruby versions. You still may need to useDateTimeif for example 1.8 compatibility is a must or you rely on using methods from its API which often deviates from that ofTime.