Skip to content

Commit

Permalink
Implement wrapper for google.protobuf.Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ntindall committed Jun 26, 2019
1 parent 4d490eb commit b390fe7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ wrappers[".google.protobuf.Any"] = {
return this.toObject(message, options);
}
};

// Custom wrapper for Timestamp.
//
// Implements the JSON serialization / deserialization as specified by
// proto specification.
//
// https://github.com/protocolbuffers/protobuf/blob/5bc250b084b88b6ec98046054f5836b6b60132ef/src/google/protobuf/timestamp.proto#L101
wrappers[".google.protobuf.Timestamp"] = {
fromObject: function(object) {
//Convert ISO-8601 to epoch millis
var dt = Date.parse(object);
return this.create({
seconds: Math.floor(dt/1000),
nanos: (dt % 1000) * 1_000_000
});
},

toObject: function(message, options) {
return new Date(message.seconds*1000 + message.nanos/1_000_000).toISOString();
}
};

0 comments on commit b390fe7

Please sign in to comment.