Full-featured date and time library

I am wondering if anyone knows of a good time and time library that has properly implemented functions such as:

  • Microsecond resolution
  • Summer time
    • Example: he knows that 2:30 AM was not in the USA on March 8, 2009 for time zones that include daylight saving time.
    • I must be able to specify a time zone, such as "US / Eastern," and it should be smart enough to know if a given timestamp should match EST or EDT.
  • Custom Date Ranges
    • Ability to create specialized business calendars that skip weekends and holidays.
  • Custom time ranges
    • The ability to define business hours so that time requested outside of business hours can be rounded up or down to the next or previous valid hour.
  • Arithmetic
    • To be able to add and subtract integer amounts of all units (years, months, weeks, days, hours, minutes, ...). Please note that adding something like 0.5 days is not defined, because it can mean 12 hours, or it can mean half the length of the day (which is not 24 hours when changing the daily savings).
  • Natural Boundary Orientation
    • Given the timestamp, I would like to be able to do things like rounding to the next decade, year, month, week, ..., quarter hour, hour, etc.

I am currently using Python, although I am happy to have a solution in another language, such as perl, C or C ++.

I found that the Python built-in libraries are not having enough difficulty with their daylight logic, and there is no explicit way (for me) to set things up like user time ranges.

+7
c python datetime
source share
6 answers

The Python datetime standard library datetime deliberately limited by consistent aspects that do not change all the time in the legislative document - why does it deliberately exclude direct support for time zones, DST, fuzzy parsing and poorly defined arithmetic (for example, "in a month" ...) and the like. In addition, dateutil for many kinds of manipulations and pytz for time zones (including DST questions), add most of what you ask for, although not very explosive things such as β€œholidays” that change so wildly not only in political jurisdictions , but even between employers within a singular jurisdiction (for example, in the USA, some employers consider Columbus Day a holiday, but many do not, and some, with offices in many places, have it as a holiday in some places, but not in others given this complete chaos , expecting to find a universal library that somehow magically makes sense of chaos is rather strange).

+11
source share

Take a look at dateutil and maybe mx.DateTime .

+6
source share

I saw it the other day, I myself did not use it ... it looks promising. http://crsmithdev.com/arrow/

+6
source share

I must be able to specify the time zone as "US / East", and it should be smart enough to know if a given timestamp should match EST or EDT.

This part is not always possible - just as 2:30 in the morning does not exist for one day of the year (in time zones with summer savings, which switches at 2 in the morning), 2:30 in the morning exists twice for another day - once to EDT, and then an hour later to EST. If you pass this date / time to the library, how do you know which of the two times you're talking about?

+4
source share

Although the book is over ten years old, I highly recommend reading Standard C Date / Time: Programming Lance Latham's world calendars and clocks . This is one of those books that you will pick up from time to time, surprised that it is written at all. The author goes in more detail than you want about calendars and time tracking systems, and along the way he develops the source code into a library (written in C) to handle all the calculations.

Surprisingly, it is still printed ...

+2
source share

I just released a Python library called Fleming ( https://github.com/ambitioninc/fleming ), and it seems to solve two of your sophistication issues regarding Daylight Saving.

Problem 1, Arithmetic. Fleming has an add_timedelta function that takes timedelta (from the Python date and time module) or relativedelta from python-dateutil and adds it to the datetime object. The add_timedelta function handles the case when a datetime object crosses a DST border. Check out https://github.com/ambitioninc/fleming#add_timedelta for a full explanation and examples. Here is a quick example:

 import fleming import datetime import timedelta dt = fleming.add_timedelta(dt, datetime.timedelta(weeks=2, days=1)) print dt 2013-03-29 20:00:00-04:00 # Do timedelta arithmetic such that it starts in DST and crosses over into no DST. # Note that the hours stay in tact and the timezone changes dt = fleming.add_timedelta(dt, datetime.timedelta(weeks=-4)) print dt 2013-03-01 20:00:00-05:00 

Problem 2, Natural Boundary Alignment. Fleming has a gender function that can take arbitrary alignment. Let's say your time was time (2013, 2, 3), and you gave it an interval of half a month = 3. This means that it will be rounded to the nearest trimont (quarter). Similarly, you can specify the next decade using the arguments year = 10. Check ( https://github.com/ambitioninc/fleming#floordt-within_tznone-yearnone-monthnone-weeknone-daynone-hournone-minutenone-secondnone-microsecondnone ) for complete examples and illustrations. Here is a quick one:

 import fleming import datetime # Get the starting of a quarter by using month=3 print fleming.floor(datetime.datetime(2013, 2, 4), month=3) 2013-01-01 00:00:00 
+1
source share

All Articles