Skip to main content

Modern datetime library for Python, written in Rust

Project description

⏰ Whenever

Typed and DST-safe datetimes for Python, written in Rust*

Do you cross your fingers every time you work with Python's datetime—hoping that you didn't mix naive and aware? or that you avoided its other pitfalls? or that you properly accounted for Daylight Saving Time (DST)? There’s no way to be sure...

✨ Until now! ✨

Whenever helps you write correct and type checked datetime code. Mistakes become red squiggles in your IDE, instead of bugs in production. It's also way faster than other third-party libraries—and usually the standard library as well.

Shows a bar chart with benchmark results.

RFC3339-parse, normalize, compare to now, shift, and change timezone (1M times)

⚠️ Note: Whenever is in pre-1.0 beta. The API may change as we gather feedback and improve the library. Leave a ⭐️ on github if you'd like to see how this project develops!

*Skeptical of Rust? Don't worry, it's available in pure Python too!

Why not the standard library?

Over 20+ years, Python's datetime has grown out of step with what you'd expect from a modern datetime library. Two points stand out:

  1. It doesn't always account for Daylight Saving Time (DST). Here is a simple example:

    bedtime = datetime(2023, 3, 25, 22, tzinfo=ZoneInfo("Europe/Paris"))
    full_rest = bedtime + timedelta(hours=8)
    # It returns 6am, but should be 7am—because we skipped an hour due to DST!
    

    Note this isn't a bug, but a design decision that DST is only considered when calculations involve two timezones. If you think this is surprising, you are not alone.

  2. Typing can't distinguish between naive and aware datetimes. Your code probably only works with one or the other, but there's no way to enforce this in the type system!

    # Does this expect naive or aware? Can't tell!
    def schedule_meeting(at: datetime) -> None: ...
    

Why not other libraries?

There are two other popular third-party libraries, but they don't (fully) address these issues. Here's how they compare to whenever and the standard library:

Whenever datetime Arrow Pendulum
DST-safe ⚠️
Typed aware/naive
Fast

Arrow is probably the most historically popular 3rd party datetime library. It attempts to provide a more "friendly" API than the standard library, but doesn't address the core issues: it keeps the same footguns, and its decision to reduce the number of types to just one (arrow.Arrow) means that it's even harder for typecheckers to catch mistakes.

Pendulum arrived on the scene in 2016, promising better DST-handling, as well as improved performance. However, it only fixes some DST-related pitfalls, and its performance has significantly degraded over time. Additionally, it hasn't been actively maintained since a breaking 3.0 release last year.

Why use whenever?

  • 🌐 DST-safe arithmetic
  • 🛡️ Typesafe API prevents common bugs
  • ✅ Fixes issues arrow/pendulum don't
  • ⚖️ Based on proven and familiar concepts
  • ⚡️ Unmatched performance
  • 💎 Thoroughly tested and documented
  • 📆 Support for date arithmetic
  • ⏱️ Nanosecond precision
  • 🦀 Rust!—but with a pure-Python option
  • 🚀 Support for the latest GIL-related improvements (experimental)

Quickstart

>>> from whenever import (
...    # Explicit types for different use cases
...    Instant,
...    ZonedDateTime,
...    LocalDateTime,
... )

# Identify moments in time, without timezone/calendar complexity
>>> now = Instant.now()
Instant(2024-07-04 10:36:56Z)

# Simple, explicit conversions
>>> now.to_tz("Europe/Paris")
ZonedDateTime(2024-07-04 12:36:56+02:00[Europe/Paris])

# A 'naive' local time can't accidentally mix with other types.
# You need to explicitly convert it and handle ambiguity.
>>> party_invite = LocalDateTime(2023, 10, 28, hour=22)
>>> party_invite.add(hours=6)
Traceback (most recent call last):
  ImplicitlyIgnoringDST: Adjusting a local datetime implicitly ignores DST [...]
>>> party_starts = party_invite.assume_tz("Europe/Amsterdam", disambiguate="earlier")
ZonedDateTime(2023-10-28 22:00:00+02:00[Europe/Amsterdam])

# DST-safe arithmetic
>>> party_starts.add(hours=6)
ZonedDateTime(2022-10-29 03:00:00+01:00[Europe/Amsterdam])

# Comparison and equality
>>> now > party_starts
True

# Formatting & parsing common formats (ISO8601, RFC3339, RFC2822)
>>> now.format_rfc2822()
"Thu, 04 Jul 2024 10:36:56 GMT"

# If you must: you can convert to/from the standard lib
>>> now.py_datetime()
datetime.datetime(2024, 7, 4, 10, 36, 56, tzinfo=datetime.timezone.utc)

Read more in the feature overview or API reference.

Roadmap

  • 🧪 0.x: get to feature-parity, process feedback, and tweak the API:

    • ✅ Datetime classes
    • ✅ Deltas
    • ✅ Date and time of day (separate from datetime)
    • ✅ Implement Rust extension for performance
    • 🚧 Parsing leap seconds
    • 🚧 Improved parsing and formatting
    • 🚧 More helpful error messages
    • 🚧 Intervals
  • 🔒 1.0: API stability and backwards compatibility

Limitations

  • Supports the proleptic Gregorian calendar between 1 and 9999 AD
  • Timezone offsets are limited to whole seconds (consistent with IANA TZ DB)
  • No support for leap seconds (consistent with industry standards and other modern libraries)

Versioning and compatibility policy

Whenever follows semantic versioning. Until the 1.0 version, the API may change with minor releases. Breaking changes will be meticulously explained in the changelog. Since the API is fully typed, your typechecker and/or IDE will help you adjust to any API changes.

⚠️ Note: until 1.x, pickled objects may not be unpicklable across versions. After 1.0, backwards compatibility of pickles will be maintained as much as possible.

License

Whenever is licensed under the MIT License. The binary wheels contain Rust dependencies which are licensed under similarly permissive licenses (MIT, Apache-2.0, and others). For more details, see the licenses included in the distribution.

Acknowledgements

This project is inspired by the following projects. Check them out!

The benchmark comparison graph is based on the one from the Ruff project.

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

whenever-0.6.6.tar.gz (148.2 kB view hashes)

Uploaded Source

Built Distributions

whenever-0.6.6-cp312-none-win_amd64.whl (236.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

whenever-0.6.6-cp312-none-win32.whl (266.3 kB view hashes)

Uploaded CPython 3.12 Windows x86

whenever-0.6.6-cp312-cp312-musllinux_1_2_x86_64.whl (543.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

whenever-0.6.6-cp312-cp312-musllinux_1_2_i686.whl (601.4 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ i686

whenever-0.6.6-cp312-cp312-musllinux_1_2_armv7l.whl (688.9 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARMv7l

whenever-0.6.6-cp312-cp312-musllinux_1_2_aarch64.whl (555.0 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.2+ ARM64

whenever-0.6.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (372.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

whenever-0.6.6-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (450.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

whenever-0.6.6-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (411.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

whenever-0.6.6-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (427.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

whenever-0.6.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (375.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

whenever-0.6.6-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl (430.2 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.5+ i686

whenever-0.6.6-cp312-cp312-macosx_11_0_arm64.whl (293.2 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

whenever-0.6.6-cp312-cp312-macosx_10_12_x86_64.whl (304.9 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

whenever-0.6.6-cp311-none-win_amd64.whl (235.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

whenever-0.6.6-cp311-none-win32.whl (265.2 kB view hashes)

Uploaded CPython 3.11 Windows x86

whenever-0.6.6-cp311-cp311-musllinux_1_2_x86_64.whl (542.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

whenever-0.6.6-cp311-cp311-musllinux_1_2_i686.whl (600.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ i686

whenever-0.6.6-cp311-cp311-musllinux_1_2_armv7l.whl (688.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARMv7l

whenever-0.6.6-cp311-cp311-musllinux_1_2_aarch64.whl (554.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.2+ ARM64

whenever-0.6.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (371.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

whenever-0.6.6-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (449.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

whenever-0.6.6-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (410.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

whenever-0.6.6-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (426.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

whenever-0.6.6-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (374.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

whenever-0.6.6-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (428.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

whenever-0.6.6-cp311-cp311-macosx_11_0_arm64.whl (292.2 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

whenever-0.6.6-cp311-cp311-macosx_10_12_x86_64.whl (303.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

whenever-0.6.6-cp310-none-win_amd64.whl (235.0 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

whenever-0.6.6-cp310-none-win32.whl (265.2 kB view hashes)

Uploaded CPython 3.10 Windows x86

whenever-0.6.6-cp310-cp310-musllinux_1_2_x86_64.whl (542.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

whenever-0.6.6-cp310-cp310-musllinux_1_2_i686.whl (600.2 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ i686

whenever-0.6.6-cp310-cp310-musllinux_1_2_armv7l.whl (688.1 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARMv7l

whenever-0.6.6-cp310-cp310-musllinux_1_2_aarch64.whl (554.1 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.2+ ARM64

whenever-0.6.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (371.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

whenever-0.6.6-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (449.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

whenever-0.6.6-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (410.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

whenever-0.6.6-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (426.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARMv7l

whenever-0.6.6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (374.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

whenever-0.6.6-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (428.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

whenever-0.6.6-cp310-cp310-macosx_11_0_arm64.whl (292.2 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

whenever-0.6.6-cp310-cp310-macosx_10_12_x86_64.whl (303.6 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

whenever-0.6.6-cp39-none-win_amd64.whl (236.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

whenever-0.6.6-cp39-none-win32.whl (265.7 kB view hashes)

Uploaded CPython 3.9 Windows x86

whenever-0.6.6-cp39-cp39-musllinux_1_2_x86_64.whl (543.0 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

whenever-0.6.6-cp39-cp39-musllinux_1_2_i686.whl (600.7 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ i686

whenever-0.6.6-cp39-cp39-musllinux_1_2_armv7l.whl (688.6 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARMv7l

whenever-0.6.6-cp39-cp39-musllinux_1_2_aarch64.whl (554.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.2+ ARM64

whenever-0.6.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (372.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

whenever-0.6.6-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (450.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

whenever-0.6.6-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (411.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

whenever-0.6.6-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (426.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARMv7l

whenever-0.6.6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (374.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

whenever-0.6.6-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (428.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

whenever-0.6.6-cp39-cp39-macosx_11_0_arm64.whl (292.6 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

whenever-0.6.6-cp39-cp39-macosx_10_12_x86_64.whl (304.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

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