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
15 changes: 10 additions & 5 deletions core/google/cloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
(?P<no_fraction>
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} # YYYY-MM-DDTHH:MM:SS
)
\. # decimal point
(?P<nanos>\d{1,9}) # nanoseconds, maybe truncated
( # Optional decimal part
\. # decimal point
(?P<nanos>\d{1,9}) # nanoseconds, maybe truncated
)?
Z # Zulu
""", re.VERBOSE)
# NOTE: Catching this ImportError is a workaround for GAE not supporting the
Expand Down Expand Up @@ -429,9 +431,12 @@ def _rfc3339_nanos_to_datetime(dt_str):
bare_seconds = datetime.datetime.strptime(
with_nanos.group('no_fraction'), _RFC3339_NO_FRACTION)
fraction = with_nanos.group('nanos')
scale = 9 - len(fraction)
nanos = int(fraction) * (10 ** scale)
micros = nanos // 1000
if fraction is None:
micros = 0
else:
scale = 9 - len(fraction)
nanos = int(fraction) * (10 ** scale)
micros = nanos // 1000
return bare_seconds.replace(microsecond=micros, tzinfo=UTC)


Expand Down
18 changes: 18 additions & 0 deletions core/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,24 @@ def test_w_truncated_nanos(self):
year, month, day, hour, minute, seconds, micros, UTC)
self.assertEqual(result, expected_result)

def test_without_nanos(self):
import datetime
from google.cloud._helpers import UTC

year = 1988
month = 4
day = 29
hour = 12
minute = 12
seconds = 12

dt_str = '%d-%02d-%02dT%02d:%02d:%02dZ' % (
year, month, day, hour, minute, seconds)
result = self._callFUT(dt_str)
expected_result = datetime.datetime(
year, month, day, hour, minute, seconds, 0, UTC)
self.assertEqual(result, expected_result)

def test_w_naonseconds(self):
import datetime
from google.cloud._helpers import UTC
Expand Down