diff --git a/scapy/data.py b/scapy/data.py index a577e14c090..d61b8b85071 100644 --- a/scapy/data.py +++ b/scapy/data.py @@ -266,9 +266,8 @@ PPI_BTLE: 'btle', } - -# On windows, epoch is 01/02/1970 at 00:00 -EPOCH = calendar.timegm((1970, 1, 2, 0, 0, 0, 3, 1, 0)) - 86400 +# See https://github.com/secdev/scapy/issues/4557 +EPOCH = calendar.timegm((1970, 1, 1, 0, 0, 0, 3, 1, 0)) MTU = 0xffff # a.k.a give me all you have diff --git a/scapy/fields.py b/scapy/fields.py index 06d73225589..99bc97da64c 100644 --- a/scapy/fields.py +++ b/scapy/fields.py @@ -3492,7 +3492,7 @@ def __init__( class UTCTimeField(Field[float, int]): - __slots__ = ["epoch", "delta", "strf", + __slots__ = ["epoch", "delta", "tz", "strf", "use_msec", "use_micro", "use_nano", "custom_scaling"] def __init__(self, @@ -3502,6 +3502,7 @@ def __init__(self, use_micro=False, # type: bool use_nano=False, # type: bool epoch=None, # type: Optional[Tuple[int, int, int, int, int, int, int, int, int]] # noqa: E501 + tz=datetime.timezone.utc, # type: datetime.timezone strf="%a, %d %b %Y %H:%M:%S %z", # type: str custom_scaling=None, # type: Optional[int] fmt="I" # type: str @@ -3511,6 +3512,7 @@ def __init__(self, mk_epoch = EPOCH if epoch is None else calendar.timegm(epoch) self.epoch = mk_epoch self.delta = mk_epoch - EPOCH + self.tz = tz self.strf = strf self.use_msec = use_msec self.use_micro = use_micro @@ -3533,9 +3535,9 @@ def i2repr(self, pkt, x): # To make negative timestamps work on all plateforms (e.g. Windows), # we need a trick. t = ( - datetime.datetime(1970, 1, 1) + + datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc) + datetime.timedelta(seconds=x) - ).strftime(self.strf) + ).astimezone(self.tz).strftime(self.strf) return "%s (%d)" % (t, int(x)) def i2m(self, pkt, x): diff --git a/test/fields.uts b/test/fields.uts index b64300b8279..c4af8cefc71 100644 --- a/test/fields.uts +++ b/test/fields.uts @@ -377,15 +377,26 @@ class TestUTCTimeField(Packet): fmt="