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