RFC 3339 Timestamp Library

2.1.0 · active · verified Sun Apr 05

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

Install

Imports

Quickstart

Demonstrates how to generate RFC 3339 timestamps from timezone-aware `datetime` objects and parse RFC 3339 strings back into `datetime` objects, including handling of naive datetimes for generation.

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}")

view raw JSON →