Skip to content

linkcheck: Use correct function to convert from UTC time to UNIX epoch#11649

Merged
AA-Turner merged 6 commits intosphinx-doc:masterfrom
mitya57:timegm
Aug 28, 2023
Merged

linkcheck: Use correct function to convert from UTC time to UNIX epoch#11649
AA-Turner merged 6 commits intosphinx-doc:masterfrom
mitya57:timegm

Conversation

@mitya57
Copy link
Copy Markdown
Contributor

@mitya57 mitya57 commented Aug 26, 2023

Subject: linkcheck: Use correct function to convert from UTC time to UNIX epoch

Feature or Bugfix

  • Bugfix

Purpose

After subtracting the offset, we get time in UTC. But time.mktime() expects a time struct in local time, not in UTC. Quoting the Python documentation:

Use the following functions to convert between time representations:

From To Use
seconds since the epoch struct_time in UTC gmtime()
seconds since the epoch struct_time in local time localtime()
struct_time in UTC seconds since the epoch calendar.timegm()
struct_time in local time seconds since the epoch mktime()

So in our case, the correct function to use is calendar.timegm().

This fixes hanging tests when the local timezone is to the west of UTC. One could test this using:

TZ=America/New_York python3 -m pytest -k test_too_many_requests_retry_after_HTTP_date

After applying this change, the test passes with timezones from both western and eastern hemispheres.

After subtracting the offset, we get time in UTC. But time.mktime()
expects a time struct in local time, not in UTC. The correct function
for converting UTC time to UNIX epoch is calendar.timegm().

This fixes hanging tests when the local timezone is to the west of UTC.
One could test this using:

TZ=America/New_York python3 -m pytest -k test_too_many_requests_retry_after_HTTP_date
@AA-Turner
Copy link
Copy Markdown
Member

Should we add a test for this? Please could you also add an entry to CHANGES, this will go into 7.2.3.

A

@mitya57
Copy link
Copy Markdown
Contributor Author

mitya57 commented Aug 27, 2023

Done.

@AA-Turner AA-Turner enabled auto-merge (squash) August 28, 2023 03:34
@AA-Turner AA-Turner merged commit 2f025a4 into sphinx-doc:master Aug 28, 2023
@mitya57 mitya57 deleted the timegm branch August 28, 2023 06:59
AA-Turner added a commit that referenced this pull request Aug 28, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants