duration.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Duration helper APIs."""
  8. import datetime
  9. from google.protobuf.duration_pb2 import Duration
  10. def from_json_string(value: str) -> Duration:
  11. """Converts a string to Duration.
  12. Args:
  13. value: A string to be converted. The string must end with 's'. Any
  14. fractional digits (or none) are accepted as long as they fit into
  15. precision. For example: "1s", "1.01s", "1.0000001s", "-3.100s"
  16. Raises:
  17. ValueError: On parsing problems.
  18. """
  19. duration = Duration()
  20. duration.FromJsonString(value)
  21. return duration
  22. def from_microseconds(micros: float) -> Duration:
  23. """Converts microseconds to Duration."""
  24. duration = Duration()
  25. duration.FromMicroseconds(micros)
  26. return duration
  27. def from_milliseconds(millis: float) -> Duration:
  28. """Converts milliseconds to Duration."""
  29. duration = Duration()
  30. duration.FromMilliseconds(millis)
  31. return duration
  32. def from_nanoseconds(nanos: float) -> Duration:
  33. """Converts nanoseconds to Duration."""
  34. duration = Duration()
  35. duration.FromNanoseconds(nanos)
  36. return duration
  37. def from_seconds(seconds: float) -> Duration:
  38. """Converts seconds to Duration."""
  39. duration = Duration()
  40. duration.FromSeconds(seconds)
  41. return duration
  42. def from_timedelta(td: datetime.timedelta) -> Duration:
  43. """Converts timedelta to Duration."""
  44. duration = Duration()
  45. duration.FromTimedelta(td)
  46. return duration
  47. def to_json_string(duration: Duration) -> str:
  48. """Converts Duration to string format.
  49. Returns:
  50. A string converted from self. The string format will contains
  51. 3, 6, or 9 fractional digits depending on the precision required to
  52. represent the exact Duration value. For example: "1s", "1.010s",
  53. "1.000000100s", "-3.100s"
  54. """
  55. return duration.ToJsonString()
  56. def to_microseconds(duration: Duration) -> int:
  57. """Converts a Duration to microseconds."""
  58. return duration.ToMicroseconds()
  59. def to_milliseconds(duration: Duration) -> int:
  60. """Converts a Duration to milliseconds."""
  61. return duration.ToMilliseconds()
  62. def to_nanoseconds(duration: Duration) -> int:
  63. """Converts a Duration to nanoseconds."""
  64. return duration.ToNanoseconds()
  65. def to_seconds(duration: Duration) -> int:
  66. """Converts a Duration to seconds."""
  67. return duration.ToSeconds()
  68. def to_timedelta(duration: Duration) -> datetime.timedelta:
  69. """Converts Duration to timedelta."""
  70. return duration.ToTimedelta()