RFC 3339 Date/Time Formatting

6.2 · active · verified Sat Apr 11

The `rfc3339` library for Python provides utilities to format `datetime` objects into RFC 3339 compliant strings and to parse such strings back into `datetime` objects. It closely adheres to the RFC 3339 standard, including providing its own UTC `tzinfo` implementation. While the PyPI package `6.2` was last released in 2019, the primary GitHub repository shows recent maintenance activity, indicating it's still actively maintained, albeit with a slow release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to format a `datetime` object into an RFC 3339 compliant string and parse such a string back into a `datetime` object using the `rfc3339` library. It also includes an example of parsing a string with a specific timezone offset.

from datetime import datetime, timezone
import rfc3339

# Get current UTC time
now_utc = datetime.now(timezone.utc)

# Format a datetime object to RFC 3339 string
rfc3339_string = rfc3339.rfc3339(now_utc)
print(f"Formatted: {rfc3339_string}")

# Parse an RFC 3339 string back to a datetime object
parsed_datetime = rfc3339.parse_rfc3339(rfc3339_string)
print(f"Parsed: {parsed_datetime}")

# Example with a specific date and offset
date_with_offset_str = "2023-10-27T10:00:00-05:00"
parsed_with_offset = rfc3339.parse_rfc3339(date_with_offset_str)
print(f"Parsed with offset: {parsed_with_offset}")

view raw JSON →