Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate treating naive datetimes as UTC #353

Open
RazerM opened this issue Aug 13, 2023 · 0 comments
Open

Deprecate treating naive datetimes as UTC #353

RazerM opened this issue Aug 13, 2023 · 0 comments
Milestone

Comments

@RazerM
Copy link
Collaborator

RazerM commented Aug 13, 2023

LoggingHandler and RedirectLoggingHandler need to be able to convert LogRecord.time to a timestamp, but logbook.set_datetime_format means it is impossible to know what a naive datetime means.

Logbook should match the Python standard library and treat naive datetimes as local time.

Plan

This will be a breaking change, i.e. Logbook 2.0

  • Remove logbook.set_datetime_format.

  • Change LogRecord.time to always use aware datetimes. May wish to have Logger pass down a timezone which defaults to timezone.utc.

  • Add a tzinfo attribute to Handler which does record.time.astimezone(tzinfo) if it is set to None or datetime.tzinfo. This allows having handlers convert to different timezones, similar to logging.Formatter.convert. We'll need to make this work in the format string, since currently you do record.time.

    Converting to a timezone is similar performance to the conversion logging.Formatter already does:

    In [1]: from time import localtime, gmtime
    
    In [2]: from datetime import datetime, timezone
    
    In [3]: from zoneinfo import ZoneInfo
    
    In [4]: now = datetime.now(timezone.utc)
    
    In [5]: nowts = now.timestamp()
    
    In [6]: berlin = ZoneInfo('Europe/Berlin')
    
    In [7]: %timeit localtime(nowts)
    331 ns ± 0.701 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
    
    In [8]: %timeit gmtime(nowts)
    218 ns ± 0.35 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
    
    In [9]: %timeit now.astimezone(berlin)
    195 ns ± 0.657 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
    
    In [10]: %timeit now.astimezone(timezone.utc)
    45 ns ± 0.104 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
@RazerM RazerM added this to the 2.0.0 milestone Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant