1# Copyright 2018 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from datetime import datetime
16from datetime import timedelta
17
18from google.protobuf import duration_pb2
19from google.protobuf import timestamp_pb2
20from proto import datetime_helpers
21
22
23class TimestampRule:
24 """A marshal between Python datetimes and protobuf timestamps.
25
26 Note: Python datetimes are less precise than protobuf datetimes
27 (microsecond vs. nanosecond level precision). If nanosecond-level
28 precision matters, it is recommended to interact with the internal
29 proto directly.
30 """
31
32 def to_python(
33 self, value, *, absent: bool = None
34 ) -> datetime_helpers.DatetimeWithNanoseconds:
35 if isinstance(value, timestamp_pb2.Timestamp):
36 if absent:
37 return None
38 return datetime_helpers.DatetimeWithNanoseconds.from_timestamp_pb(value)
39 return value
40
41 def to_proto(self, value) -> timestamp_pb2.Timestamp:
42 if isinstance(value, datetime_helpers.DatetimeWithNanoseconds):
43 return value.timestamp_pb()
44 if isinstance(value, datetime):
45 return timestamp_pb2.Timestamp(
46 seconds=int(value.timestamp()),
47 nanos=value.microsecond * 1000,
48 )
49 if isinstance(value, str):
50 timestamp_value = timestamp_pb2.Timestamp()
51 timestamp_value.FromJsonString(value=value)
52 return timestamp_value
53 return value
54
55
56class DurationRule:
57 """A marshal between Python timedeltas and protobuf durations.
58
59 Note: Python timedeltas are less precise than protobuf durations
60 (microsecond vs. nanosecond level precision). If nanosecond-level
61 precision matters, it is recommended to interact with the internal
62 proto directly.
63 """
64
65 def to_python(self, value, *, absent: bool = None) -> timedelta:
66 if isinstance(value, duration_pb2.Duration):
67 return timedelta(
68 days=value.seconds // 86400,
69 seconds=value.seconds % 86400,
70 microseconds=value.nanos // 1000,
71 )
72 return value
73
74 def to_proto(self, value) -> duration_pb2.Duration:
75 if isinstance(value, timedelta):
76 return duration_pb2.Duration(
77 seconds=value.days * 86400 + value.seconds,
78 nanos=value.microseconds * 1000,
79 )
80 if isinstance(value, str):
81 duration_value = duration_pb2.Duration()
82 duration_value.FromJsonString(value=value)
83 return duration_value
84 return value