Search code examples
protocol-buffersprotobuf-java

How to represent XMLGregorianCalendar in protobuf 3 format


I have an xml with time as

<Opened date="2012-09-13T18:30:34+05:30" Lang_Code="ENG"></Opened>

I need to represent it in proto3 format and ship the generated java classes. what data type to use? I tried google.protobuf.Timestamp but that does not fit the bill.

syntax = "proto3";

message Opened{

  <what data type?> date =1;

  string lang_code=2;

}

Solution

  • You might want to at least consider using the DateTime message in google/type/datetime.proto - that repo is the one we (Google) use for our public API protos, including supporting types like DateTime.

    You'd probably want to document and validate that:

    • The year value would never be 0
    • The hour value would never be 24
    • The seconds value would never be 60 (unless you actually plan on supporting leap seconds)
    • The time_offset oneof will always be populated via utc_offset

    Of course, you can create your own proto with similar information, should you wish. There's no out-of-the-box message that includes what you need, so you'll need to use some kind of message, unless you want to just leave it as a string (which I wouldn't recommend).