7.19. Interconverting Between Time, Date, and DateTime

Ruby has three basic classes dealing with dates and times: Time, Date, and DateTime. The following is a description of each:

  • The Time class is mostly a wrapper for the underlying time functions in the C library. These are typically based on the UNIX epoch and thus cannot represent times before 1970.

  • The Date class was created to address this shortcoming of the Time class. It can easily deal with older dates such as Leonardo da Vinci’s birthday (April 15, 1452), and it is intelligent about the dates of calendar reform. But it has its own shortcoming; it can’t deal with the time of day that Leonardo was born. It deals strictly with dates.

  • The DateTime class inherits from Date and tries to be ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.