Skip to content
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

fix(data-classes): include milliseconds in scalar types #504

Merged
merged 2 commits into from
Jul 6, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ def _formatted_time(now: datetime.date, fmt: str, timezone_offset: int) -> str:
str
Returns string formatted time with optional timezone offset
"""
if timezone_offset == 0:
return now.strftime(fmt + "Z")
if timezone_offset != 0:
now = now + datetime.timedelta(hours=timezone_offset)

datetime_str = now.strftime(fmt)
if fmt.endswith(".%f"):
datetime_str = datetime_str[:-3]

now = now + datetime.timedelta(hours=timezone_offset)
fmt += "+" if timezone_offset > 0 else "-"
fmt += str(abs(timezone_offset)).zfill(2)
fmt += ":00:00"
if timezone_offset == 0:
postfix = "Z"
else:
postfix = "+" if timezone_offset > 0 else "-"
postfix += str(abs(timezone_offset)).zfill(2)
postfix += ":00:00"

return now.strftime(fmt)
return datetime_str + postfix


def make_id() -> str:
Expand Down Expand Up @@ -65,7 +71,7 @@ def aws_time(timezone_offset: int = 0) -> str:
str
Returns current time as AWSTime scalar string with optional timezone offset
"""
return _formatted_time(datetime.datetime.utcnow(), "%H:%M:%S", timezone_offset)
return _formatted_time(datetime.datetime.utcnow(), "%H:%M:%S.%f", timezone_offset)


def aws_datetime(timezone_offset: int = 0) -> str:
Expand All @@ -81,7 +87,7 @@ def aws_datetime(timezone_offset: int = 0) -> str:
str
Returns current time as AWSDateTime scalar string with optional timezone offset
"""
return _formatted_time(datetime.datetime.utcnow(), "%Y-%m-%dT%H:%M:%S", timezone_offset)
return _formatted_time(datetime.datetime.utcnow(), "%Y-%m-%dT%H:%M:%S.%f", timezone_offset)


def aws_timestamp() -> int:
Expand Down
13 changes: 8 additions & 5 deletions tests/functional/test_data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,13 +1210,18 @@ def test_aws_date_utc():
def test_aws_time_utc():
time_str = aws_time()
assert isinstance(time_str, str)
assert datetime.datetime.strptime(time_str, "%H:%M:%SZ")
assert datetime.datetime.strptime(time_str, "%H:%M:%S.%fZ")


def test_aws_datetime_utc():
datetime_str = aws_datetime()
assert isinstance(datetime_str, str)
assert datetime.datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%SZ")
assert datetime.datetime.strptime(datetime_str[:-1] + "000Z", "%Y-%m-%dT%H:%M:%S.%fZ")


def test_format_time_to_milli():
now = datetime.datetime(2024, 4, 23, 16, 26, 34, 123021)
datetime_str = _formatted_time(now, "%H:%M:%S.%f", -12)
assert datetime_str == "04:26:34.123-12:00:00"


def test_aws_timestamp():
Expand All @@ -1227,14 +1232,12 @@ def test_aws_timestamp():
def test_format_time_positive():
now = datetime.datetime(2022, 1, 22)
datetime_str = _formatted_time(now, "%Y-%m-%d", 8)
assert isinstance(datetime_str, str)
assert datetime_str == "2022-01-22+08:00:00"


def test_format_time_negative():
now = datetime.datetime(2022, 1, 22, 14, 22, 33)
datetime_str = _formatted_time(now, "%H:%M:%S", -12)
assert isinstance(datetime_str, str)
assert datetime_str == "02:22:33-12:00:00"


Expand Down