Full-featured date and time library

2024/10/3 0:30:59

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.

Answer

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).

https://en.xdnf.cn/q/70788.html

Related Q&A

Mean of a correlation matrix - pandas data fram

I have a large correlation matrix in a pandas python DataFrame: df (342, 342).How do I take the mean, sd, etc. of all of the numbers in the upper triangle not including the 1s along the diagonal?Thank…

How to set imshow scale

Im fed up with matplotlib in that its so hard to plot images in specified size.Ive two images in 32*32, 20*20 sizes. I just want to plot them in its original size, or in proportion to its original size…

Python distutils gcc path

Im trying to cross-compile the pycrypto package, and Im getting closer and closer however, Ive hit an issue I just cant figure out.I want distutils to use the cross-compile specific gcc- so I set the C…

TypeError: builtin_function_or_method object has no attribute __getitem__

Ive got simple python functions.def readMainTemplate(templateFile):template = open(templateFile, r)data = template.read()index1 = data.index[[] #originally I passed it into data[]index2 = data.index[]]…

Extract currency amount from string in Python

Im making a program that takes currency from a string and converts it in to other currencies. For example, if the string was the car cost me $13,250 I would need to get $ and 13250. I have this regex a…

Error: The elasticsearch backend requires the installation of requests. How do I fix it?

Im having a issue when I ran "python manage.py rebuild_index" in my app supported by haystack and elasticsearch.Python 2.7 Django version 1.6.2 Haystack 2.1.0 Elasticsearch 1.0Please see the …

numpy: applying argsort to an array

The argsort() function returns a matrix of indices that can be used to index the original array so that the result would match the sort() result.Is there a way to apply those indices? I have two array…

Jinja2 for word templating

I would like to use jinja2 for word templating like mentioned is this short article. The problem Im facing is as follows, if I put {{title}} in my word-file the resulting xml can look like this:<w:r…

API capture all paginated data? (python)

Im using the requests package to hit an API (greenhouse.io). The API is paginated so I need to loop through the pages to get all the data I want. Using something like:results = [] for i in range(1,326+…

How to convert latitude longitude to decimal in python?

Assuming I have the following:latitude = "20-55-70.010N" longitude = "32-11-50.000W"What is the easiest way to convert to decimal form? Is there some library?Would converting from…