tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jorg...@apache.org
Subject tinkerpop git commit: Gremlin.Net: Use DateTimeOffset for g:Date and g:Timestamp
Date Fri, 01 Dec 2017 14:04:33 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/TINKERPOP-1745 [created] f11530775


Gremlin.Net: Use DateTimeOffset for g:Date and g:Timestamp


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/f1153077
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/f1153077
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/f1153077

Branch: refs/heads/TINKERPOP-1745
Commit: f1153077546bc1226309e1d04273e7912a744532
Parents: be86be0
Author: Jorge Bay Gondra <jorgebaygondra@gmail.com>
Authored: Fri Dec 1 15:04:26 2017 +0100
Committer: Jorge Bay Gondra <jorgebaygondra@gmail.com>
Committed: Fri Dec 1 15:04:26 2017 +0100

----------------------------------------------------------------------
 .../Structure/IO/GraphSON/DateDeserializer.cs   | 43 --------------------
 .../Structure/IO/GraphSON/DateSerializer.cs     | 16 +++++---
 .../Structure/IO/GraphSON/GraphSONReader.cs     |  4 +-
 .../Structure/IO/GraphSON/GraphSONWriter.cs     |  2 +-
 .../IO/GraphSON/GraphSONReaderTests.cs          | 16 ++++----
 .../IO/GraphSON/GraphSONWriterTests.cs          |  6 +--
 .../Structure/IO/GraphSON/TestUtils.cs          |  8 ++--
 7 files changed, 28 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateDeserializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateDeserializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateDeserializer.cs
deleted file mode 100644
index d8879f8..0000000
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateDeserializer.cs
+++ /dev/null
@@ -1,43 +0,0 @@
-#region License
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-
-#endregion
-
-using System;
-using Newtonsoft.Json.Linq;
-
-namespace Gremlin.Net.Structure.IO.GraphSON
-{
-    internal class DateDeserializer : IGraphSONDeserializer
-    {
-        public dynamic Objectify(JToken graphsonObject, GraphSONReader reader)
-        {
-            var javaTimestamp = graphsonObject.ToObject<long>();
-            return FromJavaTime(javaTimestamp);
-        }
-
-        private DateTime FromJavaTime(long javaTimestamp)
-        {
-            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
-            return epoch.AddMilliseconds(javaTimestamp);
-        }
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateSerializer.cs
index 1438e02..3333f0c 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateSerializer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/DateSerializer.cs
@@ -23,21 +23,25 @@
 
 using System;
 using System.Collections.Generic;
+using Newtonsoft.Json.Linq;
 
 namespace Gremlin.Net.Structure.IO.GraphSON
 {
-    internal class DateSerializer : IGraphSONSerializer
+    internal class DateSerializer : IGraphSONSerializer, IGraphSONDeserializer
     {
+        private static readonly DateTimeOffset UnixStart = new DateTimeOffset(1970, 1, 1,
0, 0, 0, 0, TimeSpan.Zero);
+        
         public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter
writer)
         {
-            DateTime dateTime = objectData;
-            return GraphSONUtil.ToTypedValue("Date", ToJavaTimestamp(dateTime));
+            DateTimeOffset value = objectData;
+            var ticks = (value - UnixStart).Ticks;
+            return GraphSONUtil.ToTypedValue("Date", ticks / TimeSpan.TicksPerMillisecond);
         }
 
-        private long ToJavaTimestamp(DateTime dateTime)
+        public dynamic Objectify(JToken graphsonObject, GraphSONReader reader)
         {
-            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
-            return Convert.ToInt64((dateTime - epoch).TotalMilliseconds);
+            var milliseconds = graphsonObject.ToObject<long>();
+            return UnixStart.AddTicks(TimeSpan.TicksPerMillisecond * milliseconds);
         }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
index aa1fc48..6402c43 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
@@ -42,8 +42,8 @@ namespace Gremlin.Net.Structure.IO.GraphSON
                 {"g:Float", new FloatConverter()},
                 {"g:Double", new DoubleConverter()},
                 {"g:UUID", new UuidDeserializer()},
-                {"g:Date", new DateDeserializer()},
-                {"g:Timestamp", new DateDeserializer()},
+                {"g:Date", new DateSerializer()},
+                {"g:Timestamp", new DateSerializer()},
                 {"g:Vertex", new VertexDeserializer()},
                 {"g:Edge", new EdgeDeserializer()},
                 {"g:Property", new PropertyDeserializer()},

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
index 3c17d14..4d99864 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
@@ -50,7 +50,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
                 {typeof(float), new FloatConverter()},
                 {typeof(double), new DoubleConverter()},
                 {typeof(Guid), new UuidSerializer()},
-                {typeof(DateTime), new DateSerializer()},
+                {typeof(DateTimeOffset), new DateSerializer()},
                 {typeof(Type), new ClassSerializer()},
                 {typeof(Enum), new EnumSerializer()},
                 {typeof(TraversalPredicate), new TraversalPredicateSerializer()},

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
index 747a94e..cb70530 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
@@ -72,15 +72,15 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
         }
 
         [Fact]
-        public void ShouldDeserializeDateToDateTime()
+        public void ShouldDeserializeDateToDateTimeOffset()
         {
             var graphSon = "{\"@type\":\"g:Date\",\"@value\":1475583442552}";
             var reader = CreateStandardGraphSONReader();
 
-            DateTime readDateTime = reader.ToObject(JObject.Parse(graphSon));
+            DateTimeOffset deserializedValue = reader.ToObject(JObject.Parse(graphSon));
 
-            var expectedDateTime = TestUtils.FromJavaTime(1475583442552);
-            Assert.Equal(expectedDateTime, readDateTime);
+            var expectedDateTimeOffset = TestUtils.FromJavaTime(1475583442552);
+            Assert.Equal(expectedDateTimeOffset, deserializedValue);
         }
 
         [Fact]
@@ -211,15 +211,15 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
         }
 
         [Fact]
-        public void ShouldDeserializeTimestampToDateTime()
+        public void ShouldDeserializeTimestampToDateTimeOffset()
         {
             var graphSon = "{\"@type\":\"g:Timestamp\",\"@value\":1475583442558}";
             var reader = CreateStandardGraphSONReader();
 
-            DateTime readDateTime = reader.ToObject(JObject.Parse(graphSon));
+            DateTimeOffset deserializedValue = reader.ToObject(JObject.Parse(graphSon));
 
-            var expectedDateTime = TestUtils.FromJavaTime(1475583442558);
-            Assert.Equal(expectedDateTime, readDateTime);
+            var expectedDateTimeOffset = TestUtils.FromJavaTime(1475583442558);
+            Assert.Equal(expectedDateTimeOffset, deserializedValue);
         }
 
         [Fact]

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
index 4cd831f..1f5e087 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
@@ -145,12 +145,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
         }
 
         [Fact]
-        public void ShouldSerializeDateTime()
+        public void ShouldSerializeDateTimeOffset()
         {
             var writer = CreateStandardGraphSONWriter();
-            var dateTime = TestUtils.FromJavaTime(1475583442552);
+            var dateTimeOffset = TestUtils.FromJavaTime(1475583442552);
 
-            var graphSon = writer.WriteObject(dateTime);
+            var graphSon = writer.WriteObject(dateTimeOffset);
 
             const string expected = "{\"@type\":\"g:Date\",\"@value\":1475583442552}";
             Assert.Equal(expected, graphSon);

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f1153077/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/TestUtils.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/TestUtils.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/TestUtils.cs
index 7ed7542..78e2386 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/TestUtils.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/TestUtils.cs
@@ -25,12 +25,12 @@ using System;
 
 namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
 {
-    internal class TestUtils
+    internal static class TestUtils
     {
-        public static DateTime FromJavaTime(long javaTimestamp)
+        public static DateTimeOffset FromJavaTime(long milliseconds)
         {
-            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
-            return epoch.AddMilliseconds(javaTimestamp);
+            var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
+            return epoch.AddTicks(TimeSpan.TicksPerMillisecond * milliseconds);
         }
     }
 }
\ No newline at end of file


Mime
View raw message