Skip to content

Commit

Permalink
Adding a timestamp converter.
Browse files Browse the repository at this point in the history
In preparation for parsing the timestamp from the current
operation on create() and update() requests for a cluster.
  • Loading branch information
dhermes committed Jul 26, 2015
1 parent 5fdcd61 commit a939bd1
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
42 changes: 42 additions & 0 deletions gcloud_bigtable/_helpers.py
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)
37 changes: 37 additions & 0 deletions gcloud_bigtable/test__helpers.py
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)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'httplib2 >= 0.9.1',
'oauth2client >= 1.4.6',
'protobuf >= 3.0.0a3',
'pytz',
'six >= 1.6.1',
]

Expand Down

0 comments on commit a939bd1

Please sign in to comment.