proto3 adds google.protobuf.Timestamp but not TimestampValue
@jskeet what is recommended way to represent C# nullable DateTime? in proto3 ?
Just use a Timestamp field and let it be null when you want it to be - Timestamp is just a regular message type. The conversion is simple. Suppose you have:
message Proto {
Timestamp update = 1;
}
and
public class Poco {
public DateTime? Update { get; set; }
}
Then converting between them, you'd use (in C# 6):
poco.Update = proto.Update?.ToDateTime();
proto.Update = poco.Update == null ? null : Timestamp.FromDateTime(poco.Update);
The latter is obviously fairly ugly... you could introduce your own extension method if you need to do this often:
public static Timestamp ToTimestamp(this DateTime? dateTime) =>
dateTime == null ? null : Timestamp.FromDateTime(dateTime);
// And use it like this
proto.Update = poco.Update.ToTimestamp();
Most helpful comment
Just use a
Timestampfield and let it be null when you want it to be -Timestampis just a regular message type. The conversion is simple. Suppose you have:and
Then converting between them, you'd use (in C# 6):
The latter is obviously fairly ugly... you could introduce your own extension method if you need to do this often: