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

Timestamp.toDate() is generating a different UNIX timestamp (ms) value compared to the Go's version #666

Open
yohanesmario opened this issue Jan 8, 2025 · 1 comment

Comments

@yohanesmario
Copy link

Issue Description

The implementation for Timestamp.toDate() is using .ceil() for the nanos component. This cause a difference of 1ms in this case:

Go's version:

ts := &timestamppb.Timestamp{
  Seconds: int64(0),
  Nanos: int32(1),
}
fmt.Printf("%d\n", ts.AsTime().UnixMilli()) // Will output "0"

Typescript's version:

const ts: Timestamp = {
  seconds: BigInt(0),
  nanos: 1,
}
console.log(Timestamp.toDate(ts).getTime()); // Will output "1"

Proposed Changes

Change this function:

function toDate(message: ${Timestamp}): Date {
return new Date(${PbLong}.from(message.seconds).toNumber() * 1000 + Math.ceil(message.nanos / 1000000));
}

To use Math.floor() instead of Math.ceil() like this:

function toDate(message: ${Timestamp}): Date {
    return new Date(${PbLong}.from(message.seconds).toNumber() * 1000 + Math.floor(message.nanos / 1000000));
}
@yohanesmario
Copy link
Author

I'll try to provide a working PR over the weekend for this if it's a valid concern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant