Skip to content

Commit

Permalink
fix UTCTimeField timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
douniwan5788 committed Oct 10, 2024
1 parent 6f0faf3 commit bf07e36
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 2 additions & 3 deletions scapy/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions scapy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
15 changes: 13 additions & 2 deletions test/fields.uts
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,26 @@ class TestUTCTimeField(Packet):
fmt="<Q",
epoch=[1601, 1, 1, 0, 0, 0],
custom_scaling=1e7,
),
UTCTimeField(
"Time1",
None,
fmt="<Q",
epoch=[1601, 1, 1, 0, 0, 0],
tz=timezone(timedelta(seconds=14400)),
custom_scaling=1e7,
)
]


p = TestUTCTimeField(Time=0)
assert p.sprintf("%Time%") == 'Mon, 01 Jan 1601 00:00:00 (-11644473600)'
assert p.sprintf("%Time%") == 'Mon, 01 Jan 1601 00:00:00 +0000 (-11644473600)'

p = TestUTCTimeField(Time=133587912345678900)
assert p.sprintf("%Time%") == 'Sun, 28 Apr 2024 15:20:34 (1714317634)'
assert p.sprintf("%Time%") == 'Sun, 28 Apr 2024 15:20:34 +0000 (1714317634)'

p = TestUTCTimeField(Time1=133587912345678900)
assert p.sprintf("%Time1%") == 'Sun, 28 Apr 2024 19:20:34 +0400 (1714317634)'

############
############
Expand Down

0 comments on commit bf07e36

Please sign in to comment.