Skip to main content

Like datetime.timedelta, for date arithmetic.

Project description

datedelta.datedelta is datetime.timedelta for date arithmetic.

It can add years, months, or days to dates while accounting for oddities of the Gregorian calendar. It can also subtract years, months, or days from dates.

Typically, it’s useful to compute yearly or monthly subscriptions periods.

Behavior

There are two date arithmetic traps in the Gregorian calendar:

  1. Leap years. Problems arise when adding years to a February 29th gives a result in a non-leap year.

  2. Variable number of days in months. Problems arise when adding months to a 29th, 30th or 31th gives a result in a month where that day doesn’t exist.

In both cases, the result must be changed to the first day of the next month.

Provided periods are represented by [start date inclusive, end date exclusive), datedelta gives consistent results. (This representation of periods is akin to 0-based indexing, which is the convention Python uses.)

Operations are always performed on years, then months, then days. This order usually provides the expected behavior. It also minimizes loss of precision.

Limitations

Additions involving datedelta are neither associative not commutative in general. Here are two examples where adding a datedelta then subtracting it doesn’t return the original value:

>>> import datetime
>>> import datedelta

>>> datetime.date(2020, 2, 29) + datedelta.datedelta(years=1)
datetime.date(2021, 3, 1)

>>> datetime.date(2021, 3, 1) - datedelta.datedelta(years=1)
datetime.date(2020, 3, 1)

>>> datetime.date(2020, 1, 31) + datedelta.datedelta(months=1)
datetime.date(2020, 3, 1)

>>> datetime.date(2020, 3, 1) - datedelta.datedelta(months=1)
datetime.date(2020, 2, 1)

To avoid counter-intuitive results, datedelta only implements operations that have unambiguous semantics:

  • Adding a datedelta to a date

  • Subtracting a datedelta from a date

  • Adding a datedelta to a datedelta when components have the same sign

  • Subtracting a datedelta from a datedelta when components have opposite signs

(PEP 20 says: “In the face of ambiguity, refuse the temptation to guess.”)

Alternatives

datedelta.datedelta is smarter than datetime.timedelta because it knows about years and months in addition to days.

datedelta.datedelta provides a subset of the features found in dateutil.relativedelta. Not only does it only support dates, but:

  • It omits the “replace” behavior which is very error-prone.

  • It doesn’t allow explicit control of leapdays.

  • It uses keyword-only arguments.

  • It requires Python 3.

Handling leapdays automatically reduces the number of choices the programmer must make and thus the number of errors they can make.

If you’re stuck with Python 2, just copy the code, make datedelta inherit from object, and remove the * in the signature of __init__.

If you’re comfortable with dateutil and don’t mind its larger footprint, there’s little to gain by switching to datedelta.

Changelog

1.0

  • Initial stable release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datedelta-1.0.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

datedelta-1.0.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file datedelta-1.0.0.tar.gz.

File metadata

  • Download URL: datedelta-1.0.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for datedelta-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3832cc1a292c83f082f43c4502e54f94da58a4f5c31701f58660bc52b6244faa
MD5 0f741e333ab3f0e426e8c23b4ae71942
BLAKE2b-256 c326eb523c6109d6cddfc06d04b75ee4daa27bff4545fcbb2eb1c6dbc4531358

See more details on using hashes here.

File details

Details for the file datedelta-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for datedelta-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d167cdf9a72a9a6fcf1e2162f43666876b851b1422d493f1bafee91b0ca16274
MD5 925fe905eba72611a57c9fc61c3c31f3
BLAKE2b-256 f172bed1696397bb253c91033d0cbdc830bff8541a34b631d729caf63dd1b3a3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page