Skip to content

Deserialize Timestamps (google.protobuf.Timestamp) into Date objects #43

@tatethurston

Description

@tatethurston

Today Protoscript deserializes timestamps into an object in the shape { seconds: bigint, nanoseconds: number }.

We could instead deserialize timestamps into JavaScript Date objects. The only unclear pieces are:

  1. JS Date objects lack some of the precision of the object form. Specifically, JS Date's can handle up to 3 digit of ms precision, vs the timestamp object's 9 digits of nanosecond precision.
  2. This would be a breaking change for user's source code. The wire format would be unchanged.

The above could be mitigated with an opt-in or opt-out flag. I'm inclined to make this the default behavior, and potentially consider an opt-out flag.

Altternatively, ProtoScript could create a subclass Timestamp from Date. That subclass could then be used anywhere a Date is used, and optionally provide the nanoseconds for code paths aware of Timestamp. Eg:

class Timestamp extends Date {
  #nanoseconds = 0;
  
  setNanoseconds(nanoseconds: number) {
    this.#nanoseconds = nanoseconds;
  }
  getNanoseconds() {
    return this.#nanoseconds;
  }
}

Or something more clever using the constructor arguments -- but I'm considered about edge cases and maintenance there.

Please comment or like this issue if you're a user and want to see this change land.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions