summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCGantert345 <57003061+CGantert345@users.noreply.github.com>2022-04-11 10:10:04 +0200
committerCGantert345 <57003061+CGantert345@users.noreply.github.com>2022-04-11 10:10:04 +0200
commit32a1132a55a6493941ca8441104e1b9d85688147 (patch)
treeb9edb9e27c571c4ca1e777227ae8cb851cfaad94
parentnullpointer (diff)
downloadUIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.tar
UIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.tar.gz
UIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.tar.bz2
UIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.tar.lz
UIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.tar.xz
UIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.tar.zst
UIC-barcode-32a1132a55a6493941ca8441104e1b9d85688147.zip
-rw-r--r--src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java20
-rw-r--r--src/test/java/org/uic/barcode/test/TimeStampTest.java50
2 files changed, 61 insertions, 9 deletions
diff --git a/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java b/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java
index c2cd8fa..2f54da3 100644
--- a/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java
+++ b/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java
@@ -1,11 +1,11 @@
package org.uic.barcode.dynamicContent.fdc1;
import java.time.Instant;
-import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
-import java.time.temporal.ChronoField;
+import java.util.Calendar;
import java.util.Date;
+import java.util.TimeZone;
import org.uic.barcode.asn1.datatypes.FieldOrder;
import org.uic.barcode.asn1.datatypes.IntRange;
@@ -41,18 +41,22 @@ public class TimeStamp {
* Instantiates a new time stamp and sets the time-stamp to now.
*/
public TimeStamp() {
- ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
- day = new Long(now.get(ChronoField.DAY_OF_YEAR));
- secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY));
+ setNow();
}
/**
* Sets the the time-stamp to now.
*/
public void setNow() {
- ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
- day = new Long(now.get(ChronoField.DAY_OF_YEAR));
- secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY));
+ Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+ day = (long) c.get(Calendar.DAY_OF_YEAR);
+ long now = c.getTimeInMillis();
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ long passed = now - c.getTimeInMillis();
+ secondOfDay = passed / 1000;
}
/**
diff --git a/src/test/java/org/uic/barcode/test/TimeStampTest.java b/src/test/java/org/uic/barcode/test/TimeStampTest.java
index c15e0fb..f03a63c 100644
--- a/src/test/java/org/uic/barcode/test/TimeStampTest.java
+++ b/src/test/java/org/uic/barcode/test/TimeStampTest.java
@@ -1,5 +1,8 @@
package org.uic.barcode.test;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@@ -36,6 +39,51 @@ public class TimeStampTest {
}
-
+ @Test public void testDateConversion2() {
+
+
+ Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+ long day = (long) c.get(Calendar.DAY_OF_YEAR);
+ long now = c.getTimeInMillis();
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ long passed = now - c.getTimeInMillis();
+ long secondOfDay = passed / 1000;
+
+ TimeStamp ts = new TimeStamp();
+ assert (ts.day == day);
+ assert (ts.secondOfDay >= secondOfDay - 1);
+ assert (ts.secondOfDay <= secondOfDay + 1);
+
+
+ }
+
+ @Test public void testDateConversion3() {
+
+ //implemntation not available on older android versions:
+ ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
+ long day = new Long(now.get(ChronoField.DAY_OF_YEAR));
+ long secondOfDay = new Long(now.get(ChronoField.SECOND_OF_DAY));
+
+ //alternative implementation
+ Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+ long day2 = (long) c.get(Calendar.DAY_OF_YEAR);
+ long now2 = c.getTimeInMillis();
+ c.set(Calendar.HOUR_OF_DAY, 0);
+ c.set(Calendar.MINUTE, 0);
+ c.set(Calendar.SECOND, 0);
+ c.set(Calendar.MILLISECOND, 0);
+ long passed = now2 - c.getTimeInMillis();
+ long secondOfDay2 = passed / 1000;
+
+
+ assert (day2 == day);
+ assert (secondOfDay2 >= secondOfDay - 1);
+ assert (secondOfDay2 <= secondOfDay + 1);
+
+
+ }
}