Skip to content

Commit

Permalink
[Java] unique timestamp in seconds (#201)
Browse files Browse the repository at this point in the history
* feat: unique timestampMs

* fixing time

---------

Co-authored-by: ArtursK <[email protected]>
  • Loading branch information
arifBurakDemiray and ArtursKadikis authored Jan 3, 2024
1 parent ed39c26 commit 6f42c9d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sdk-java/src/main/java/ly/count/sdk/java/internal/TimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class TimeUtils {

protected static final Double NS_IN_SECOND = 1000000000.0d;
protected static final Double NS_IN_MS = 1000000.0d;
protected static final Double MS_IN_SECOND = 1000d;
protected static final long MS_IN_SECOND = 1000;
private static final UniqueTimeGenerator uniqueTimer = new UniqueTimeGenerator();

public static class Instant {
Expand Down Expand Up @@ -62,6 +62,16 @@ public static synchronized long uniqueTimestampMs() {
return uniqueTimer.timestamp();
}

/**
* Wraps {@link System#currentTimeMillis()} to always return different value, even within
* same millisecond and even when time changes. Works in a limited window of 10 timestamps for now.
*
* @return unique time in seconds
*/
public static synchronized long uniqueTimestampS() {
return uniqueTimestampMs() / MS_IN_SECOND;
}

/**
* Wraps {@link System#currentTimeMillis()} and returns it
*
Expand Down

0 comments on commit 6f42c9d

Please sign in to comment.