rdblue commented on code in PR #5462:
URL: https://github.com/apache/iceberg/pull/5462#discussion_r942801391
##########
python/pyiceberg/utils/datetime.py:
##########
@@ -116,3 +131,33 @@ def to_human_timestamptz(timestamp_micros: int) -> str:
def to_human_timestamp(timestamp_micros: int) -> str:
"""Converts a TimestampType value to human string"""
return (EPOCH_TIMESTAMP +
timedelta(microseconds=timestamp_micros)).isoformat()
+
+
+def micros_to_hours(timestamp: int) -> int:
+ """Converts a timestamp in microseconds to a date in hours"""
+ return int((datetime.utcfromtimestamp(timestamp / 1_000_000) -
EPOCH_TIMESTAMP).total_seconds() / 3600)
+
+
+def days_to_months(days: int) -> int:
+ """Creates a date from the number of days from 1970-01-01"""
+ dt = datetime.utcfromtimestamp(days * 86400)
+ return (dt.year - EPOCH_TIMESTAMP.year) * 12 + (dt.month -
EPOCH_TIMESTAMP.month) - (1 if dt.day < EPOCH_TIMESTAMP.day else 0)
+
+
+def micros_to_months(timestamp: int) -> int:
+ dt = datetime.utcfromtimestamp(timestamp / 1_000_000)
+ return (dt.year - EPOCH_TIMESTAMP.year) * 12 + (dt.month -
EPOCH_TIMESTAMP.month) - (1 if dt.day < EPOCH_TIMESTAMP.day else 0)
+
+
+def days_to_years(days: int) -> int:
+ dt = datetime.utcfromtimestamp(days * 86400)
+ 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
+ )
Review Comment:
I think it may be easier if you use dates?
```python
return days_to_date(days).year - EPOCH_DATE.year
```
That seems to have the right behavior to me: `days_to_years(-1)` is -1
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]