A Protobuf Duration and a Timestamp Are the Same Bytes (Until You Read the Schema)

I encoded the same two numbers twice this week. Once as a Timestamp, once as a Duration, and the bytes that came out were identical, down to the last one. 90 in the first field, five hundred million in the second, and both encodings produced the exact same length-delimited block: the tag, the length, the seconds varint, the nanos varint, 8 bytes of inner payload. The format has no way to know what the pair is meant to mean. Timestamp and Duration are the same message shape in the well-known files, int64 seconds as field one, int32 nanos as field two, and the wire does not carry the word duration anywhere. A decoder without your schema sees a nested message with two whole numbers and nothing more, and it cannot tell you whether that pair is a moment in time or a length of time.

Which means the difference between a moment and a length is not in the bytes at all. It is in your .proto and in your head. The same stream of 8 bytes is a point on the calendar when the field is a Timestamp and an interval when the field is a Duration, and nothing on the wire forces you to be consistent about it. That is why the raw decode tab is the right place to look when a duration field gives you trouble. You decode the stream with no schema and you see the two varints sitting there, and you can confirm the numbers the way a schema would, without the schema having a vote. The label is the whole story, and the label lives in the file you may not have.

The JSON side does the same split the Timestamp post covered, with one difference in the rendering. The official mapping says a Duration goes out as a string, the seconds and the fractional part and a trailing s, like ninety point five s. The library the converter runs, protobufjs, does not render that form. It gives you the object back, seconds as a decimal string because it is sixty-four bits, nanos as a plain number, and if you hand it the spec form on the way in it refuses with an object expected error, the same one-way door as the Timestamp. A value serialized by a compliant library comes out in a shape this one only accepts in the other direction, and neither is wrong about the shape it speaks.

The pair also skips the checks the spec says it should carry. The mapping requires the nanos to stay between zero and nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine, and it requires seconds and nanos to have the same sign, since a duration with a positive seconds and a negative nanos is a duration that runs backwards. I put ninety in seconds and minus five hundred million in nanos, encoded, decoded, and it came back exactly as I put it in, no error, a pair that should read as eighty-nine and a half seconds but will read as ninety and a half to anyone who adds instead of subtracts. A fully negative duration, minus ninety and a half seconds, carries both numbers as negative varints, each one signing out to ten bytes the way any negative sixty-four-bit integer does, and it round-trips clean. The wire is honest about all of it. It just does not judge it.

There is one more thing the bytes do not carry, and it is the one that bites in practice. A duration that is zero and a duration that was never set are the same on the wire, which is zero bytes, because an empty message encodes to nothing. So elapsed time of zero seconds and elapsed time of unknown are the same absence, and if your protocol needs to tell them apart the schema has to add the mechanism, an explicit presence, a wrapper, a separate flag. The no-schema view shows it the same way the Timestamp post ended: two varints in a nested message, and the only thing standing between that pair and a wrong reading is the label in a file you can read.

All of it runs in the browser with no protoc. Paste a .proto that imports the duration file, encode a job record with an elapsed time in it, and watch the two varints come out as a length-delimited block. Decode a stream that carries one and see the object form instead of the ninety point five s string. The raw decode tab at proto.datajutsu.com shows the pair as plain numbers before the schema gets a vote, which is the fastest way to see that the word duration was never in the bytes either. The sister post on the Timestamp is the Timestamp post, and the mapping rules both of these are judged against are in the JSON mapping post.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe