Protobuf.js: google.protobuf.Timestamp

Created on 31 May 2016  路  11Comments  路  Source: protobufjs/protobuf.js

Hello,

I would like to transfer a Javascript Date object. It seem google.protobuf.Timestamp is the 'standard' way of formatting it. Is it possible to register a decode/encode function on the Timestamp message, so that I can convert JS Date to Timestamp and back?

Most helpful comment

I would like to do the following.

message User {
  google.protobuf.Timestamp created = 1;
}
var user = {
created:Date.now(),
}
var b = new User(user).encode();

Without the need to do something like the following on each Date object.

var now = Math.floor(Date.now().getTime()/1000);
var user = {
created:{seconds:now},
}

Can I register a encode/decode function to the Timestamp message?

All 11 comments

Reference: https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto

Looks like this is just a wrapper for "seconds of UTC time since Unix epoch". It should be safe to include this file in your project and to work with Timestamp#seconds.

Minimal proto:

syntax = "proto3";
package google.protobuf;
message Timestamp {
  int64 seconds = 1;
  int32 nanos = 2;
}

I would like to do the following.

message User {
  google.protobuf.Timestamp created = 1;
}
var user = {
created:Date.now(),
}
var b = new User(user).encode();

Without the need to do something like the following on each Date object.

var now = Math.floor(Date.now().getTime()/1000);
var user = {
created:{seconds:now},
}

Can I register a encode/decode function to the Timestamp message?

I now this was closed but wanted to know if you found a solution? I'm trying to do the same thing of using a Timestamp message and can't find a way to make it work. Thanks.

I'm also wondering about this; Looking at Google's own implementation the JSON representation of the google.protobuf.Timestamp message type is the string representation...

From https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto

JSON Mapping

// In JSON format, the Timestamp type is encoded as a string in the
// RFC 3339 format. That is, the
// format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z"
// where {year} is always expressed using four digits while {month}, {day},
// {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional
// seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution),
// are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone
// is required. A proto3 JSON serializer should always use UTC (as indicated by
// "Z") when printing the Timestamp type and a proto3 JSON parser should be
// able to accept both UTC and other timezones (as indicated by an offset).
//
// For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past
// 01:30 UTC on January 15, 2017.
//
// In JavaScript, one can convert a Date object to this format using the
// standard toISOString()
// method. In Python, a standard datetime.datetime object can be converted
// to this format using strftime
// with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one
// can use the Joda Time's ISODateTimeFormat.dateTime() to obtain a formatter capable of generating timestamps in this format.
//

Can this be reopened? Can't make it work :-|

Can this be reopened? Can't make it work :-|

Hey.
I hope I am not too late but you can do something link this.

if (window.proto) {
  const proto = window.proto;
  const timeMS = Date.now();

  // Create timestamp
  const timestamp = new proto.google.protobuf.Timestamp()
  timestamp.setSeconds(timeMS / 1000);
  timestamp.setNanos((timeMS % 1000) * 1e6);
}

I can not use timestamp.setSeconds and timestamp.setNanos, so I used the following way:

const timestamp = google.protobuf.Timestamp.fromObject({
  seconds: timeMS / 1000,
  nanos: (timeMS % 1000) * 1e6
})

Why was this closed? There was no explanation. Is this a won't fix? This still doesn't work.

@marshauf can you reopen this?

@jontroncoso
Timestamp Documentation mentions about the way it is structured. Following the same, in JavaScript, you can generate a timestamp with the below code.

new Date().toISOString()

I also had some issues using the Timestamp in Typescript. Turned out it was because of setSeconds and setNanos.

The following code did the trick for me:

import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';

const timestamp = new Timestamp();
timestamp.fromDate(new Date());

Source: https://stackoverflow.com/a/58391603/787625

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andiwonder picture andiwonder  路  3Comments

chloe2018s picture chloe2018s  路  3Comments

b1naryMan picture b1naryMan  路  4Comments

mj-mehdizadeh picture mj-mehdizadeh  路  5Comments

thedillonb picture thedillonb  路  4Comments