RFC 3339 Date/Time Utilities
The `tonyg-rfc3339` library provides a Python implementation for parsing and formatting dates and times according to RFC 3339. It includes `tzinfo` classes for UTC and fixed-offset timezones, aiming for simple, standard, and robust timestamp handling across languages. The current version is 0.1, with its last PyPI release in 2015, suggesting a maintenance or stable status rather than active development.
Warnings
- gotcha The library explicitly states it 'sticks quite closely to the RFC 3339 profile of ISO 8601'. If you require parsing more general or less strict ISO 8601 formats, this library might not be suitable, and you may need alternatives like `pyiso8601`.
- gotcha The library does not implement 'Unknown Local Offset Convention' (RFC 3339 Section 4.3) and only supports 'Internet Date/Time Format' (Section 5.6) for parsing. Leap seconds are also not addressed.
- deprecated For Python 3.2 and later, the built-in `datetime` module's `isoformat()` method can generate RFC 3339 compatible strings, and `datetime.fromisoformat()` (Python 3.7+) offers robust ISO 8601 parsing. More modern and actively maintained libraries like `rfc3339lib` or `strict-rfc3339` also exist.
- gotcha The GitHub repository notes that the project has 'MOVED TO: https://git.leastfixedpoint.com/tonyg/python-rfc3339'. This indicates the GitHub mirror may not be the primary development location, and active development or issue tracking might occur elsewhere, or not at all.
Install
-
pip install tonyg-rfc3339
Imports
- parse_datetime
from rfc3339 import parse_datetime
- generate_datetime
from rfc3339 import generate_datetime
- UTC
from rfc3339 import UTC
Quickstart
from datetime import datetime, timedelta
from rfc3339 import parse_datetime, generate_datetime, UTC
# Example 1: Parsing an RFC 3339 timestamp
timestamp_str = "2023-10-27T10:00:00Z"
dt_object = parse_datetime(timestamp_str)
print(f"Parsed datetime: {dt_object}")
# Expected: Parsed datetime: 2023-10-27 10:00:00+00:00
# Example 2: Parsing with an offset
offset_timestamp_str = "2023-10-27T12:00:00+02:00"
dt_offset_object = parse_datetime(offset_timestamp_str)
print(f"Parsed datetime with offset: {dt_offset_object}")
# Expected: Parsed datetime with offset: 2023-10-27 12:00:00+02:00
# Example 3: Generating an RFC 3339 timestamp from a datetime object
now_utc = datetime.now(UTC)
rfc3339_output = generate_datetime(now_utc)
print(f"Generated RFC 3339: {rfc3339_output}")
# Expected: Generated RFC 3339: 2026-04-11T18:42:00Z (example time)
# Example 4: Comparing two equivalent timestamps (parsed from different zones)
midnight_utc = parse_datetime("2008-08-24T00:00:00Z")
one_am_bst = parse_datetime("2008-08-24T01:00:00+01:00")
print(f"Are equivalent datetimes equal? {midnight_utc == one_am_bst}")
# Expected: True