diff --git a/swh/model/git_objects.py b/swh/model/git_objects.py --- a/swh/model/git_objects.py +++ b/swh/model/git_objects.py @@ -96,29 +96,6 @@ return float_value.rstrip("0").encode() -@lru_cache() -def format_offset(offset: int, negative_utc: Optional[bool] = None) -> bytes: - """Convert an integer number of minutes into an offset representation. - - The offset representation is [+-]hhmm where: - - - hh is the number of hours; - - mm is the number of minutes. - - A null offset is represented as +0000. - """ - if offset < 0 or offset == 0 and negative_utc: - sign = "-" - else: - sign = "+" - - hours = abs(offset) // 60 - minutes = abs(offset) % 60 - - t = "%s%02d%02d" % (sign, hours, minutes) - return t.encode() - - def normalize_timestamp(time_representation): """Normalize a time representation for processing by Software Heritage @@ -291,9 +268,8 @@ if date_offset is not None: date_f = format_date(date_offset.timestamp) - offset_f = format_offset(date_offset.offset, date_offset.negative_utc) - ret.extend([b" ", date_f, b" ", offset_f]) + ret.extend([b" ", date_f, b" ", date_offset.offset_bytes]) return b"".join(ret) diff --git a/swh/model/tests/test_identifiers.py b/swh/model/tests/test_identifiers.py --- a/swh/model/tests/test_identifiers.py +++ b/swh/model/tests/test_identifiers.py @@ -41,20 +41,10 @@ b"1448210036.12": {"seconds": 1448210036, "microseconds": 120000,}, } - self.offsets = { - 0: b"+0000", - -630: b"-1030", - 800: b"+1320", - } - def test_format_date(self): for date_repr, date in self.dates.items(): self.assertEqual(git_objects.format_date(date), date_repr) - def test_format_offset(self): - for offset, res in self.offsets.items(): - self.assertEqual(git_objects.format_offset(offset), res) - content_example = { "status": "visible", diff --git a/swh/model/tests/test_model.py b/swh/model/tests/test_model.py --- a/swh/model/tests/test_model.py +++ b/swh/model/tests/test_model.py @@ -468,6 +468,16 @@ assert tstz.negative_utc is True assert tstz.offset_bytes == b"-0000" + tstz = TimestampWithTimezone(timestamp=ts, offset=-630, negative_utc=False) + attr.validate(tstz) + assert tstz.negative_utc is False + assert tstz.offset_bytes == b"-1030" + + tstz = TimestampWithTimezone(timestamp=ts, offset=800, negative_utc=False) + attr.validate(tstz) + assert tstz.negative_utc is False + assert tstz.offset_bytes == b"+1320" + with pytest.raises(AttributeTypeError): TimestampWithTimezone( timestamp=datetime.datetime.now(), offset=0, negative_utc=False