forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving _pb_timestamp_to_datetime into core.
This is in advance of `v1beta3`, where it will be needed to parse `Value.timestamp_value` (which is of type `timestamp_pb2.Timestamp`). Also adding `_datetime_to_pb_timestamp` for the other direction.
- Loading branch information
Showing
4 changed files
with
77 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import socket | ||
import sys | ||
|
||
from google.protobuf import timestamp_pb2 | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
dhermes
Author
Owner
|
||
import six | ||
from six.moves.http_client import HTTPConnection # pylint: disable=F0401 | ||
|
||
|
@@ -329,6 +330,39 @@ def _to_bytes(value, encoding='ascii'): | |
raise TypeError('%r could not be converted to bytes' % (value,)) | ||
|
||
|
||
def _pb_timestamp_to_datetime(timestamp): | ||
"""Convert a Timestamp protobuf to a datetime object. | ||
:type timestamp: :class:`google.protobuf.timestamp_pb2.Timestamp` | ||
:param timestamp: A Google returned timestamp protobuf. | ||
:rtype: :class:`datetime.datetime` | ||
:returns: A UTC datetime object converted from a protobuf timestamp. | ||
""" | ||
return ( | ||
_EPOCH + | ||
datetime.timedelta( | ||
seconds=timestamp.seconds, | ||
microseconds=(timestamp.nanos / 1000.0), | ||
) | ||
) | ||
|
||
|
||
def _datetime_to_pb_timestamp(when): | ||
"""Convert a datetime object to a Timestamp protobuf. | ||
:type when: :class:`datetime.datetime` | ||
:param when: the datetime to convert | ||
:rtype: :class:`google.protobuf.timestamp_pb2.Timestamp` | ||
:returns: A timestamp protobuf corresponding to the object. | ||
""" | ||
ms_value = _microseconds_from_datetime(when) | ||
seconds, micros = divmod(ms_value, 10**6) | ||
nanos = micros * 10**3 | ||
return timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos) | ||
|
||
|
||
try: | ||
from pytz import UTC # pylint: disable=unused-import,wrong-import-position | ||
except ImportError: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 01c3d91
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for the question about the source of google.protos.timestamp_pb
, this commit LGTM.
I'm missing how that module is being generated under the changes for googleapis#1353: are we relying on it being importable from a dependency, somehow?