Package google.protobuf
Stay organized with collections
Save and categorize content based on your preferences.
Duration
A Duration represents a signed, fixed-length span of time represented as a count of seconds and fractions of seconds at nanosecond resolution. It is independent of any calendar and concepts like "day" or "month". It is related to Timestamp in that the difference between two Timestamp values is a Duration and it can be added or subtracted from a Timestamp. Range is approximately +-10,000 years.
Example 1: Compute Duration from two Timestamps in pseudo code.
Timestamp start = ...;
Timestamp end = ...;
Duration duration = ...;
duration.seconds = end.seconds - start.seconds;
duration.nanos = end.nanos - start.nanos;
if (duration.seconds < 0 && duration.nanos > 0) {
duration.seconds += 1;
duration.nanos -= 1000000000;
} else if (duration.seconds > 0 && duration.nanos < 0) {
duration.seconds -= 1;
duration.nanos += 1000000000;
}
Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
Timestamp start = ...;
Duration duration = ...;
Timestamp end = ...;
end.seconds = start.seconds + duration.seconds;
end.nanos = start.nanos + duration.nanos;
if (end.nanos < 0) {
end.seconds -= 1;
end.nanos += 1000000000;
} else if (end.nanos >= 1000000000) {
end.seconds += 1;
end.nanos -= 1000000000;
}
Field name |
Type |
Description |
seconds |
int64 |
Signed seconds of the span of time. Must be from -315,576,000,000 to +315,576,000,000 inclusive. |
nanos |
int32 |
Signed fractions of a second at nanosecond resolution of the span of time. Durations less than one second are represented with a 0 seconds field and a positive or negative nanos field. For durations of one second or more, a non-zero value for the nanos field must be of the same sign as the seconds field. Must be from -999,999,999 to +999,999,999 inclusive. |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-06-26 UTC.
[null,null,["Last updated 2024-06-26 UTC."],[[["\u003cp\u003e\u003ccode\u003eDuration\u003c/code\u003e represents a time span with seconds and nanoseconds, independent of calendar concepts.\u003c/p\u003e\n"],["\u003cp\u003eIt can be calculated by finding the difference between two \u003ccode\u003eTimestamp\u003c/code\u003e values.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eDuration\u003c/code\u003e can be added to or subtracted from a \u003ccode\u003eTimestamp\u003c/code\u003e to compute a new \u003ccode\u003eTimestamp\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eValid \u003ccode\u003eDuration\u003c/code\u003e values range from approximately -10,000 years to +10,000 years.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eseconds\u003c/code\u003e field represents the seconds and \u003ccode\u003enanos\u003c/code\u003e field represents the nanoseconds of the duration.\u003c/p\u003e\n"]]],[],null,["# Package google.protobuf\n\nIndex\n-----\n\n- [Duration](/digital-asset-links/reference/rpc/google.protobuf#google.protobuf.Duration) (message)\n\nDuration\n--------\n\nA Duration represents a signed, fixed-length span of time represented as a count of seconds and fractions of seconds at nanosecond resolution. It is independent of any calendar and concepts like \"day\" or \"month\". It is related to Timestamp in that the difference between two Timestamp values is a Duration and it can be added or subtracted from a Timestamp. Range is approximately +-10,000 years.\n\nExample 1: Compute Duration from two Timestamps in pseudo code. \n\n Timestamp start = ...;\n Timestamp end = ...;\n Duration duration = ...;\n\n duration.seconds = end.seconds - start.seconds;\n duration.nanos = end.nanos - start.nanos;\n\n if (duration.seconds \u003c 0 && duration.nanos \u003e 0) {\n duration.seconds += 1;\n duration.nanos -= 1000000000;\n } else if (duration.seconds \u003e 0 && duration.nanos \u003c 0) {\n duration.seconds -= 1;\n duration.nanos += 1000000000;\n }\n\nExample 2: Compute Timestamp from Timestamp + Duration in pseudo code. \n\n Timestamp start = ...;\n Duration duration = ...;\n Timestamp end = ...;\n\n end.seconds = start.seconds + duration.seconds;\n end.nanos = start.nanos + duration.nanos;\n\n if (end.nanos \u003c 0) {\n end.seconds -= 1;\n end.nanos += 1000000000;\n } else if (end.nanos \u003e= 1000000000) {\n end.seconds += 1;\n end.nanos -= 1000000000;\n }\n\n| Field name | Type | Description |\n|------------|---------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `seconds` | `int64` | Signed seconds of the span of time. Must be from -315,576,000,000 to +315,576,000,000 inclusive. |\n| `nanos` | `int32` | Signed fractions of a second at nanosecond resolution of the span of time. Durations less than one second are represented with a 0 `seconds` field and a positive or negative `nanos` field. For durations of one second or more, a non-zero value for the `nanos` field must be of the same sign as the `seconds` field. Must be from -999,999,999 to +999,999,999 inclusive. |"]]