Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix UTCTimeField timezone #4564

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading