10.1. Does Anybody Really Care About Time?

Working with date and time information is frankly among the most frustrating parts of nearly every software project I've done. At some point, it seems like I always need some date or time arithmetic in my program and I always confront the same problems.

  • Data problems: How does the language represent a duration, as opposed to a calendar date? How is a range of dates represented?

  • Ambiguities and complications: What does it really mean to add a month to a date? What happens to date calculations as you cross time zones? Or, even more fun, when you cross the daylight savings time boundary? At some point, what seems like a simple date calculation becomes a mind-bending exercise in juggling time zones.

  • Toolkit limitations: Nearly every programming language library that deals with dates and times has some kind of quirk or frustration. (I'm looking at you, Java Calendar class ... but Ruby is no exception here, so don't think I'm letting it off the hook easily.)

In this section: your guide to the jungle that is international time.

10.1.1. Dates and Times

The confusion starts immediately. Ruby has two separate implementations of date and time functionality, each of which has its own distinct strengths and weaknesses. In one corner is the Time class, which is a wrapper around the same Unix/C-language time libraries that every other scripting language has. As you'd expect from a library that has its origins in the early days of Unix, the programmer ...

Get Professional Ruby on Rails™ 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.