Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Google Protobuf TimeStamp from .Net System.DateTime

How to convert .Net System.DateTime to google Protobuf TimeStamp when forming a protobuf message?

like image 791
sbp Avatar asked Dec 12 '19 05:12

sbp


2 Answers

Use protobuf's Timestamp.FromDateTime. Following is the example of converting DateTime.Now to proto timestamp type.

Google.Protobuf.WellKnownTypes.Timestamp protoTimestamp 
    = Google.Protobuf.WellKnownTypes.Timestamp.FromDateTime(DateTime.Now);

This is the official reference link

like image 74
sbp Avatar answered Oct 15 '22 13:10

sbp


Timestamp only works with UTC. So you may do this:

Timestamp.FromDateTime(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)));
like image 37
Gauravsa Avatar answered Oct 15 '22 11:10

Gauravsa