Skip to main content

pytest-dst-vectors

tests

A pytest plugin that runs your tests over DST edge cases: local wall times that do not exist (spring-forward gap), that happen twice (fall-back overlap), and the seconds right next to a change. Use it when you test code that converts local wall times to UTC around DST changes.

The cases come from dst-test-vectors (pinned to 0.1.1). This plugin adds no data of its own and never uses the network. Python 3.10+, pytest 7+.

Install

pip install pytest-dst-vectors

The plugin loads itself (pytest11 entry point). A test that has a dst_case argument is run once per case.

Examples

Every Python example below is run by the test suite (tests/test_readme.py extracts the code blocks and runs them with pytest).

1. Gaps and overlaps with zoneinfo

from datetime import timezone
from zoneinfo import ZoneInfo

import pytest


@pytest.mark.dst_vectors(kind="gap")
def test_gap_fold(dst_case):
    zone = ZoneInfo(dst_case.zone)
    before = dst_case.local.replace(tzinfo=zone, fold=0).astimezone(timezone.utc)
    after = dst_case.local.replace(tzinfo=zone, fold=1).astimezone(timezone.utc)
    assert before == dst_case.compatible  # fold=0 uses the offset before the change
    assert after == dst_case.earlier      # fold=1 uses the offset after it


@pytest.mark.dst_vectors(kind="overlap", zone="America/New_York")
def test_overlap_fold(dst_case):
    zone = ZoneInfo(dst_case.zone)
    first = dst_case.local.replace(tzinfo=zone, fold=0).astimezone(timezone.utc)
    second = dst_case.local.replace(tzinfo=zone, fold=1).astimezone(timezone.utc)
    assert (first, second) == (dst_case.earlier, dst_case.later)

2. Test your own to_utc with dst_check

dst_check(func, policy=..., **filters) calls func(zone, naive_local_datetime) for every matching case and returns the mismatches (an empty list means all agree). Policies are compatible (default), earlier, later and raise (gaps must raise); see dst-test-vectors for details.

from datetime import timezone
from zoneinfo import ZoneInfo


def to_utc(zone, local):
    return local.replace(tzinfo=ZoneInfo(zone)).astimezone(timezone.utc)


def to_utc_later(zone, local):
    return local.replace(tzinfo=ZoneInfo(zone), fold=1).astimezone(timezone.utc)


def test_to_utc_earlier(dst_check):
    assert dst_check(to_utc, policy="earlier") == []


def test_to_utc_later_in_overlaps(dst_check):
    assert dst_check(to_utc_later, policy="later", kind="overlap") == []

3. Run against a full vectors file

The plugin uses the bundled sample by default. To run the same tests against another file in the same format, such as the full 2000-2050 set (dst_test_vectors.json), pass it on the command line:

pytest --dst-vectors=dst_test_vectors.json

or set it in pytest.ini (pyproject.toml works the same under [tool.pytest.ini_options]):

[pytest]
dst_vectors_file = dst_test_vectors.json
import pytest


@pytest.mark.dst_vectors(zone="Europe/London", year=2026)
def test_london_2026(dst_case):
    assert dst_case.zone == "Europe/London"
    assert dst_case.local.year == 2026

The test suite runs this example with a small stand-in file named dst_test_vectors.json, once with --dst-vectors=dst_test_vectors.json and once with the ini line above.

Marker

@pytest.mark.dst_vectors(kind=None, zone=None, tag=None, year=None), keyword arguments only:

Argument Meaning
kind "gap", "overlap" or "normal"
zone IANA zone name, or a list of names
tag e.g. "midnight", "odd_offset", "delta_30min", "dst"
year local year

A test that takes dst_case without the marker gets every case. dst_case is a dst_test_vectors.Case (zone, local, kind, utc, earlier, later, compatible, transition_utc, tags, note).

If the filters match 0 cases, or kind is not one of the three kinds, collection of that test fails with a message that names the filters. Unknown marker arguments fail the same way.

Test ids

Ids are Case.id: zone, local time, kind.

test_gap_fold[America/New_York 2026-03-08T02:00:00 gap]
test_overlap_fold[America/New_York 2026-11-01T01:30:00 overlap]

Select one with -k "New_York and overlap".

Options

Option Ini Meaning
--dst-vectors=PATH dst_vectors_file Vectors JSON file in the dst-test-vectors format. Default: the bundled sample. A relative ini path is taken from the rootdir.

The report header shows what is active, for example dst-vectors: bundled sample, tzdata 2026f, 36 cases. An unreadable file stops pytest with a usage error.

What the bundled sample covers

36 cases for 2026 in 5 zones (America/New_York, Europe/London, America/Havana, Australia/Lord_Howe, Pacific/Chatham), generated from IANA tzdata 2026f: gaps, overlaps and the seconds next to each change, including a 30-minute change (Lord Howe), +12:45/+13:45 (Chatham) and changes at midnight (Havana). Cases are unchanged from the dst-test-vectors bundle. The expected values only match a machine whose tz database is as new as the vectors, so an older system tzdata can fail a few cases; install tzdata to pin one.

Full set

The full set has 51,404 vectors for 2000-2050 in every zone, in the same JSON format: https://github.com/plainfoldhq/tz-transitions (paid; the sample above is free). DST dates by country: https://plainfoldhq.github.io/dst-dates/

About

Plainfold is an AI-run business; this package was written and tested by AI agents.

MIT license. Questions: plainfoldhq@proton.me.

Metadata

Release files for pytest-dst-vectors 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-dst-vectors 0.1.0
File Size Uploaded Provenance Actions
pytest_dst_vectors-0.1.0.tar.gz 9.9 kB Without provenance Details for pytest_dst_vectors-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-dst-vectors 0.1.0
File Interpreter ABI Platform Actions
pytest_dst_vectors-0.1.0-py3-none-any.whl Python 3 none any Details for pytest_dst_vectors-0.1.0-py3-none-any.whl

Total release size: 16.6 kB

Release files / pytest_dst_vectors-0.1.0.tar.gz

Download URL pytest_dst_vectors-0.1.0.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2efd92a7721cf6b8bd7dca0c99f327c85f457e14b797b61bf6a8a50d61105ec2
BLAKE2b-256 checksum
How to use checksums
b6e8ce2c18ecbc7555cb1ae5b871a5dc616c8a3cf0060440adb665edbefa42ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / pytest_dst_vectors-0.1.0-py3-none-any.whl

Download URL pytest_dst_vectors-0.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd67317bce152ed6a805d0fe7268c288db2036192ad3ea0c28f45acfe13d9508
BLAKE2b-256 checksum
How to use checksums
ec4be0beb97c2fffadbdfe98efc1bcf2752469919f30f87910d0bb0a286453e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.16

Security for pytest-dst-vectors 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page