tags:

views:

211

answers:

4

I'm wondering if anyone knows of a good date and time library that has correctly-implemented features like the following:

  • Microsecond resolution
  • Daylight savings
    • Example: it knows that 2:30am did not exist in the US on 8 March 2009 for timezones that respect daylight savings.
    • I should be able to specify a timezone like "US/Eastern" and it should be smart enough to know whether a given timestamp should correspond to EST or EDT.
  • Custom date ranges
    • The ability to create specialized business calendars that skip over weekends and holidays.
  • Custom time ranges
    • The ability to define business hours so that times requested outside the business hours can be rounded up or down to the next or previous valid hour.
  • Arithmetic
    • Be able to add and subtract integer amounts of all units (years, months, weeks, days, hours, minutes, ...). Note that adding something like 0.5 days isn't well-defined because it could mean 12 hours or it could mean half the duration of a day (which isn't 24 hours on daylight savings changes).
  • Natural boundary alignment
    • Given a timestamp, I'd like be able to do things like round down to the nearest decade, year, month, week, ..., quarter hour, hour, etc.

I'm currently using Python, though I'm happy to have a solution in another language like perl, C, or C++.

I've found that the built-in Python libraries lack sophistication with their daylight savings logic and there isn't an obvious way (to me) to set up things like custom time ranges.

+3  A: 

Take a look at the dateutil and possibly mx.DateTime packages.

mhawke
+5  A: 

Python's standard library's datetime module is deliberately limited to non-controversial aspects that aren't changing all the time by legislative fiat -- that's why it deliberately excludes direct support for timezones, DST, fuzzy parsing and ill-defined arithmetic (such as "one month later"...) and the like. On top of it, dateutil for many kinds of manipulations, and pytz for timezones (including DST issues), add most of what you're asking for, though not extremely explosive things like "holidays" which vary so wildly not just across political jurisdictions but even across employers within a simgle jurisdiction (e.g. in the US some employers consider "Columbus Day" a holiday, but many don't -- and some, with offices in many locations, have it as a holiday on some locations but not in others; given this utter, total chaos, to expect to find a general-purpose library that somehow magically makes sense of the chaos is pretty weird).

Alex Martelli
+1: and if you really need, you could handle holiday calendars with dateutil module. Basically you need to configure rruleset, which will include rules for each holiday. then you can combine those rules with you base calendar etc. Of course you have to configure all by your self, but this can be done at least just once and then used in your library for all the company/project
van
+3  A: 

I should be able to specify a timezone like "US/Eastern" and it should be smart enough to know whether a given timestamp should correspond to EST or EDT.

This part isn't always possible - in the same way that 2:30am doesn't exist for one day of the year (in timezones with daylight saving that switches at 2:00am), 2:30am exists twice for another day - once in EDT and then an hour later in EST. If you pass that date/time to the library, how does it know which of the two times you're talking about?

caf
I'd like it to do something smart with 2:30am in each case. From what I've found in the pytz library, there isn't a public way to ask it if a timestamp is non-existent or ambiguous. As long as the library has a clear way of providing this information, I'm happy.
Mr Fooz
+1  A: 

Although the book is over ten years old, I would strongly recommend reading Standard C Date/Time Library: Programming the World's Calendars and Clocks by Lance Latham. This is one of those books that you will pick back up from time to time in amazement that it got written at all. The author goes into more detail than you want about calenders and time keeping systems, and along the way develops the source code to a library (written in C) to handle all of the calculations.

Amazingly, it seems to be still in print...

RBerteig