Protobuf: proto3 lacks TimestampValue

Created on 23 Sep 2016  路  1Comment  路  Source: protocolbuffers/protobuf

proto3 adds google.protobuf.Timestamp but not TimestampValue

@jskeet what is recommended way to represent C# nullable DateTime? in proto3 ?

c# question

Most helpful comment

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();

>All comments

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();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mkosieradzki picture mkosieradzki  路  70Comments

joshuarubin picture joshuarubin  路  69Comments

blowmage picture blowmage  路  73Comments

liujisi picture liujisi  路  48Comments

laszloagardi picture laszloagardi  路  40Comments