You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/09/04 13:22:17 UTC

[tinkerpop] 07/40: Fixed the graphbinary g; Date serialization - wasn't account for timezone stuff properly (still)

This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch TINKERPOP-2279
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git

commit a199fa122dbcabd68be9e870b180f53368d3d093
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Fri Jul 26 11:43:47 2019 -0400

    Fixed the graphbinary g;Date serialization - wasn't account for timezone stuff properly (still)
---
 .../jython/gremlin_python/structure/io/graphbinaryV1.py | 17 ++++++++---------
 .../jython/tests/structure/io/test_graphbinaryV1.py     |  3 ++-
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
index 33342de..f84d2a0 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphbinaryV1.py
@@ -17,6 +17,7 @@ specific language governing permissions and limitations
 under the License.
 """
 import datetime
+import calendar
 import struct
 import time
 import uuid
@@ -185,15 +186,13 @@ class DateIO(_GraphBinaryTypeIO):
 
     @classmethod
     def dictify(cls, obj, writer):
-        if six.PY3:
-            pts = (obj - datetime.datetime(1970, 1, 1)) / datetime.timedelta(seconds=1)
-        else:
-            # Hack for legacy Python - timestamp() in Python 3.3
-            pts = (time.mktime(obj.timetuple()) + obj.microsecond / 1e6) - \
-                  (time.mktime(datetime.datetime(1970, 1, 1).timetuple()))
-
-        # Java timestamp expects milliseconds - have to use int because of legacy Python
-        ts = int(round(pts * 1000))
+        try:
+            timestamp_seconds = calendar.timegm(obj.utctimetuple())
+            pts = timestamp_seconds * 1e3 + getattr(obj, 'microsecond', 0) / 1e3
+        except AttributeError:
+            pts = calendar.timegm(obj.timetuple()) * 1e3
+            
+        ts = int(round(pts * 100))
         return cls.as_bytes(cls.graphbinary_type, None, struct.pack(">q", ts))
 
     @classmethod
diff --git a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
index 5775f3a..db58545 100644
--- a/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
+++ b/gremlin-python/src/main/jython/tests/structure/io/test_graphbinaryV1.py
@@ -18,6 +18,7 @@ under the License.
 """
 
 import datetime
+import calendar
 import time
 import uuid
 import math
@@ -54,7 +55,7 @@ class TestGraphSONWriter(object):
         assert x == output
 
     def test_date(self):
-        x = datetime.datetime(2016, 12, 14, 16, 14, 36, 295000)
+        x = calendar.timegm(datetime.datetime(2016, 12, 14, 16, 14, 36, 295000).utctimetuple())
         output = self.graphbinary_reader.readObject(self.graphbinary_writer.writeObject(x))
         assert x == output