Skip to content

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

@yohanesmario

Description

@yohanesmario

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));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions