Timestamp Conversion¶
pcapkit.utilities.datetime converts a UNIX timestamp to an aware
datetime without the C library’s range (#2099), and
a datetime back to a UNIX timestamp without GraalPy’s lost
second before 1970 (#2134). It is shared by the PCAP and PCAP-NG readers
and writers and the MH timestamp options, which is why it lives here rather than
beside any one of them.
- pcapkit.utilities.datetime.fromtimestamp(timestamp, tz)[source]¶
datetime.datetime.fromtimestamp()with atz, minus the C library’s range.The result equals
datetime.datetime.fromtimestamp(timestamp, tz)on 64-bit Linux for everytimestampthat call accepts, on every platform: whole seconds and microseconds split from a float as CPython splits them, rounding half to even, then added to the UNIX epoch and rendered intz. The standard library breaks the instant down with the C library’sgmtime(), whose range on Windows is 1970 to 3000, so there it raisesOSErrorfor instantsdatetimeholds.- Parameters:
- Return type:
- Returns:
The instant
timestampnames, rendered intz.- Raises:
ValueError – If
timestampis NaN, or names a year outside 1 to 9999.OverflowError – If
timestampis past a 64-bittime_t, ortzmoves the result out of the rangedatetimeholds.
Added in version 1.5.0: Every capture and MH timestamp is converted with it (#2099).
- pcapkit.utilities.datetime.totimestamp(dt)[source]¶
datetime.datetime.timestamp(), as CPython computes it, on every interpreter.For an aware
dtthat is(dt - epoch).total_seconds(). GraalPy’s own counts the whole seconds toward zero, so there an instant before 1970 with a fraction of a second comes out one second late, and this computes it as CPython does. A naivedtis local time, and isdt.timestamp()everywhere.