-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In preparation for parsing the timestamp from the current operation on create() and update() requests for a cluster.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Utility methods for gcloud_bigtable. | ||
Primarily includes helpers for dealing with low-level | ||
protobuf objects. | ||
""" | ||
|
||
|
||
import datetime | ||
import pytz | ||
|
||
|
||
def _pb_timestamp_to_datetime(timestamp): | ||
"""Convert a Timestamp protobuf to a datetime object. | ||
:type timestamp: :class:`._generated.timestamp_pb.Timestamp` | ||
:param timestamp: A Google returned timestamp protobuf. | ||
:rtype: :class:`datetime.datetime` | ||
:returns: A UTC datetime object converted from a protobuf timestamp. | ||
""" | ||
naive_dt = ( | ||
datetime.datetime.utcfromtimestamp(0) + | ||
datetime.timedelta( | ||
seconds=timestamp.seconds, | ||
microseconds=(timestamp.nanos / 1000.0), | ||
) | ||
) | ||
return naive_dt.replace(tzinfo=pytz.utc) |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import unittest2 | ||
|
||
|
||
class Test__pb_timestamp_to_datetime(unittest2.TestCase): | ||
|
||
def _callFUT(self, timestamp): | ||
from gcloud_bigtable._helpers import _pb_timestamp_to_datetime | ||
return _pb_timestamp_to_datetime(timestamp) | ||
|
||
def test_it(self): | ||
import datetime | ||
import pytz | ||
from gcloud_bigtable._generated.timestamp_pb2 import Timestamp | ||
|
||
# Epoch is midnight on January 1, 1970 ... | ||
dt_stamp = datetime.datetime(1970, month=1, day=1, hour=0, | ||
minute=1, second=1, microsecond=1234, | ||
tzinfo=pytz.utc) | ||
# ... so 1 minute and 1 second after is 61 seconds and 1234 | ||
# microseconds is 1234000 nanoseconds. | ||
timestamp = Timestamp(seconds=61, nanos=1234000) | ||
self.assertEqual(self._callFUT(timestamp), dt_stamp) |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
'httplib2 >= 0.9.1', | ||
'oauth2client >= 1.4.6', | ||
'protobuf >= 3.0.0a3', | ||
'pytz', | ||
'six >= 1.6.1', | ||
] | ||
|
||
|