Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Prepend leading zeros #7663

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

Merged
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
2 changes: 1 addition & 1 deletion api_core/google/api_core/datetime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def rfc3339(self):
"""
if self._nanosecond == 0:
return to_rfc3339(self)
nanos = str(self._nanosecond).rstrip("0")
nanos = str(self._nanosecond).rjust(9, '0').rstrip("0")
return "{}.{}Z".format(self.strftime(_RFC3339_NO_FRACTION), nanos)

@classmethod
Expand Down
19 changes: 19 additions & 0 deletions api_core/tests/unit/test_datetime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,20 +203,39 @@ def test_rfc3339_wo_nanos():
stamp = datetime_helpers.DatetimeWithNanoseconds(2016, 12, 20, 21, 13, 47, 123456)
assert stamp.rfc3339() == "2016-12-20T21:13:47.123456Z"

@staticmethod
def test_rfc3339_wo_nanos_w_leading_zero():
stamp = datetime_helpers.DatetimeWithNanoseconds(2016, 12, 20, 21, 13, 47, 1234)
assert stamp.rfc3339() == "2016-12-20T21:13:47.001234Z"

@staticmethod
def test_rfc3339_w_nanos():
stamp = datetime_helpers.DatetimeWithNanoseconds(
2016, 12, 20, 21, 13, 47, nanosecond=123456789
)
assert stamp.rfc3339() == "2016-12-20T21:13:47.123456789Z"

@staticmethod
def test_rfc3339_w_nanos_w_leading_zero():
stamp = datetime_helpers.DatetimeWithNanoseconds(
2016, 12, 20, 21, 13, 47, nanosecond=1234567
)
assert stamp.rfc3339() == "2016-12-20T21:13:47.001234567Z"

@staticmethod
def test_rfc3339_w_nanos_no_trailing_zeroes():
stamp = datetime_helpers.DatetimeWithNanoseconds(
2016, 12, 20, 21, 13, 47, nanosecond=100000000
)
assert stamp.rfc3339() == "2016-12-20T21:13:47.1Z"

@staticmethod
def test_rfc3339_w_nanos_w_leading_zero_and_no_trailing_zeros():
stamp = datetime_helpers.DatetimeWithNanoseconds(
2016, 12, 20, 21, 13, 47, nanosecond=1234500
)
assert stamp.rfc3339() == "2016-12-20T21:13:47.0012345Z"

@staticmethod
def test_from_rfc3339_w_invalid():
stamp = "2016-12-20T21:13:47"
Expand Down