Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
duration.py101 linesDownload Raw Back to protobuf
1# Protocol Buffers - Google's data interchange format2# Copyright 2008 Google Inc.  All rights reserved.3#4# Use of this source code is governed by a BSD-style5# license that can be found in the LICENSE file or at6# https://developers.google.com/open-source/licenses/bsd7 8"""Contains the Duration helper APIs."""9 10import datetime11 12from google.protobuf.duration_pb2 import Duration13 14 15def 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'. Any20      fractional digits (or none) are accepted as long as they fit into21      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 duration29 30 31def from_microseconds(micros: float) -> Duration:32  """Converts microseconds to Duration."""33  duration = Duration()34  duration.FromMicroseconds(micros)35  return duration36 37 38def from_milliseconds(millis: float) -> Duration:39  """Converts milliseconds to Duration."""40  duration = Duration()41  duration.FromMilliseconds(millis)42  return duration43 44 45def from_nanoseconds(nanos: float) -> Duration:46  """Converts nanoseconds to Duration."""47  duration = Duration()48  duration.FromNanoseconds(nanos)49  return duration50 51 52def from_seconds(seconds: float) -> Duration:53  """Converts seconds to Duration."""54  duration = Duration()55  duration.FromSeconds(seconds)56  return duration57 58 59def from_timedelta(td: datetime.timedelta) -> Duration:60  """Converts timedelta to Duration."""61  duration = Duration()62  duration.FromTimedelta(td)63  return duration64 65 66def 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 contains71    3, 6, or 9 fractional digits depending on the precision required to72    represent the exact Duration value. For example: "1s", "1.010s",73    "1.000000100s", "-3.100s"74  """75  return duration.ToJsonString()76 77 78def to_microseconds(duration: Duration) -> int:79  """Converts a Duration to microseconds."""80  return duration.ToMicroseconds()81 82 83def to_milliseconds(duration: Duration) -> int:84  """Converts a Duration to milliseconds."""85  return duration.ToMilliseconds()86 87 88def to_nanoseconds(duration: Duration) -> int:89  """Converts a Duration to nanoseconds."""90  return duration.ToNanoseconds()91 92 93def to_seconds(duration: Duration) -> int:94  """Converts a Duration to seconds."""95  return duration.ToSeconds()96 97 98def to_timedelta(duration: Duration) -> datetime.timedelta:99  """Converts Duration to timedelta."""100  return duration.ToTimedelta()101 
codekingpro/portable-devtools · Team Ai