Fokko commented on code in PR #1632: URL: https://github.com/apache/iceberg-python/pull/1632#discussion_r1977718277
########## pyiceberg/utils/datetime.py: ########## @@ -91,6 +91,57 @@ def timestamp_to_micros(timestamp_str: str) -> int: raise ValueError(f"Invalid timestamp without zone: {timestamp_str} (must be ISO-8601)") +def time_str_to_nanos(time_str: str) -> int: + """Convert an ISO-8601 formatted time to nanoseconds from midnight.""" + return time_to_nanos(time.fromisoformat(time_str)) + + +def time_to_nanos(t: time) -> int: + """Convert a datetime.time object to nanoseconds from midnight.""" + # python datetime and time doesn't have nanoseconds support yet + # https://github.com/python/cpython/issues/59648 + return ((((t.hour * 60 + t.minute) * 60) + t.second) * 1_000_000 + t.microsecond) * 1_000 + + +def datetime_to_nanos(dt: datetime) -> int: + """Convert a datetime to nanoseconds from 1970-01-01T00:00:00.000000000.""" + # python datetime and time doesn't have nanoseconds support yet + # https://github.com/python/cpython/issues/59648 + if dt.tzinfo: + delta = dt - EPOCH_TIMESTAMPTZ + else: + delta = dt - EPOCH_TIMESTAMP + return ((delta.days * 86400 + delta.seconds) * 1_000_000 + delta.microseconds) * 1_000 + + +def timestamp_to_nanos(timestamp_str: str) -> int: + """Convert an ISO-9601 formatted timestamp without zone to microseconds from 1970-01-01T00:00:00.000000. + + Currently only microsecond precision timestamp_str is supported as python datetime does not have + nanoseconds support. + """ + if ISO_TIMESTAMP.fullmatch(timestamp_str): + return datetime_to_nanos(datetime.fromisoformat(timestamp_str)) Review Comment: Here we truncate the nano's, which doesn't seem right. Maybe we need to do some post-processing to still add these to the `int`? WDYT? -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org