Repository: googleapis/google-cloud-python

Issue:
datetime property
I not sure is it is a bug but when I have tried save() a entity with a datetime property appear this error.

```
pb_attr, pb_value = helpers.get_protobuf_attribute_and_value(value)
File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/helpers.py", line 39, in get_protobuf_attribute_and_value
name, value = 'timestamp_microseconds', time.mktime(val.timetuple())
NameError: global name 'time' is not defined
```



Discussion:
It definitely is a bug -- just a missing import.

I just created a pull request that fixes the import and adds tests so this shouldn't happen again.

I have tried but seems there are other issue, when saving datat

 toy.update({'name': 'Toy', 'some_int_value': 1234, 'fecha': datetime(2014,12,1,23,59,59)})

---- tracer
  File "gcloud/datastore/entity.py", line 182, in save
    properties=dict(self))
  File "gcloud/datastore/connection.py", line 344, in save_entity
    setattr(prop.value, pb_attr, pb_value)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/python_message.py", line 454, in setter
    type_checker.CheckValue(new_value)
  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/internal/type_checkers.py", line 104, in CheckValue raise TypeError(message)
TypeError: 1417474799.0 has type --type 'float'--, 
but expected one of: (--type 'int', --type 'long'--)


Resolving patch:
diff --git a/gcloud/datastore/helpers.py b/gcloud/datastore/helpers.py
--- a/gcloud/datastore/helpers.py
+++ b/gcloud/datastore/helpers.py
@@ -1,6 +1,6 @@
 """Helper methods for dealing with Cloud Datastore's Protobuf API."""
+import calendar
 from datetime import datetime
-import time
 
 import pytz
 
@@ -37,7 +37,15 @@ def get_protobuf_attribute_and_value(val):
   """
 
   if isinstance(val, datetime):
-    name, value = 'timestamp_microseconds', time.mktime(val.timetuple())
+    name = 'timestamp_microseconds'
+    # If the datetime is naive (no timezone), consider that it was
+    # intended to be UTC and replace the tzinfo to that effect.
+    if not val.tzinfo:
+      val = val.replace(tzinfo=pytz.utc)
+    # Regardless of what timezone is on the value, convert it to UTC.
+    val = val.astimezone(pytz.utc)
+    # Convert the datetime to a microsecond timestamp.
+    value = long(calendar.timegm(val.timetuple()) * 1e6) + val.microsecond
   elif isinstance(val, Key):
     name, value = 'key', val.to_protobuf()
   elif isinstance(val, bool):
@@ -69,8 +77,9 @@ def get_value_from_protobuf(pb):
   """
 
   if pb.value.HasField('timestamp_microseconds_value'):
-    timestamp = pb.value.timestamp_microseconds_value / 1e6
-    return datetime.fromtimestamp(timestamp).replace(tzinfo=pytz.utc)
+    microseconds = pb.value.timestamp_microseconds_value
+    return (datetime.utcfromtimestamp(0) +
+            datetime.timedelta(microseconds=microseconds))
 
   elif pb.value.HasField('key_value'):
     return Key.from_protobuf(pb.value.key_value)


Test patch:
diff --git a/gcloud/datastore/test_helpers.py b/gcloud/datastore/test_helpers.py
--- a/gcloud/datastore/test_helpers.py
+++ b/gcloud/datastore/test_helpers.py
@@ -1,3 +1,4 @@
+import calendar
 from datetime import datetime
 import time
 
@@ -28,7 +29,7 @@ def test_get_protobuf_attribute(self):
             test_value, expected_name, actual_name))
 
   def test_get_protobuf_value(self):
-    now = datetime.now()
+    now = datetime.utcnow()
 
     mapping = (
         (str('string'), 'string'),
@@ -37,7 +38,7 @@ def test_get_protobuf_value(self):
         (long(), int()),
         (float(), float()),
         (bool(), bool()),
-        (now, time.mktime(now.timetuple())),
+        (now, long(calendar.timegm(now.timetuple()) * 1e6 + now.microsecond)),
         (Key(), Key().to_protobuf()),
         )