Monkeypatching Timezones in Python

In ledgerify (sidenote: Ledgerify is my work-in-progress approach to importing data into ledger/hledger for power users (or maybe just for me).) I’m dealing with a situation that imported dates are possibly in a different timezone than the local one. Potentially, this could lead to saving transactions with off-by-one dates. Users may specify the timezone for their input data which will be then transposed to the local timezone.

To reliably test this feature, we must monkeypatch the local timezone to one fixed value. I haven’t found any library to do that (including freezegun), so I wrote it myself. It’s a context manager which automatically restores the real timezone when it exits. This works on Unix and to be frank I don’t know how to approach this problem on Windows and other systems which don’t have tzset() function. (sidenote: Although there’s _tzset for Windows, Python documentation still marks time.tzset as Unix-only. )

@contextmanager
def utctimezone():
    # Note: tzset() is available only on Unix
    real_tz = os.environ.get("TZ")

    try:
        os.environ["TZ"] = "+00:00"
        time.tzset()
        yield
    finally:
        if real_tz is None:
            del os.environ["TZ"]
        else:
            os.environ["TZ"] = real_tz
        time.tzset()