TJF
TJF

Reputation: 2258

protobuf-net encode DateTime UTC decode ToLocalTime

Is there a way for protobuf-net to automatically encode DateTime values in UTC but decode with ToLocalTime?

I'm passing messages between systems in different time zone and would like each DateTime property in my DTO' to be serialized as UTC (essentially automatically call ToUniversalTime before encoding) and decode in LocalTime so the systems receiving the messages automatically can operate on the DateTime Properties in LocalTime, so developers don't have to remember calling ToLocalTime on each property for DTO's received over the wire?

Upvotes: 2

Views: 281

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063814

For this type of transformation, the usual approach is to have two properties:

[ProtoMember(n, ...)]
public DateTime When { get; set; }

public DateTime WhenLocal => When.ToLocal(); // etc

The serializer won't care about the one without the attribute. The serializer property could be private if you really really don't want callers using it.

Upvotes: 1

Related Questions