pyrfc3339.generator – Generate RFC 3339 timestamps
- pyrfc3339.generator.generate(dt: datetime, utc: bool = True, accept_naive: bool = False, microseconds: bool = False) str
Generate an RFC 3339-formatted timestamp from a
datetime.datetime.>>> from datetime import datetime, timezone >>> from zoneinfo import ZoneInfo >>> generate(datetime(2009, 1, 1, 12, 59, 59, 0, timezone.utc)) '2009-01-01T12:59:59Z'
The timestamp be normalized to UTC unless
utc=Falseis specified, in which case it will use the timezone from thedatetime’stzinfoattribute.>>> eastern = ZoneInfo('US/Eastern') >>> dt = datetime(2009, 1, 1, 12, 59, 59, tzinfo=eastern) >>> generate(dt) '2009-01-01T17:59:59Z' >>> generate(dt, utc=False) '2009-01-01T12:59:59-05:00'
Unless
accept_naive=Trueis specified, thedatetimemust not be naive.>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0)) Traceback (most recent call last): ... ValueError: naive datetime and accept_naive is False
>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0), accept_naive=True) '2009-01-01T12:59:59Z'
If, however,
accept_naive=Trueis specified, thedatetimeis assumed to represent a UTC time. Attempting to generate a local timestamp from a naive datetime will result in an error.>>> generate(datetime(2009, 1, 1, 12, 59, 59, 0), accept_naive=True, utc=False) Traceback (most recent call last): ... ValueError: cannot generate a local timestamp from a naive datetime
- Parameters:
dt (datetime.datetime) – the
datetimefor which to generate an RFC 3339 timestamp.utc (bool) –
Trueto normalize the supplieddatetime.datetimeto UTC;Falseotherwise. Defaults toTrue.accept_naive (bool) –
Trueifgenerate()should accept a ‘naive’ datetime (that is, one without timezone information) and treat it as a UTC timestamp;Falseotherwise. Defaults toFalse.microseconds (bool) –
Trueto generate a timestamp which includes fractional seconds, if present;Falseotherwise. Defaults toFalse. Note that fractional seconds are truncated, not rounded whenmicrosecondsisFalse.
- Returns:
the supplied
datetimeinstance represented as an RFC 3339 timestamp- Return type: