Strict RFC3339
strict-rfc3339 is a Python library providing strict, simple, and lightweight functions for converting Unix timestamps to and from RFC3339 formatted strings. It aims to avoid the complexities of timezones as much as possible, focusing on strict adherence to the RFC3339 standard. The current version is 0.7, with development appearing to be in maintenance mode, as the last release was in April 2016.
Warnings
- gotcha The library does not support leap seconds. In validation, seconds values of '60' or '61' are rejected, as Python's native timestamps do not support them, and validating them accurately would require constantly updated timezone data.
- gotcha On 32-bit systems, RFC3339 generation using `gmtime` or `localtime` may be limited by the size of `time_t`, typically restricting dates to between approximately 1901 and 2038. This limitation does not affect `rfc3339_to_timestamp`.
- gotcha The library explicitly avoids direct usage of Python's `datetime` objects and comprehensive timezone libraries like `pytz` to prevent common issues related to DST transitions and ambiguous times. This means that while it strictly handles RFC3339 offsets, it does not provide full timezone conversion capabilities itself.
Install
-
pip install strict-rfc3339
Imports
- validate_rfc3339
import strict_rfc3339 strict_rfc3339.validate_rfc3339(...)
- rfc3339_to_timestamp
import strict_rfc3339 strict_rfc3339.rfc3339_to_timestamp(...)
- timestamp_to_rfc3339_utc
import strict_rfc3339 strict_rfc3339.timestamp_to_rfc3339_utc(...)
- now_to_rfc3339_utc
import strict_rfc3339 strict_rfc3339.now_to_rfc3339_utc()
- now_to_rfc3339_localoffset
import strict_rfc3339 strict_rfc3339.now_to_rfc3339_localoffset()
Quickstart
import strict_rfc3339
import time
# Get current UTC time as RFC3339 string
rfc3339_now_utc = strict_rfc3339.now_to_rfc3339_utc()
print(f"Current UTC RFC3339: {rfc3339_now_utc}")
# Get current local time as RFC3339 string with local offset
rfc3339_now_local = strict_rfc3339.now_to_rfc3339_localoffset()
print(f"Current Local RFC3339: {rfc3339_now_local}")
# Validate an RFC3339 string
is_valid = strict_rfc3339.validate_rfc3339("2023-10-27T10:00:00Z")
print(f"'2023-10-27T10:00:00Z' is valid: {is_valid}")
is_invalid = strict_rfc3339.validate_rfc3339("some rubbish")
print(f"'some rubbish' is valid: {is_invalid}")
# Convert RFC3339 to Unix timestamp
timestamp = strict_rfc3339.rfc3339_to_timestamp("2023-10-27T10:00:00+00:00")
print(f"Timestamp for '2023-10-27T10:00:00+00:00': {timestamp}")
print(f"Time tuple (UTC): {time.gmtime(timestamp)[:6]}")
# Convert Unix timestamp to RFC3339 (UTC)
new_rfc3339_utc = strict_rfc3339.timestamp_to_rfc3339_utc(timestamp)
print(f"Timestamp {timestamp} to RFC3339 UTC: {new_rfc3339_utc}")