Extend pre-compiled Duration and Timestamp instead of manual definition
This commit is contained in:
		| @@ -941,19 +941,10 @@ def which_one_of(message: Message, group_name: str) -> Tuple[str, Any]: | |||||||
|     return (field_name, getattr(message, field_name)) |     return (field_name, getattr(message, field_name)) | ||||||
|  |  | ||||||
|  |  | ||||||
| @dataclasses.dataclass | from .lib.google.protobuf import Duration, Timestamp | ||||||
| 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) |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class _Duration(Duration): | ||||||
|     def to_timedelta(self) -> timedelta: |     def to_timedelta(self) -> timedelta: | ||||||
|         return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3) |         return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3) | ||||||
|  |  | ||||||
| @@ -966,16 +957,7 @@ class _Duration(Message): | |||||||
|         return ".".join(parts) + "s" |         return ".".join(parts) + "s" | ||||||
|  |  | ||||||
|  |  | ||||||
| @dataclasses.dataclass | class _Timestamp(Timestamp): | ||||||
| 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) |  | ||||||
|  |  | ||||||
|     def to_datetime(self) -> datetime: |     def to_datetime(self) -> datetime: | ||||||
|         ts = self.seconds + (self.nanos / 1e9) |         ts = self.seconds + (self.nanos / 1e9) | ||||||
|         return datetime.fromtimestamp(ts, tz=timezone.utc) |         return datetime.fromtimestamp(ts, tz=timezone.utc) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user