You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2018/02/16 21:49:51 UTC

[incubator-plc4x] branch master updated: + flipped win time methods as its more usual to work in java with dates since 1970

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

sruehl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git


The following commit(s) were added to refs/heads/master by this push:
     new 496da15  + flipped win time methods as its more usual to work in java with dates   since 1970
496da15 is described below

commit 496da153a18b38200efaef53166e9b5167618972
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Feb 16 22:49:47 2018 +0100

    + flipped win time methods as its more usual to work in java with dates
      since 1970
---
 .../java/ads/api/commands/types/TimeStamp.java     | 27 +++++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java
index c490223..4a2981c 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/api/commands/types/TimeStamp.java
@@ -77,6 +77,10 @@ public class TimeStamp extends ByteValue {
     }
 
     public static TimeStamp of(BigInteger value) {
+        return new TimeStamp(javaToWinTime(value));
+    }
+
+    public static TimeStamp ofWinTime(BigInteger value) {
         return new TimeStamp(value);
     }
 
@@ -84,14 +88,17 @@ public class TimeStamp extends ByteValue {
         return of(BigInteger.valueOf(value));
     }
 
+    public static TimeStamp ofWinTime(long value) {
+        return of(javaToWinTime(BigInteger.valueOf(value)));
+    }
+
     public static TimeStamp of(byte... values) {
         return new TimeStamp(values);
     }
 
     public static TimeStamp of(Date timestamp) {
-        BigInteger timeMillisSince19700101 = BigInteger.valueOf(timestamp.getTime());
-        BigInteger timeMillisSince16010101 = EPOCH_DIFF_IN_MILLIS.add(timeMillisSince19700101);
-        return new TimeStamp(timeMillisSince16010101.multiply(BigInteger.valueOf(10_000)));
+        BigInteger winStamp = javaToWinTime(BigInteger.valueOf(timestamp.getTime()));
+        return new TimeStamp(winStamp);
     }
 
     public static TimeStamp of(ByteBuf byteBuf) {
@@ -105,9 +112,17 @@ public class TimeStamp extends ByteValue {
     }
 
     public Date getAsDate() {
-        BigInteger timeMillisSince16010101 = bigIntegerValue.divide(BigInteger.valueOf(10_000));
-        BigInteger timeMillisSince19700101 = timeMillisSince16010101.subtract(EPOCH_DIFF_IN_MILLIS);
-        return new Date(timeMillisSince19700101.longValue());
+        return new Date(winTimeToJava(bigIntegerValue).longValue());
+    }
+
+    public static BigInteger javaToWinTime(BigInteger timeMillisSince19700101) {
+        BigInteger timeMillisSince16010101 = EPOCH_DIFF_IN_MILLIS.add(timeMillisSince19700101);
+        return timeMillisSince16010101.multiply(BigInteger.valueOf(10_000));
+    }
+
+    public static BigInteger winTimeToJava(BigInteger winTime) {
+        BigInteger timeMillisSince16010101 = winTime.divide(BigInteger.valueOf(10_000));
+        return timeMillisSince16010101.subtract(EPOCH_DIFF_IN_MILLIS);
     }
 
     @Override

-- 
To stop receiving notification emails like this one, please contact
sruehl@apache.org.