Maya: Datetimes for Humans
Maya is a Python library designed to make working with datetimes significantly easier and more intuitive. It wraps Python's built-in `datetime` module, providing a human-friendly API that simplifies common tasks such as parsing diverse date strings, handling timezones, and converting between various time representations. It aims to eliminate common pitfalls like distinguishing between naive and aware datetimes. The current version is 0.6.1, and while releases are infrequent, the project is actively maintained to address user issues.
Warnings
- gotcha When parsing imprecise dates (e.g., `maya.when('tomorrow')` or '2024-01-15'), Maya typically returns a `MayaDT` object representing 00:00:00 of that day. This may not align with a user's expectation of representing a full day or a time-of-day relative to the current moment.
- gotcha While Maya aims to simplify timezone handling by internally converting all times to UTC for consistent arithmetic, operations like adding a `timedelta` to a `MayaDT` object obtained with a specific `to_timezone` might not correctly account for Daylight Saving Time (DST) transitions in non-UTC timezones. This can lead to off-by-one-hour errors.
- gotcha Older versions of Maya (e.g., v0.4.1) had specific, limited compatibility with `pendulum` versions (e.g., `>= 1.0` and `<= 1.5.1`). While this might be resolved in 0.6.1, unexpected `pendulum` version incompatibilities can still occur during installation or runtime, leading to broken parsing functionality.
- gotcha Users have reported that `maya.parse()` may fail to correctly parse some valid ISO 8601 strings or encounter issues with 'certain dates', indicating potential edge cases in its parsing logic.
- gotcha Installing `maya` in a `python-slim` Docker container (or similar minimal environments) can fail due to dependencies (specifically `regex`) requiring a C compiler (`gcc`) or Python development headers (`python3.x-dev`) during the wheel building process.
Install
-
pip install maya
Imports
- maya
import maya
Quickstart
import maya
# Get the current time
now = maya.now()
print(f"Current time: {now}")
# Parse anything
date_obj = maya.parse("tomorrow")
print(f"'Tomorrow' parsed: {date_obj.slang_date()}, {date_obj.slang_time()}")
# Convert to a specific timezone
la_time = now.datetime(to_timezone="America/Los_Angeles")
print(f"Current time in LA: {la_time}")
# Generate time intervals
start = maya.when("2024-01-01")
end = maya.when("2024-01-03")
print("Days in interval:")
for day in maya.intervals(start=start, end=end, interval=60*60*24):
print(f"- {day.slang_date()}")