sbp
sbp

Reputation: 973

Generate Google Protobuf TimeStamp from .Net System.DateTime

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

Upvotes: 7

Views: 4937

Answers (2)

Gauravsa
Gauravsa

Reputation: 6528

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

Timestamp.FromDateTime(DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)));

Upvotes: 4

sbp
sbp

Reputation: 973

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

Upvotes: 10

Related Questions