Ruby and Date classes

By reeset / On / In ruby

I’d seen this somewhere before, but thought I’d benchmark it to see if it was true.  Ruby has 3 classes for doing dates.  There is Date, DateTime and Time classes.  In languages like C# and PHP, the equivalent DateTime class is the one that is generally utilized to do date/time manipulations.  However, in Ruby, DateTime and Time are nearly identical classes, the difference being that DateTime is a convenience class.  Benchmarking shows the DateTime class to be 20-25 times slower than the native Time class.  After doing a little research, the reason is clear.  The Time class is a native class developed in C, the DateTime is a wrapper class written in Ruby.  So while I rarely have occasion to do time manipulation in a loop — it would seem that the DateTime class should pretty much always be avoided.  In fact, I can’t see why the DateTime class exists.  I don’t see what it does that isn’t available in either the Date class or Time class. 



2 thoughts on “Ruby and Date classes

  1. It seems, that Time can only deal with dates starting from 1970, while Date and DateTime, although a lot more complex, can deal with practically any date.

    As an example, if you want to calculate the age of someone born in 1969 or before, you may have to use Date or DateTime.