8000 Use calendar.timegm instead of time.mktime for UTC timestamps by fenuks · Pull Request #565 · python-jsonschema/check-jsonschema · GitHub
[go: up one dir, main page]

Skip to content

Use calendar.timegm instead of time.mktime for UTC timestamps #565

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

Closed
wants to merge 1 commit into from

Conversation

fenuks
Copy link
@fenuks fenuks commented May 8, 2025

time.mktime() converts a time tuple in local time to seconds since the Epoch, as stated in the docs:

Convert a time tuple in local time to seconds since the Epoch.
Note that mktime(gmtime(0)) will not generally return zero for most
time zones; instead the returned value will either be equal to that
of the timezone or altzone attributes on the time module.

calendar.timegm() is guaranteed to produce UTC timestamp:

Unrelated but handy function to calculate Unix timestamp from GMT.

time.mktime() converts a time tuple in local time to seconds since the
Epoch, as stated in the docs:

> Convert a time tuple in local time to seconds since the Epoch.
> Note that mktime(gmtime(0)) will not generally return zero for most
> time zones; instead the returned value will either be equal to that
> of the timezone or altzone attributes on the time module.

calendar.timegm() is guaranteed to produce UTC timestamp:

> Unrelated but handy function to calculate Unix timestamp from GMT.
@sirosen
Copy link
Member
sirosen commented May 15, 2025

Thanks for this fix!

I took some time to poke around and see if there's any other interface which provides this -- it feels weird to import calendar for basic time functionality -- but it looks like not. 🤷

I need to add a test which demonstrates the issue before I can merge, and there are issues in CI related to the lastest click release. I've got a PR up for the click issue, and that should unblock me working up a test to confirm this fix. 😄

@sirosen sirosen mentioned this pull request May 18, 2025
sirosen added a commit that referenced this pull request May 18, 2025
sirosen added a commit that referenced this pull request May 18, 2025
* Use calendar.timegm instead of time.mktime for UTC timestamps

time.mktime() converts a time tuple in local time to seconds since the
Epoch, as stated in the docs:

> Convert a time tuple in local time to seconds since the Epoch.
> Note that mktime(gmtime(0)) will not generally return zero for most
> time zones; instead the returned value will either be equal to that
> of the timezone or altzone attributes on the time module.

calendar.timegm() is guaranteed to produce UTC timestamp:

> Unrelated but handy function to calculate Unix timestamp from GMT.

* Add a regression test for localtime handling

* Add changelog entry for #565

---------

Co-authored-by: Radosław Kozicki <rkpraca@posteo.net>
@sirosen sirosen closed this in #567 May 18, 2025
@fenuks
Copy link
Author
fenuks commented May 19, 2025

Thank you!

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

Successfully merging this pull request may close these issues.

2 participants
0