timestamp_pb.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/timestamp.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 TimestampMixin
  23. if TYPE_CHECKING:
  24. from protobuf import DescFile
  25. _TimestampFields: TypeAlias = Literal["seconds", "nanos"]
  26. class Timestamp(Message[_TimestampFields], TimestampMixin):
  27. """
  28. A Timestamp represents a point in time independent of any time zone or local
  29. calendar, encoded as a count of seconds and fractions of seconds at
  30. nanosecond resolution. The count is relative to an epoch at UTC midnight on
  31. January 1, 1970, in the proleptic Gregorian calendar which extends the
  32. Gregorian calendar backwards to year one.
  33. All minutes are 60 seconds long. Leap seconds are "smeared" so that no leap
  34. second table is needed for interpretation, using a [24-hour linear
  35. smear](https://developers.google.com/time/smear).
  36. The range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. By
  37. restricting to that range, we ensure that we can convert to and from [RFC
  38. 3339](https://www.ietf.org/rfc/rfc3339.txt) date strings.
  39. # Examples
  40. Example 1: Compute Timestamp from POSIX `time()`.
  41. Timestamp timestamp;
  42. timestamp.set_seconds(time(NULL));
  43. timestamp.set_nanos(0);
  44. Example 2: Compute Timestamp from POSIX `gettimeofday()`.
  45. struct timeval tv;
  46. gettimeofday(&tv, NULL);
  47. Timestamp timestamp;
  48. timestamp.set_seconds(tv.tv_sec);
  49. timestamp.set_nanos(tv.tv_usec * 1000);
  50. Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`.
  51. FILETIME ft;
  52. GetSystemTimeAsFileTime(&ft);
  53. UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
  54. // A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z
  55. // is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z.
  56. Timestamp timestamp;
  57. timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL));
  58. timestamp.set_nanos((INT32) ((ticks % 10000000) * 100));
  59. Example 4: Compute Timestamp from Java `System.currentTimeMillis()`.
  60. long millis = System.currentTimeMillis();
  61. Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
  62. .setNanos((int) ((millis % 1000) * 1000000)).build();
  63. Example 5: Compute Timestamp from Java `Instant.now()`.
  64. Instant now = Instant.now();
  65. Timestamp timestamp =
  66. Timestamp.newBuilder().setSeconds(now.getEpochSecond())
  67. .setNanos(now.getNano()).build();
  68. Example 6: Compute Timestamp from current time in Python.
  69. timestamp = Timestamp()
  70. timestamp.GetCurrentTime()
  71. # JSON Mapping
  72. In JSON format, the Timestamp type is encoded as a string in the
  73. [RFC 3339](https://www.ietf.org/rfc/rfc3339.txt) format. That is, the
  74. format is "{year}-{month}-{day}T{hour}:{min}:{sec}[.{frac_sec}]Z"
  75. where {year} is always expressed using four digits while {month}, {day},
  76. {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional
  77. seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution),
  78. are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone
  79. is required. A ProtoJSON serializer should always use UTC (as indicated by
  80. "Z") when printing the Timestamp type and a ProtoJSON parser should be
  81. able to accept both UTC and other timezones (as indicated by an offset).
  82. For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past
  83. 01:30 UTC on January 15, 2017.
  84. In JavaScript, one can convert a Date object to this format using the
  85. standard
  86. [toISOString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString)
  87. method. In Python, a standard `datetime.datetime` object can be converted
  88. to this format using
  89. [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) with
  90. the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one can use
  91. the Joda Time's [`ISODateTimeFormat.dateTime()`](
  92. http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime()
  93. ) to obtain a formatter capable of generating timestamps in this format.
  94. ```proto
  95. message google.protobuf.Timestamp
  96. ```
  97. Attributes:
  98. seconds:
  99. Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must
  100. be between -62135596800 and 253402300799 inclusive (which corresponds to
  101. 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z).
  102. ```proto
  103. int64 seconds = 1;
  104. ```
  105. nanos:
  106. Non-negative fractions of a second at nanosecond resolution. This field is
  107. the nanosecond portion of the duration, not an alternative to seconds.
  108. Negative second values with fractions must still have non-negative nanos
  109. values that count forward in time. Must be between 0 and 999,999,999
  110. inclusive.
  111. ```proto
  112. int32 nanos = 2;
  113. ```
  114. """
  115. __slots__ = ("seconds", "nanos")
  116. if TYPE_CHECKING:
  117. def __init__(self, *, seconds: int = 0, nanos: int = 0) -> None:
  118. pass
  119. seconds: int
  120. nanos: int
  121. _DESC = file_desc(
  122. b'\n\x1fgoogle/protobuf/timestamp.proto\x12\x0fgoogle.protobuf";\n\tTimestamp\x12\x18\n\x07seconds\x18\x01 \x01(\x03R\x07seconds\x12\x14\n\x05nanos\x18\x02 \x01(\x05R\x05nanosB\x85\x01\n\x13com.google.protobufB\x0eTimestampProtoP\x01Z2google.golang.org/protobuf/types/known/timestamppb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3',
  123. [],
  124. {"Timestamp": Timestamp},
  125. )
  126. def desc() -> DescFile:
  127. """Returns the descriptor for the file `google/protobuf/timestamp.proto`."""
  128. return _DESC