tzoffset
Parse and format bare numeric UTC offsets (Z, +05:30, -0800) both ways.
Install
pip install tzoffset
Usage
parse_offset turns an offset string into a fixed-offset datetime.timezone;
format_offset writes one back in the style you choose.
from datetime import timedelta
from tzoffset import format_offset, parse_offset
print(parse_offset("+05:30"))
# UTC+05:30
print(parse_offset("-0800").utcoffset(None))
# -1 day, 16:00:00
print(parse_offset("Z") == parse_offset("+00"))
# True
print(format_offset(parse_offset("+0930")))
# +09:30
print(format_offset(timedelta(hours=-8), style="compact"))
# -0800
print(format_offset(timedelta(hours=9), style="short"))
# +09
print(format_offset(timedelta(0), zero="Z"))
# Z
Grammars
parse_offset(text, grammar=...) picks how strict the reading is. Digits are ASCII only,
hours go up to 23 and minutes and seconds up to 59 in every grammar. Anything else raises
OffsetError, a ValueError.
| grammar | accepts | rejects |
|---|---|---|
"lenient" (default) |
Z, z, +hh, +hhmm, +hh:mm, +hhmmss, +hh:mm:ss, surrounding ASCII whitespace |
mixed colon and compact forms |
"rfc3339" |
Z, z, +hh:mm (RFC 3339 section 5.6) |
everything else, spaces included |
"iso8601" |
Z, z, +hh, +hhmm, +hh:mm (RFC 3339 Appendix A) |
seconds, spaces, -00, -0000, -00:00 |
from tzoffset import OffsetError, parse_offset
print(parse_offset("+05:30:45").utcoffset(None))
# 5:30:45
for text in ["+0530", "+05:30:45", " +05:30"]:
try:
parse_offset(text, grammar="rfc3339")
except OffsetError as error:
print(error)
# not a valid rfc3339 UTC offset
# not a valid rfc3339 UTC offset
# not a valid rfc3339 UTC offset
Unknown local offset
RFC 3339 section 4.3, as updated by RFC 9557 section 2, gives Z and -00:00 one meaning:
the UTC time is known and the local offset is not. +00:00 instead says UTC is the
reference for the time; RFC 5322 gives mail the same pair, -0000 and +0000. All of them
parse to UTC; is_unknown_local_offset keeps the difference, and zero= picks the
spelling when formatting.
from datetime import datetime, timezone
from tzoffset import format_offset, is_unknown_local_offset
print(is_unknown_local_offset("-00:00"), is_unknown_local_offset("Z"))
# True True
print(is_unknown_local_offset("+00:00"))
# False
print(format_offset(timezone.utc, zero="-"))
# -00:00
print(format_offset(datetime(2026, 7, 1, 12, tzinfo=timezone.utc), zero="Z"))
# Z
Zones
A zoneinfo.ZoneInfo has no single offset, so format_offset takes an aware datetime
and writes the offset in force at that moment. Offsets with seconds, such as the local mean
time the tz database keeps for years before standard time, are written with a seconds field
(-04:56:02); fractions of a second are refused. On Windows, zoneinfo needs the tzdata
package (pip install tzdata) to find any zone.
from datetime import datetime
from zoneinfo import ZoneInfo
from tzoffset import format_offset
print(format_offset(datetime(2026, 1, 15, tzinfo=ZoneInfo("Europe/Madrid"))))
# +01:00
print(format_offset(datetime(2026, 7, 15, tzinfo=ZoneInfo("Europe/Madrid"))))
# +02:00
print(format_offset(datetime(1800, 1, 1, tzinfo=ZoneInfo("America/New_York"))))
# -04:56:02
Command line
$ tzoffset +0530 -08 Z
+05:30
-08:00
+00:00
$ tzoffset --style compact --zero Z +05:30 -00:00
+0530
Z
$ tzoffset --zone America/New_York --at 2026-03-08T12:00
-04:00
--grammar picks the input grammar, --zone with --at prints the offset of a tz
database zone at a moment (without --at it is now). --at takes RFC 3339 timestamps
such as 1985-04-12T23:20:50.52Z on every supported Python; the GUIDE lists the other
forms. A naive --at is wall time in that zone; a wall time skipped or repeated by a clock change takes the offset in force before the
change (fold=0). Bad input exits with status 2, and a reader that closes the output
early (| head) gives status 1. Also available as python -m tzoffset.
API
| Name | What it does |
|---|---|
parse_offset(text, *, grammar="lenient") |
A fixed-offset datetime.timezone; timezone.utc itself for a zero offset. |
is_unknown_local_offset(text, *, grammar="lenient") |
True for Z and a negative zero (-00:00, -0000, -00), after validating the text. |
format_offset(value, *, style="colon", zero="+") |
Text for a timedelta, a fixed-offset tzinfo or an aware datetime. |
Grammar, Style, Zero |
Literal aliases of the accepted grammar, style and zero values. |
OffsetError |
ValueError subclass for text that is not an offset and values out of range. Arguments of the wrong type raise TypeError. Messages omit supplied text arguments. |
The GUIDE has every rule in detail, and the CHANGELOG what changed in each release.
Why
Python can read and write an offset only as a side effect of something larger, and each way has gaps (checked on CPython 3.10 to 3.14):
datetime.strptime(text, "%z")returns adatetimeof 1 January 1900 that carries the offset. It refuses+05and a lower-casez, and it accepts what protocols do not: Arabic-Indic digits (+٠٥:30reads as+05:30) and fractions of a second (+05:30:45.5). It reads-00:00as plain UTC, so the "local offset unknown" meaning of RFC 3339 is lost, and there is no strict mode.datetime.fromisoformatandtime.fromisoformatneed a date or a time in front of the offset and refuse a lower-casez, which RFC 3339 allows.strftime("%z")writes only+0530;%:z(+05:30) exists from Python 3.12. Neither writesZ,-00:00or the short+05, andisoformat()always writes+00:00.python-dateutil(3.0.0) hasisoparser().parse_tzstr, which reads++5as+05:00and+05:3as+05:03, and has no seconds field.
tzoffset is small, typed and has no dependencies. It reads and writes the offset alone,
both ways, with a named grammar for RFC 3339 and for ISO 8601 and a lenient one for logs,
and it keeps -00:00 apart from +00:00. It is tested against the texts of RFC 3339,
RFC 5322 and RFC 9557, the zone offsets of the tz database 2026e, java.time.ZoneOffset,
rfc3339-validator, python-dateutil and the standard library.
Not covered: POSIX TZ strings such as EST5EDT,M3.2.0,M11.1.0 (rule sets with names and
an inverted sign; use zoneinfo), and offsets of a day or more, which Python cannot hold.
Development
python scripts/fetch_vectors.py downloads the RFCs and the tz database release listed in
tests/vectors/sources/MANIFEST.txt and checks their SHA-256 (they are not part of the
repository; their tests skip without them). python scripts/java_oracle.py regenerates
the recorded java.time.ZoneOffset output with a JDK. pip install -e .[dev] installs the
tools and the reference parsers, scripts/check.ps1 runs format, lint, types and tests,
and TZOFFSET_E2E=1 also builds the wheel and tests the installed package and its command.
TZOFFSET_E2E_PYPI=1 tests upgrading the live PyPI 0.2.0 release to the built wheel in
the same environment and compares the stable API and command line before and after.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for tzoffset 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tzoffset-1.0.0.tar.gz | 91.9 kB | Without provenance | Details for tzoffset-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tzoffset-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for tzoffset-1.0.0-py3-none-any.whl |
Total release size: 109.3 kB
Release files / tzoffset-1.0.0.tar.gz
| Download URL | tzoffset-1.0.0.tar.gz |
|---|---|
| Size | 91.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
75a79212e838a768ae8f11152b1dd3282171699b3e69a95291ee996e9547e08e
|
|
BLAKE2b-256 checksum How to use checksums |
2d198fb564c872777db71e6ab4040b0aea474019fb6fad414f9ba60757b57c87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / tzoffset-1.0.0-py3-none-any.whl
| Download URL | tzoffset-1.0.0-py3-none-any.whl |
|---|---|
| Size | 17.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
000d3882d8e0e2d13e793512c08487eb6800247f59c6dcf47f2ad6cce7d86116
|
|
BLAKE2b-256 checksum How to use checksums |
667c16262d4387b262d22c1e2949843848d2bf564f0a1b5ef19bad57afe7987b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for tzoffset 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.