Tempora
Tempora is a Python library by `jaraco` providing a collection of objects and routines pertaining to date and time. It offers utilities and constants in its top-level package, routines for measuring and profiling in the `timing` module, an event scheduler in `schedule`, and functionalities for handling datetime-aware UTC values in `utc`. The library is currently at version 5.8.1 and maintains a regular release cadence with frequent updates.
Warnings
- gotcha When using testing libraries like `freezegun`, `tempora.utc.now()` might not reflect the 'frozen' time. This is because `freezegun` patches `datetime.datetime`, and if `tempora.utc.now()` or other `tempora` functions obtain a handle to `datetime.datetime` before `freezegun`'s context is entered, they will use the original, unfrozen time.
- breaking The library has moved away from `pytz` for timezone handling in favor of `zoneinfo` (standard library module from Python 3.9+). While `tempora` has ported this functionality, direct reliance on `pytz`-specific behaviors or imports like `pytz.UTC` might lead to incorrect results or deprecation warnings in other contexts. Always prefer `datetime.timezone.utc` or `zoneinfo.ZoneInfo` for modern Python development with time zones.
- gotcha The `tempora.schedule.Scheduler` currently does not automatically run its event loop. To execute scheduled tasks, you need to explicitly run the scheduler's event loop, typically in a dedicated thread or by integrating it with an existing event loop (e.g., `asyncio`). The provided quickstart only sets up the schedule but does not run it.
Install
-
pip install tempora
Imports
- timing
from tempora import timing
- schedule
from tempora import schedule
- utc
from tempora import utc
- timing.Stopwatch
from tempora.timing import Stopwatch
- schedule.Scheduler
from tempora.schedule import Scheduler
- utc.now
from tempora.utc import now
Quickstart
import datetime
from tempora.timing import Stopwatch
from tempora.schedule import Scheduler
from tempora.utc import now
# Example using Stopwatch
with Stopwatch() as sw:
sum(range(10000))
print(f"Summing range took: {sw.elapsed} seconds")
# Example using utc.now()
utc_time = now()
print(f"Current UTC time: {utc_time}")
# Example using Scheduler (basic usage - requires a running event loop)
def my_task():
print(f"Task executed at {now()}")
scheduler = Scheduler()
scheduler.add_event(my_task, datetime.timedelta(seconds=1))
print("Scheduler created to run a task after 1 second. (This example doesn't run the event loop).")