RFC 3339 Timestamp Library
pyRFC3339 parses and generates RFC 3339-compliant timestamps using Python datetime.datetime objects. It aims to simplify timestamp parsing and generation, leveraging improvements in native Python capabilities. The current stable version is 2.1.0, released on August 23, 2025. It appears to be actively maintained with recent releases and GitHub activity.
Warnings
- breaking Version 2.0 and later of `pyrfc3339` require Python 3.9 or higher. Users on Python 3.8 or older must remain on a `pyrfc3339` 1.x release or upgrade their Python interpreter.
- gotcha `generate()` by default requires timezone-aware `datetime` objects. Passing a 'naive' datetime (one without timezone information) will raise a `ValueError`.
- gotcha When using `parse()`, if `produce_naive=True` is specified, the returned `datetime` object will not have `tzinfo` attached. This can lead to unexpected behavior if subsequent operations expect a timezone-aware `datetime`.
- gotcha `parse()` delegates to Python's native `datetime.datetime.fromisoformat()`. While generally effective, this means it may accept some ISO 8601 formatted timestamps that do not strictly adhere to all nuances of RFC 3339.
Install
-
pip install pyrfc3339
Imports
- generate, parse
from pyrfc3339 import generate, parse
Quickstart
from datetime import datetime, timezone, timedelta
from pyrfc3339 import generate, parse
# Get current UTC time and generate RFC 3339 string
now_utc = datetime.now(timezone.utc)
print(f"Current UTC: {now_utc.isoformat()}")
rfc3339_timestamp = generate(now_utc)
print(f"Generated RFC 3339: {rfc3339_timestamp}")
# Parse an RFC 3339 timestamp
parsed_dt_utc = parse('2009-01-01T10:01:02Z')
print(f"Parsed UTC: {parsed_dt_utc}")
# Parse with an offset
parsed_dt_offset = parse('2009-01-01T14:01:02-04:00')
print(f"Parsed with offset: {parsed_dt_offset}")
# Example of generating from a naive datetime (requires accept_naive=True)
try:
generate(datetime(2023, 1, 1, 12, 0, 0))
except ValueError as e:
print(f"Expected error for naive datetime: {e}")
naive_as_utc = generate(datetime(2023, 1, 1, 12, 0, 0), accept_naive=True)
print(f"Generated from naive (as UTC): {naive_as_utc}")