Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 7. Working with Times and Dates > Interconverting Between Time, Date, a...

Feedback: 0 Comments on this Section

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 the best of both worlds. It can represent dates as well as Date can, and times as well as Time can. This is often the “right” way to represent a date-time value.

    But don’t be fooled into thinking that a DateTime is just a Date with an embedded Time. There are, in fact, several methods missing from DateTime, such as usec, dst?, and others.

You are currently reading a free preview of this book.


Sign up for a
Safari Library subscription
to access Rough Cuts.


Subscribe Now