Skip to main content

Strict timezone-safe datetime handling for Python

Project description

TimeUnifier

Eliminate naive datetime bugs in Python with strict, explicit timezone handling.

Python's built-in datetime is flexible but that flexibility allows subtle bugs that often go unnoticed until production.

from datetime import datetime

a = datetime.now()
b = datetime.utcnow()

print(a > b)  # unclear, depends on context
from time_unifier import now_utc

a = now_utc()
b = now_utc()

print(a > b)  # always valid

TimeUnifier removes ambiguity by enforcing correctness at the API level.

Quick Start

from time_unifier import now_utc

ut = now_utc()
zt = ut.to_zone("Europe/Stockholm")
ut2 = zt.to_utc()

Core Idea

  • All time must have a timezone
  • UTC is the internal standard
  • No mixing between UTC and zoned time
  • All conversions are explicit

If something is incorrect, it fails immediately.

Basic Usage

Create Time

from time_unifier import now_utc, now_zone

ut = now_utc()
zt = now_zone("Europe/Stockholm")

Convert Time

zt = ut.to_zone("Europe/Stockholm")
ut2 = zt.to_utc()

Compare Time

ut1 = now_utc()
ut2 = now_utc()

print(ut1 < ut2)

Duration

d = ut2 - ut1

print(d.hours())
print(d.minutes())
print(d.days())

JSON

json_value = ut.to_json()
ut_back = type(ut).from_json(json_value)

Zones

from time_unifier import is_valid_zone, list_zones

print(is_valid_zone("Europe/Stockholm"))
print(list_zones()[:5])

What Happens When You Do It Wrong

Naive datetime

from datetime import datetime
from time_unifier import UTCTime

UTCTime(datetime.now())  # invalid
from time_unifier import now_utc

ut = now_utc()  # valid

Invalid timezone

now_utc().to_zone("Invalid/Timezone")  # invalid
now_utc().to_zone("Europe/Stockholm")  # valid

Mixed types

from time_unifier import now_utc, now_zone

ut = now_utc()
zt = now_zone("Europe/Stockholm")

ut == zt  # invalid
ut == zt.to_utc()  # valid

Design

TimeUnifier is built on a simple rule:

Incorrect time handling should be impossible, not just discouraged.

  • Explicit over implicit
  • No hidden conversions
  • Fail fast on invalid usage
  • Minimal API with strong guarantees

Comparison

  • datetime: flexible but allows unsafe patterns
  • pendulum / arrow: convenience-focused

TimeUnifier prioritizes correctness and predictability.

Recommended Style

import time_unifier as tu

ut = tu.now_utc()
zt = ut.to_zone("Europe/Stockholm")

Guarantees

  • No naive datetimes
  • No implicit conversions
  • Clear and predictable errors
  • Strict separation between time types

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

time_unifier-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

time_unifier-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file time_unifier-0.1.0.tar.gz.

File metadata

  • Download URL: time_unifier-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for time_unifier-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7bd3ac808e04551a3648e8b73e3aabad4be69e8924d6ee0db9a5c138aafb4d36
MD5 ffc1ab5ed9699443ed8241cb7462ad8e
BLAKE2b-256 c04ce68ff5d30053be72566dcc8ebd47a69643a13e747c74e31f0e2dbd706855

See more details on using hashes here.

File details

Details for the file time_unifier-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: time_unifier-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for time_unifier-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3266ecacc27ad9d6056a5ec3728ba666897635fb38493093204bd1fb938fd43c
MD5 67c05e2c2937ac47938429ba6363ad29
BLAKE2b-256 fce9bc36586faeb0af7b5fb86931a8f3fe58df8a813cf4d74e235f9d1af84573

See more details on using hashes here.

Supported by

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