Changeset View
Changeset View
Standalone View
Standalone View
swh/model/model.py
Show First 20 Lines • Show All 402 Lines • ▼ Show 20 Lines | class TimestampWithTimezone(BaseModel): | ||||
) -> "TimestampWithTimezone": | ) -> "TimestampWithTimezone": | ||||
"""Returns a :class:`TimestampWithTimezone` instance from the old dictionary | """Returns a :class:`TimestampWithTimezone` instance from the old dictionary | ||||
format (with ``offset`` and ``negative_utc`` instead of ``offset_bytes``). | format (with ``offset`` and ``negative_utc`` instead of ``offset_bytes``). | ||||
""" | """ | ||||
negative = offset < 0 or negative_utc | negative = offset < 0 or negative_utc | ||||
(hours, minutes) = divmod(abs(offset), 60) | (hours, minutes) = divmod(abs(offset), 60) | ||||
offset_bytes = f"{'-' if negative else '+'}{hours:02}{minutes:02}".encode() | offset_bytes = f"{'-' if negative else '+'}{hours:02}{minutes:02}".encode() | ||||
tstz = TimestampWithTimezone(timestamp=timestamp, offset_bytes=offset_bytes) | tstz = TimestampWithTimezone(timestamp=timestamp, offset_bytes=offset_bytes) | ||||
assert tstz.offset == offset, (tstz.offset, offset) | assert tstz.offset_minutes() == offset, (tstz.offset_minutes(), offset) | ||||
return tstz | return tstz | ||||
@classmethod | @classmethod | ||||
def from_dict( | def from_dict( | ||||
cls, time_representation: Union[Dict, datetime.datetime, int] | cls, time_representation: Union[Dict, datetime.datetime, int] | ||||
) -> "TimestampWithTimezone": | ) -> "TimestampWithTimezone": | ||||
"""Builds a TimestampWithTimezone from any of the formats | """Builds a TimestampWithTimezone from any of the formats | ||||
accepted by :func:`swh.model.normalize_timestamp`.""" | accepted by :func:`swh.model.normalize_timestamp`.""" | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | class TimestampWithTimezone(BaseModel): | ||||
def to_datetime(self) -> datetime.datetime: | def to_datetime(self) -> datetime.datetime: | ||||
"""Convert to a datetime (with a timezone set to the recorded fixed UTC offset) | """Convert to a datetime (with a timezone set to the recorded fixed UTC offset) | ||||
Beware that this conversion can be lossy: ``-0000`` and 'weird' offsets | Beware that this conversion can be lossy: ``-0000`` and 'weird' offsets | ||||
cannot be represented. Also note that it may fail due to type overflow. | cannot be represented. Also note that it may fail due to type overflow. | ||||
""" | """ | ||||
timestamp = datetime.datetime.fromtimestamp( | timestamp = datetime.datetime.fromtimestamp( | ||||
self.timestamp.seconds, | self.timestamp.seconds, | ||||
datetime.timezone(datetime.timedelta(minutes=self.offset)), | datetime.timezone(datetime.timedelta(minutes=self.offset_minutes())), | ||||
) | ) | ||||
timestamp = timestamp.replace(microsecond=self.timestamp.microseconds) | timestamp = timestamp.replace(microsecond=self.timestamp.microseconds) | ||||
return timestamp | return timestamp | ||||
@classmethod | @classmethod | ||||
def from_iso8601(cls, s): | def from_iso8601(cls, s): | ||||
"""Builds a TimestampWithTimezone from an ISO8601-formatted string. | """Builds a TimestampWithTimezone from an ISO8601-formatted string. | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | def offset_minutes(self): | ||||
-120 | -120 | ||||
>>> TimestampWithTimezone( | >>> TimestampWithTimezone( | ||||
... Timestamp(seconds=1642765364, microseconds=0), offset_bytes=b"+0530" | ... Timestamp(seconds=1642765364, microseconds=0), offset_bytes=b"+0530" | ||||
... ).offset_minutes() | ... ).offset_minutes() | ||||
330 | 330 | ||||
""" | """ | ||||
return self._parse_offset_bytes(self.offset_bytes) | return self._parse_offset_bytes(self.offset_bytes) | ||||
@property | |||||
def offset(self): | |||||
"""Deprecated alias of :meth:`offset_minutes`.""" | |||||
return self.offset_minutes() | |||||
@attr.s(frozen=True, slots=True) | @attr.s(frozen=True, slots=True) | ||||
class Origin(HashableObject, BaseModel): | class Origin(HashableObject, BaseModel): | ||||
"""Represents a software source: a VCS and an URL.""" | """Represents a software source: a VCS and an URL.""" | ||||
object_type: Final = "origin" | object_type: Final = "origin" | ||||
url = attr.ib(type=str, validator=type_validator()) | url = attr.ib(type=str, validator=type_validator()) | ||||
▲ Show 20 Lines • Show All 904 Lines • Show Last 20 Lines |