diff --git a/modules/txn/src/main/java/org/jpos/transaction/TxnId.java b/modules/txn/src/main/java/org/jpos/transaction/TxnId.java index 79f5194ff7..92fde8e9ed 100644 --- a/modules/txn/src/main/java/org/jpos/transaction/TxnId.java +++ b/modules/txn/src/main/java/org/jpos/transaction/TxnId.java @@ -22,6 +22,9 @@ import org.joda.time.DateTimeZone; import java.io.File; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -34,6 +37,7 @@ public class TxnId { private static final long NMUL = 100000L; private static final long MAX_VALUE = Long.parseLong("zzzzzzzzzzzz", 36); private static Pattern pattern = Pattern.compile("^([\\d]{3})-([\\d]{3})-([\\d]{5})-([\\d]{3})-([\\d]{5})$"); + private static ZoneId UTC = ZoneId.of("UTC"); private TxnId() { super(); @@ -140,6 +144,13 @@ public static TxnId create (DateTime dt, int node, long transactionId) { return id.init (dt.getYear()-2000, dt.getDayOfYear(), dt.getSecondOfDay(), node, transactionId); } + public static TxnId create (Instant instant, int node, long transactionId) { + TxnId id = new TxnId(); + ZonedDateTime utc = instant.atZone(UTC); + int seconds = (utc.getHour() * 3600) + (utc.getMinute()*60) + utc.getSecond(); + return id.init (utc.getYear()-2000, utc.getDayOfYear(), seconds, node, transactionId); + } + /** * @param idString TxnId in YYYY-DDD-SSS-NN-TTTTT format * diff --git a/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java b/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java index 4510fe17c5..ae620b4383 100644 --- a/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java +++ b/modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java @@ -21,6 +21,8 @@ import org.joda.time.DateTime; import org.junit.jupiter.api.Test; +import java.time.Instant; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; @@ -76,4 +78,13 @@ public void testMaxValue() { assertEquals(txnId2.toString(), txnId3.toString()); assertEquals(12, txnId.toRrn().length()); } + + @Test + public void testJodaTimeCompatibility() { + DateTime now = DateTime.now(); + Instant instant = now.toDate().toInstant(); + TxnId txnId1 = TxnId.create(now, 0, 1L); + TxnId txnId2 = TxnId.create(instant, 0, 1L); + assertEquals(txnId1, txnId2); + } }