Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit be6e3bb

Browse filesBrowse files
committed
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.
1 parent 02553e6 commit be6e3bb
Copy full SHA for be6e3bb

File tree

Expand file treeCollapse file tree

4 files changed

+77
-41
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+77
-41
lines changed

‎gcloud/_helpers.py

Copy file name to clipboardExpand all lines: gcloud/_helpers.py
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import socket
2424
import sys
2525

26+
from google.protobuf import timestamp_pb2
2627
import six
2728
from six.moves.http_client import HTTPConnection # pylint: disable=F0401
2829

@@ -354,6 +355,39 @@ def _to_bytes(value, encoding='ascii'):
354355
raise TypeError('%r could not be converted to bytes' % (value,))
355356

356357

358+
def _pb_timestamp_to_datetime(timestamp):
359+
"""Convert a Timestamp protobuf to a datetime object.
360+
361+
:type timestamp: :class:`google.protobuf.timestamp_pb2.Timestamp`
362+
:param timestamp: A Google returned timestamp protobuf.
363+
364+
:rtype: :class:`datetime.datetime`
365+
:returns: A UTC datetime object converted from a protobuf timestamp.
366+
"""
367+
return (
368+
_EPOCH +
369+
datetime.timedelta(
370+
seconds=timestamp.seconds,
371+
microseconds=(timestamp.nanos / 1000.0),
372+
)
373+
)
374+
375+
376+
def _datetime_to_pb_timestamp(when):
377+
"""Convert a datetime object to a Timestamp protobuf.
378+
379+
:type when: :class:`datetime.datetime`
380+
:param when: the datetime to convert
381+
382+
:rtype: :class:`google.protobuf.timestamp_pb2.Timestamp`
383+
:returns: A timestamp protobuf corresponding to the object.
384+
"""
385+
ms_value = _microseconds_from_datetime(when)
386+
seconds, micros = divmod(ms_value, 10**6)
387+
nanos = micros * 10**3
388+
return timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos)
389+
390+
357391
try:
358392
from pytz import UTC # pylint: disable=unused-import,wrong-import-position
359393
except ImportError:

‎gcloud/bigtable/cluster.py

Copy file name to clipboardExpand all lines: gcloud/bigtable/cluster.py
+1-20Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
"""User friendly container for Google Cloud Bigtable Cluster."""
1616

1717

18-
import datetime
1918
import re
2019

2120
from google.longrunning import operations_pb2
2221

23-
from gcloud._helpers import _EPOCH
22+
from gcloud._helpers import _pb_timestamp_to_datetime
2423
from gcloud.bigtable._generated import bigtable_cluster_data_pb2 as data_pb2
2524
from gcloud.bigtable._generated import (
2625
bigtable_cluster_service_messages_pb2 as messages_pb2)
@@ -93,24 +92,6 @@ def _prepare_create_request(cluster):
9392
)
9493

9594

96-
def _pb_timestamp_to_datetime(timestamp):
97-
"""Convert a Timestamp protobuf to a datetime object.
98-
99-
:type timestamp: :class:`google.protobuf.timestamp_pb2.Timestamp`
100-
:param timestamp: A Google returned timestamp protobuf.
101-
102-
:rtype: :class:`datetime.datetime`
103-
:returns: A UTC datetime object converted from a protobuf timestamp.
104-
"""
105-
return (
106-
_EPOCH +
107-
datetime.timedelta(
108-
seconds=timestamp.seconds,
109-
microseconds=(timestamp.nanos / 1000.0),
110-
)
111-
)
112-
113-
11495
def _parse_pb_any_to_native(any_val, expected_type=None):
11596
"""Convert a serialized "google.protobuf.Any" value to actual type.
11697

‎gcloud/bigtable/test_cluster.py

Copy file name to clipboardExpand all lines: gcloud/bigtable/test_cluster.py
-21Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -686,27 +686,6 @@ def test_it(self):
686686
self.assertEqual(request_pb.cluster.serve_nodes, serve_nodes)
687687

688688

689-
class Test__pb_timestamp_to_datetime(unittest2.TestCase):
690-
691-
def _callFUT(self, timestamp):
692-
from gcloud.bigtable.cluster import _pb_timestamp_to_datetime
693-
return _pb_timestamp_to_datetime(timestamp)
694-
695-
def test_it(self):
696-
import datetime
697-
from google.protobuf.timestamp_pb2 import Timestamp
698-
from gcloud._helpers import UTC
699-
700-
# Epoch is midnight on January 1, 1970 ...
701-
dt_stamp = datetime.datetime(1970, month=1, day=1, hour=0,
702-
minute=1, second=1, microsecond=1234,
703-
tzinfo=UTC)
704-
# ... so 1 minute and 1 second after is 61 seconds and 1234
705-
# microseconds is 1234000 nanoseconds.
706-
timestamp = Timestamp(seconds=61, nanos=1234000)
707-
self.assertEqual(self._callFUT(timestamp), dt_stamp)
708-
709-
710689
class Test__parse_pb_any_to_native(unittest2.TestCase):
711690

712691
def _callFUT(self, any_val, expected_type=None):

‎gcloud/test__helpers.py

Copy file name to clipboardExpand all lines: gcloud/test__helpers.py
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,48 @@ def test_with_nonstring_type(self):
484484
self.assertRaises(TypeError, self._callFUT, value)
485485

486486

487+
class Test__pb_timestamp_to_datetime(unittest2.TestCase):
488+
489+
def _callFUT(self, timestamp):
490+
from gcloud._helpers import _pb_timestamp_to_datetime
491+
return _pb_timestamp_to_datetime(timestamp)
492+
493+
def test_it(self):
494+
import datetime
495+
from google.protobuf.timestamp_pb2 import Timestamp
496+
from gcloud._helpers import UTC
497+
498+
# Epoch is midnight on January 1, 1970 ...
499+
dt_stamp = datetime.datetime(1970, month=1, day=1, hour=0,
500+
minute=1, second=1, microsecond=1234,
501+
tzinfo=UTC)
502+
# ... so 1 minute and 1 second after is 61 seconds and 1234
503+
# microseconds is 1234000 nanoseconds.
504+
timestamp = Timestamp(seconds=61, nanos=1234000)
505+
self.assertEqual(self._callFUT(timestamp), dt_stamp)
506+
507+
508+
class Test__datetime_to_pb_timestamp(unittest2.TestCase):
509+
510+
def _callFUT(self, when):
511+
from gcloud._helpers import _datetime_to_pb_timestamp
512+
return _datetime_to_pb_timestamp(when)
513+
514+
def test_it(self):
515+
import datetime
516+
from google.protobuf.timestamp_pb2 import Timestamp
517+
from gcloud._helpers import UTC
518+
519+
# Epoch is midnight on January 1, 1970 ...
520+
dt_stamp = datetime.datetime(1970, month=1, day=1, hour=0,
521+
minute=1, second=1, microsecond=1234,
522+
tzinfo=UTC)
523+
# ... so 1 minute and 1 second after is 61 seconds and 1234
524+
# microseconds is 1234000 nanoseconds.
525+
timestamp = Timestamp(seconds=61, nanos=1234000)
526+
self.assertEqual(self._callFUT(dt_stamp), timestamp)
527+
528+
487529
class _AppIdentity(object):
488530

489531
def __init__(self, app_id):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.