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

fix(timestamps): generate timestamps correctly and remove the deprecated pytz #461

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

uptickmetachu
Copy link

@uptickmetachu uptickmetachu commented Feb 14, 2024

Describe your changes

  • Fixed timestamp generation so that it correctly reflects the time at the timezone. Previously timestamps were generated with the local machine time datetime.datetime.now() and then set to the timezone.
  • I removed pytz as it is now deprecated since the inclusion of the built in zoneinfo

Issue number and link

Closes #453

Checklist before requesting a review

  • I have properly bumped all version refs by running ./scripts/version-bump.sh v<new> from project root - for example ./scripts/version-bump.sh v1.9.3

@echoboomer echoboomer self-requested a review February 24, 2024 23:23
@rwejdling
Copy link

@echoboomer would it be possible to get this (or similar fix) in the next release?

@uptickmetachu
Copy link
Author

@echoboomer I've rebased this and bumped to v1.10.8

Would love to have this one reviewed / merged before I do the confluence one.

@echoboomer
Copy link
Owner

Looks like this will need some changes.

@uptickmetachu
Copy link
Author

uptickmetachu commented May 24, 2024

https://docs.python.org/3/library/datetime.html#technical-detail

%Z
In strftime(), %Z is replaced by an empty string if tzname() returns None; otherwise %Z is replaced by the returned value, which must be a string.
strptime() only accepts certain values for %Z:
any value in time.tzname for your machine’s locale
the hard-coded values UTC and GMT
So someone living in Japan may have JST, UTC, and GMT as valid values, but probably not EST. It will raise ValueError for invalid values.

Mmmm using %Z in the output format means we won't be able to reparse the time (if it is not UTC or local to the machine time).

This has far reaching implications especially since all timestamps are stored as formatted timestamp strings in the database.

As a short term fix; I think all instances of: datetime.datetime.strptime(inc.created_at, tools.timestamp_fmt) will need to be replaced with a utility function that can correctly parse the stored times. This may involve an additional time parsing library like arrow.

@uptickmetachu
Copy link
Author

And another problem;

When we do parse the datetime with strptime it will be stored as a timezone naive datetime object. These objects
are then compared against other date time naive objects.
Eg:

https://github.com/echoboomer/incident-bot/blob/4126c4c3e9ba77a1186a9dc256c10a4c4717bf13/backend/bot/scheduler/scheduler.py#L90C1-L97C2

Should I modify this PR to ensure all timezones are datetime aware?

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.

[BUG] Timestamp mismatch
3 participants