File tree Expand file tree Collapse file tree 2 files changed +46
-4
lines changed
src/test/java/org/javawebstack/orm/test Expand file tree Collapse file tree 2 files changed +46
-4
lines changed Original file line number Diff line number Diff line change 88import org .javawebstack .orm .exception .ORMConfigurationException ;
99import org .junit .jupiter .api .Test ;
1010
11+ import javax .swing .text .TabSet ;
1112import java .sql .Timestamp ;
1213import java .time .Instant ;
1314
15+ import static org .javawebstack .orm .test .shared .util .TimestampUtil .diffInNanoseconds ;
1416import static org .junit .jupiter .api .Assertions .*;
1517
1618public class TypesTest extends ORMTestCase {
@@ -58,10 +60,7 @@ public void testFields() throws ORMConfigurationException {
5860 assertEquals (model .exampleDouble , 123456789.1234567890D );
5961 assertEquals (model .exampleLong , 999999999999999999L );
6062 assertEquals (model .exampleLongPrimitive , 999999999999999999L );
61-
62- // TODO: Use
63- assertNotNull (model .timestampTest );
64-
63+ assertTrue (diffInNanoseconds (timestamp , model .timestampTest ) < 1 );
6564 model .exampleNull = "Text" ;
6665 model .save ();
6766 model = Repo .get (ExampleModel .class ).get (id );
Original file line number Diff line number Diff line change 1+ package org .javawebstack .orm .test .shared .util ;
2+
3+ import java .sql .Timestamp ;
4+
5+ /*
6+ * The original author is Aaron Digulla
7+ * https://stackoverflow.com/a/582487
8+ */
9+ public class TimestampUtil {
10+
11+ public static Double diffInNanoseconds (java .util .Date t1 , java .util .Date t2 )
12+ {
13+ // Make sure the result is always > 0
14+ if (t1 .compareTo (t2 ) < 0 )
15+ {
16+ java .util .Date tmp = t1 ;
17+ t1 = t2 ;
18+ t2 = tmp ;
19+ }
20+
21+ // Timestamps mix milli and nanoseconds in the API, so we have to separate the two
22+ long diffSeconds = (t1 .getTime () / 1000 ) - (t2 .getTime () / 1000 );
23+ // For normals dates, we have millisecond precision
24+ int nano1 = ((int ) t1 .getTime () % 1000 ) * 1000000 ;
25+ // If the parameter is a Timestamp, we have additional precision in nanoseconds
26+ if (t1 instanceof Timestamp )
27+ nano1 = ((Timestamp )t1 ).getNanos ();
28+ int nano2 = ((int ) t2 .getTime () % 1000 ) * 1000000 ;
29+ if (t2 instanceof Timestamp )
30+ nano2 = ((Timestamp )t2 ).getNanos ();
31+
32+ int diffNanos = nano1 - nano2 ;
33+ if (diffNanos < 0 )
34+ {
35+ // Borrow one second
36+ diffSeconds --;
37+ diffNanos += 1000000000 ;
38+ }
39+
40+ return diffSeconds * 1000000000.0 + diffNanos / 1000.0 ;
41+
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments