Skip to content

Commit 903c3f1

Browse files
anandoleecopybara-github
authored andcommitted
Nextgen Proto Pythonic API: Add duration.py
PiperOrigin-RevId: 661035580
1 parent 814352c commit 903c3f1

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

python/google/protobuf/duration.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
# Protocol Buffers - Google's data interchange format
3+
# Copyright 2008 Google Inc. All rights reserved.
4+
#
5+
# Use of this source code is governed by a BSD-style
6+
# license that can be found in the LICENSE file or at
7+
# https://developers.google.com/open-source/licenses/bsd
8+
9+
"""Tests proto Duration APIs."""
10+
11+
import datetime
12+
import unittest
13+
14+
from google.protobuf import duration
15+
16+
from google.protobuf import duration_pb2
17+
18+
19+
class DurationTest(unittest.TestCase):
20+
21+
def test_duration_integer_conversion(self):
22+
self.assertEqual(1, duration.to_nanoseconds(duration.from_nanoseconds(1)))
23+
self.assertEqual(-1, duration.to_seconds(duration.from_seconds(-1)))
24+
self.assertEqual(
25+
123, duration.to_milliseconds(duration.from_milliseconds(123))
26+
)
27+
self.assertEqual(
28+
321, duration.to_microseconds(duration.from_microseconds(321))
29+
)
30+
31+
def test_duration_json(self):
32+
33+
def check_duration(message, text):
34+
self.assertEqual(text, duration.to_json_string(message))
35+
parsed_duration = duration.from_json_string(text)
36+
self.assertEqual(message, parsed_duration)
37+
38+
message = duration_pb2.Duration()
39+
message.seconds = 0
40+
message.nanos = 0
41+
check_duration(message, '0s')
42+
message.nanos = 10000000
43+
check_duration(message, '0.010s')
44+
message.nanos = 10000
45+
check_duration(message, '0.000010s')
46+
message.nanos = 10
47+
check_duration(message, '0.000000010s')
48+
49+
def test_duration_timedelta(self):
50+
message = duration.from_nanoseconds(1999999999)
51+
td = duration.to_timedelta(message)
52+
self.assertEqual(1, td.seconds)
53+
self.assertEqual(999999, td.microseconds)
54+
55+
message = duration.from_microseconds(-1)
56+
td = duration.to_timedelta(message)
57+
converted_message = duration.from_timedelta(td)
58+
self.assertEqual(message, converted_message)
59+
60+
61+
if __name__ == '__main__':
62+
unittest.main()

python/google/protobuf/internal/timestamp_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# license that can be found in the LICENSE file or at
77
# https://developers.google.com/open-source/licenses/bsd
88

9-
"""Tests proto_time_util APIs."""
9+
"""Tests proto Timestamp APIs."""
1010

1111
import datetime
1212
import unittest
@@ -17,7 +17,7 @@
1717
from google.protobuf import timestamp_pb2
1818

1919

20-
class ProtoTimeUtilTest(unittest.TestCase):
20+
class TimestampTest(unittest.TestCase):
2121

2222
def test_timestamp_integer_conversion(self):
2323
self.assertEqual(1, timestamp.to_nanoseconds(timestamp.from_nanoseconds(1)))

0 commit comments

Comments
 (0)