timestamp.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc. All rights reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style
  5. # license that can be found in the LICENSE file or at
  6. # https://developers.google.com/open-source/licenses/bsd
  7. """Contains the Timestamp helper APIs."""
  8. import datetime
  9. from typing import Optional
  10. from google.protobuf.timestamp_pb2 import Timestamp
  11. def from_json_string(value: str) -> Timestamp:
  12. """Parse a RFC 3339 date string format to Timestamp.
  13. Args:
  14. value: A date string. Any fractional digits (or none) and any offset are
  15. accepted as long as they fit into nano-seconds precision. Example of
  16. accepted format: '1972-01-01T10:00:20.021-05:00'
  17. Raises:
  18. ValueError: On parsing problems.
  19. """
  20. timestamp = Timestamp()
  21. timestamp.FromJsonString(value)
  22. return timestamp
  23. def from_microseconds(micros: float) -> Timestamp:
  24. """Converts microseconds since epoch to Timestamp."""
  25. timestamp = Timestamp()
  26. timestamp.FromMicroseconds(micros)
  27. return timestamp
  28. def from_milliseconds(millis: float) -> Timestamp:
  29. """Converts milliseconds since epoch to Timestamp."""
  30. timestamp = Timestamp()
  31. timestamp.FromMilliseconds(millis)
  32. return timestamp
  33. def from_nanoseconds(nanos: float) -> Timestamp:
  34. """Converts nanoseconds since epoch to Timestamp."""
  35. timestamp = Timestamp()
  36. timestamp.FromNanoseconds(nanos)
  37. return timestamp
  38. def from_seconds(seconds: float) -> Timestamp:
  39. """Converts seconds since epoch to Timestamp."""
  40. timestamp = Timestamp()
  41. timestamp.FromSeconds(seconds)
  42. return timestamp
  43. def from_current_time() -> Timestamp:
  44. """Converts the current UTC to Timestamp."""
  45. timestamp = Timestamp()
  46. timestamp.FromDatetime(datetime.datetime.now(tz=datetime.timezone.utc))
  47. return timestamp
  48. def to_json_string(ts: Timestamp) -> str:
  49. """Converts Timestamp to RFC 3339 date string format.
  50. Returns:
  51. A string converted from timestamp. The string is always Z-normalized
  52. and uses 3, 6 or 9 fractional digits as required to represent the
  53. exact time. Example of the return format: '1972-01-01T10:00:20.021Z'
  54. """
  55. return ts.ToJsonString()
  56. def to_microseconds(ts: Timestamp) -> int:
  57. """Converts Timestamp to microseconds since epoch."""
  58. return ts.ToMicroseconds()
  59. def to_milliseconds(ts: Timestamp) -> int:
  60. """Converts Timestamp to milliseconds since epoch."""
  61. return ts.ToMilliseconds()
  62. def to_nanoseconds(ts: Timestamp) -> int:
  63. """Converts Timestamp to nanoseconds since epoch."""
  64. return ts.ToNanoseconds()
  65. def to_seconds(ts: Timestamp) -> int:
  66. """Converts Timestamp to seconds since epoch."""
  67. return ts.ToSeconds()
  68. def to_datetime(
  69. ts: Timestamp, tz: Optional[datetime.tzinfo] = None
  70. ) -> datetime.datetime:
  71. """Converts Timestamp to a datetime.
  72. Args:
  73. tz: A datetime.tzinfo subclass; defaults to None.
  74. Returns:
  75. If tzinfo is None, returns a timezone-naive UTC datetime (with no timezone
  76. information, i.e. not aware that it's UTC).
  77. Otherwise, returns a timezone-aware datetime in the input timezone.
  78. """
  79. return ts.ToDatetime(tzinfo=tz)