RFC 3339 Date/Time Utilities

0.1 · maintenance · verified Sat Apr 11

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

Install

Imports

Quickstart

The quickstart demonstrates parsing RFC 3339 formatted strings into timezone-aware `datetime` objects and generating RFC 3339 strings from `datetime` objects. It also shows how to use the provided `UTC` timezone object and the library's ability to handle timezone offsets for comparison.

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

view raw JSON →