Skip to content

Conversation

@rdblue
Copy link
Contributor

@rdblue rdblue commented Aug 29, 2022

This fixes some of the date/time transform methods introduced in #5462.

@rdblue rdblue requested a review from Fokko August 29, 2022 19:12
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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

Copy link
Contributor

@Fokko Fokko left a 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% 

@Fokko Fokko merged commit 97c85aa into apache:master Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants