In this article
Use the specified Duration codec instead of serializing internal fields or converting an unlabelled integer according to a local unit assumption.
Identify the duration before converting its representation
A service returns an extraction elapsed time and accepts a delay before its next observation. The well-known Duration type represents a signed fixed span independent of calendars. Its message fields hold seconds and fractional nanoseconds. Do not infer milliseconds from an integer merely because another nearby API used them, and do not treat a fixed duration as a calendar month or a local-clock recurrence.
Follow the custom JSON representation
The ProtoJSON specification represents Duration as a string ending in s, such as "1.5s". It does not serialize the ordinary message-shaped seconds and nanos object as the canonical JSON value. Generated output has specified fractional precision, and accepted fractions must fit nanosecond precision with the suffix present. A general JSON serializer applied to an internal object can therefore produce valid JSON that violates this interface’s Duration representation.
Preserve the sign relationship
The type reference defines consistency between seconds and the fractional field for spans whose whole-second part is nonzero. Subsecond negative spans have their own valid representation. Use the type’s codec and normalization rules rather than independently attaching a sign to each component. Otherwise equivalent-looking arithmetic can construct an invalid message or change the intended delay when the application crosses between internal fields and external seconds strings.
Distinguish a span from an absolute point
API time guidance recommends different types for durations and timestamps even though their internal field shapes look similar. The same numeric components cannot be relabeled without changing meaning. For an offset within a document’s audio or video stream, also declare the reference point. A duration says how far from that origin; it does not by itself identify which recording or timeline the value addresses.
Review the consumer’s units and origin
Inspect the received JSON value, parsed duration and final operation that consumes it. Task budgets distinguish an elapsed measurement from an execution allowance; tool observations preserve the conversion. The accepted result should retain the same span and intended reference, rather than merely produce a plausible number. This makes a Duration field usable across languages without relying on undocumented seconds-versus-milliseconds assumptions or accidental similarity to a timestamp object.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.