Skip to content

Commit

Permalink
add create(Instant,...)
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Dec 3, 2021
1 parent 3cc4881 commit 7cf42e4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/txn/src/main/java/org/jpos/transaction/TxnId.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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
*
Expand Down
11 changes: 11 additions & 0 deletions modules/txn/src/test/java/org/jpos/transaction/TxnIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 7cf42e4

Please sign in to comment.