Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-38.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: fix
fix:
description: Timestamps can now be in the format with fractional seconds through
Python datetime as many timestamps are.
links:
- https://github.com/palantir/python-compute-module/pull/38
12 changes: 10 additions & 2 deletions compute_modules/function_registry/datetime_conversion_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@

class DatetimeConversionUtil:
DATETIME_FORMAT_STRING = "%Y-%m-%dT%H:%M:%SZ"
DATETIME_FORMAT_HIGHER_PRECISION_STRING = "%Y-%m-%dT%H:%M:%S.%fZ"

@staticmethod
def datetime_to_string(datetime_obj: datetime) -> str:
obj = datetime_obj.astimezone(timezone.utc)
# Format as ISO 8601 string with 'Z' suffix
return obj.strftime(DatetimeConversionUtil.DATETIME_FORMAT_STRING)
return obj.strftime(
DatetimeConversionUtil.DATETIME_FORMAT_HIGHER_PRECISION_STRING
if obj.microsecond > 0
else DatetimeConversionUtil.DATETIME_FORMAT_STRING
)

@staticmethod
def string_to_datetime(datetime_string: str) -> datetime:
return datetime.strptime(datetime_string, DatetimeConversionUtil.DATETIME_FORMAT_STRING)
try:
return datetime.strptime(datetime_string, DatetimeConversionUtil.DATETIME_FORMAT_STRING)
except ValueError:
return datetime.strptime(datetime_string, DatetimeConversionUtil.DATETIME_FORMAT_HIGHER_PRECISION_STRING)