RyanScottLewis RyanScottLewis - 3 months ago 9
Ruby Question

Are the Date, Time, and DateTime classes necessary?

What is the purpose of having

Date
and
Time
classes when there is a
DateTime
class that can handle both?

Answer

DateTime is a subclass of Date, so whatever you can do with Date can be done with DateTime. But as tadman and steenslag point out, DateTime is slower. See steenslag's answer for how much slower it is.

With respect to DateTime vs, Time, I found something here.

Citation


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 ... while Date (and DateTime) have an 
almost infinite range but are terribly slow.

In short, DateTime is an all around superstar, and should be preferred in general, but if you want to optimize to the last bit, using Time can improve performance.