duration_pb.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (c) 2025-2026 Buf Technologies, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # Generated from google/protobuf/duration.proto. DO NOT EDIT.
  15. # Generated by protoc-gen-py v0.1.1 with parameter "no_fmt_off".
  16. # ruff: noqa: PGH004
  17. # ruff: noqa
  18. from __future__ import annotations
  19. from typing import Literal, TYPE_CHECKING, TypeAlias
  20. from protobuf import Message
  21. from protobuf._codegen import file_desc
  22. from protobuf.wkt._mixin import DurationMixin
  23. if TYPE_CHECKING:
  24. from protobuf import DescFile
  25. _DurationFields: TypeAlias = Literal["seconds", "nanos"]
  26. class Duration(Message[_DurationFields], DurationMixin):
  27. """
  28. A Duration represents a signed, fixed-length span of time represented
  29. as a count of seconds and fractions of seconds at nanosecond
  30. resolution. It is independent of any calendar and concepts like "day"
  31. or "month". It is related to Timestamp in that the difference between
  32. two Timestamp values is a Duration and it can be added or subtracted
  33. from a Timestamp. Range is approximately +-10,000 years.
  34. # Examples
  35. Example 1: Compute Duration from two Timestamps in pseudo code.
  36. Timestamp start = ...;
  37. Timestamp end = ...;
  38. Duration duration = ...;
  39. duration.seconds = end.seconds - start.seconds;
  40. duration.nanos = end.nanos - start.nanos;
  41. if (duration.seconds < 0 && duration.nanos > 0) {
  42. duration.seconds += 1;
  43. duration.nanos -= 1000000000;
  44. } else if (duration.seconds > 0 && duration.nanos < 0) {
  45. duration.seconds -= 1;
  46. duration.nanos += 1000000000;
  47. }
  48. Example 2: Compute Timestamp from Timestamp + Duration in pseudo code.
  49. Timestamp start = ...;
  50. Duration duration = ...;
  51. Timestamp end = ...;
  52. end.seconds = start.seconds + duration.seconds;
  53. end.nanos = start.nanos + duration.nanos;
  54. if (end.nanos < 0) {
  55. end.seconds -= 1;
  56. end.nanos += 1000000000;
  57. } else if (end.nanos >= 1000000000) {
  58. end.seconds += 1;
  59. end.nanos -= 1000000000;
  60. }
  61. Example 3: Compute Duration from datetime.timedelta in Python.
  62. td = datetime.timedelta(days=3, minutes=10)
  63. duration = Duration()
  64. duration.FromTimedelta(td)
  65. # JSON Mapping
  66. In JSON format, the Duration type is encoded as a string rather than an
  67. object, where the string ends in the suffix "s" (indicating seconds) and
  68. is preceded by the number of seconds, with nanoseconds expressed as
  69. fractional seconds. For example, 3 seconds with 0 nanoseconds should be
  70. encoded in JSON format as "3s", while 3 seconds and 1 nanosecond should
  71. be expressed in JSON format as "3.000000001s", and 3 seconds and 1
  72. microsecond should be expressed in JSON format as "3.000001s".
  73. ```proto
  74. message google.protobuf.Duration
  75. ```
  76. Attributes:
  77. seconds:
  78. Signed seconds of the span of time. Must be from -315,576,000,000
  79. to +315,576,000,000 inclusive. Note: these bounds are computed from:
  80. 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
  81. ```proto
  82. int64 seconds = 1;
  83. ```
  84. nanos:
  85. Signed fractions of a second at nanosecond resolution of the span
  86. of time. Durations less than one second are represented with a 0
  87. `seconds` field and a positive or negative `nanos` field. For durations
  88. of one second or more, a non-zero value for the `nanos` field must be
  89. of the same sign as the `seconds` field. Must be from -999,999,999
  90. to +999,999,999 inclusive.
  91. ```proto
  92. int32 nanos = 2;
  93. ```
  94. """
  95. __slots__ = ("seconds", "nanos")
  96. if TYPE_CHECKING:
  97. def __init__(self, *, seconds: int = 0, nanos: int = 0) -> None:
  98. pass
  99. seconds: int
  100. nanos: int
  101. _DESC = file_desc(
  102. b'\n\x1egoogle/protobuf/duration.proto\x12\x0fgoogle.protobuf":\n\x08Duration\x12\x18\n\x07seconds\x18\x01 \x01(\x03R\x07seconds\x12\x14\n\x05nanos\x18\x02 \x01(\x05R\x05nanosB\x83\x01\n\x13com.google.protobufB\rDurationProtoP\x01Z1google.golang.org/protobuf/types/known/durationpb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3',
  103. [],
  104. {"Duration": Duration},
  105. )
  106. def desc() -> DescFile:
  107. """Returns the descriptor for the file `google/protobuf/duration.proto`."""
  108. return _DESC