Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1125 NatsJetStreamMetaData timestamp timezone #1126

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/java/io/nats/client/impl/NatsJetStreamMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

package io.nats.client.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

/**
Expand Down Expand Up @@ -103,9 +105,10 @@ else if (parts.length >= 11) {
// not so clever way to separate nanos from seconds
long tsi = Long.parseLong(parts[streamIndex + 5]);
long seconds = tsi / NANO_FACTOR;
int nanos = (int) (tsi - ((tsi / NANO_FACTOR) * NANO_FACTOR));
LocalDateTime ltd = LocalDateTime.ofEpochSecond(seconds, nanos, OffsetDateTime.now().getOffset());
timestamp = ZonedDateTime.of(ltd, ZoneId.systemDefault()); // I think this is safe b/c the zone should match local
int nanos = (int) (tsi - (seconds * NANO_FACTOR));
Instant utcInstant = Instant.ofEpochSecond(seconds, nanos);
OffsetDateTime utcOffsetDT = OffsetDateTime.ofInstant(utcInstant, ZoneOffset.UTC);
timestamp = utcOffsetDT.atZoneSameInstant(ZoneId.systemDefault());

this.pending = hasPending ? Long.parseLong(parts[streamIndex + 6]) : -1L;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package io.nats.client.impl;

import io.nats.client.Message;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -52,9 +54,13 @@ private void validateMeta(boolean hasPending, boolean hasDomainHashToken, NatsJe
assertEquals(2, meta.streamSequence());
assertEquals(3, meta.consumerSequence());

assertEquals(2020, meta.timestamp().getYear());
assertEquals(6, meta.timestamp().getMinute());
assertEquals(113260000, meta.timestamp().getNano());
ZonedDateTime localTs = meta.timestamp();
assertEquals(2020, localTs.getYear());
assertEquals(6, localTs.getMinute());
assertEquals(113260000, localTs.getNano());

ZonedDateTime utcTs = localTs.withZoneSameInstant(ZoneId.of("UTC"));
assertEquals(0, utcTs.getHour());

assertEquals(hasPending ? 4L : -1L, meta.pendingCount());

Expand Down
Loading