-
Notifications
You must be signed in to change notification settings - Fork 3k
Python: Fix date/time transforms #5667
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
Conversation
| def micros_to_years(timestamp: int) -> int: | ||
| dt = micros_to_timestamp(timestamp) | ||
| return (dt.year - EPOCH_TIMESTAMP.year) - ( | ||
| 1 if dt.month < EPOCH_TIMESTAMP.month or (dt.month == EPOCH_TIMESTAMP.month and dt.day < EPOCH_TIMESTAMP.day) else 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Month and day are never 0.
| return int((datetime.utcfromtimestamp(timestamp // 1_000_000) - EPOCH_TIMESTAMP).total_seconds() / 3600) | ||
| def micros_to_hours(micros: int) -> int: | ||
| """Converts a timestamp in microseconds to hours from 1970-01-01T00:00""" | ||
| return micros // 3_600_000_000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This converts to a timestamp only to get back to a delta, and then get the seconds. Instead, we can convert directly.
| def micros_to_days(timestamp: int) -> int: | ||
| """Converts a timestamp in microseconds to a date in days""" | ||
| return (datetime.fromtimestamp(timestamp / 1_000_000) - EPOCH_TIMESTAMP).days | ||
| return timedelta(microseconds=timestamp).days |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, we can go directly to delta rather than going to date and subtracting.
Fokko
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks @rdblue
Also, this is already properly tested:
poetry run coverage report -m --fail-under=90
Name Stmts Miss Cover Missing
------------------------------------------------------------------------
...
pyiceberg/utils/datetime.py 69 0 100%
This fixes some of the date/time transform methods introduced in #5462.