From eeed1c0db7e79c87b51c8878ee590d224789c6a2 Mon Sep 17 00:00:00 2001 From: boukeversteegh Date: Fri, 29 May 2020 15:37:58 +0200 Subject: [PATCH] Extend pre-compiled Duration and Timestamp instead of manual definition --- betterproto/__init__.py | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) diff --git a/betterproto/__init__.py b/betterproto/__init__.py index c300ded..3fd4a58 100644 --- a/betterproto/__init__.py +++ b/betterproto/__init__.py @@ -941,19 +941,10 @@ def which_one_of(message: Message, group_name: str) -> Tuple[str, Any]: return (field_name, getattr(message, field_name)) -@dataclasses.dataclass -class _Duration(Message): - # Signed seconds of the span of time. Must be from -315,576,000,000 to - # +315,576,000,000 inclusive. Note: these bounds are computed from: 60 - # sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years - seconds: int = int64_field(1) - # Signed fractions of a second at nanosecond resolution of the span of time. - # Durations less than one second are represented with a 0 `seconds` field and - # a positive or negative `nanos` field. For durations of one second or more, - # a non-zero value for the `nanos` field must be of the same sign as the - # `seconds` field. Must be from -999,999,999 to +999,999,999 inclusive. - nanos: int = int32_field(2) +from .lib.google.protobuf import Duration, Timestamp + +class _Duration(Duration): def to_timedelta(self) -> timedelta: return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3) @@ -966,16 +957,7 @@ class _Duration(Message): return ".".join(parts) + "s" -@dataclasses.dataclass -class _Timestamp(Message): - # Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must - # be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive. - seconds: int = int64_field(1) - # Non-negative fractions of a second at nanosecond resolution. Negative - # second values with fractions must still have non-negative nanos values that - # count forward in time. Must be from 0 to 999,999,999 inclusive. - nanos: int = int32_field(2) - +class _Timestamp(Timestamp): def to_datetime(self) -> datetime: ts = self.seconds + (self.nanos / 1e9) return datetime.fromtimestamp(ts, tz=timezone.utc)