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 a tz, minus the C library’s range.

The result equals datetime.datetime.fromtimestamp(timestamp, tz) on 64-bit Linux for every timestamp that 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 in tz. The standard library breaks the instant down with the C library’s gmtime(), whose range on Windows is 1970 to 3000, so there it raises OSError for instants datetime holds.

Parameters:
  • timestamp (float | int) – Seconds since the UNIX epoch.

  • tz (tzinfo) – Timezone the result is rendered in.

Return type:

datetime

Returns:

The instant timestamp names, rendered in tz.

Raises:
  • ValueError – If timestamp is NaN, or names a year outside 1 to 9999.

  • OverflowError – If timestamp is past a 64-bit time_t, or tz moves the result out of the range datetime holds.

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 dt that 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 naive dt is local time, and is dt.timestamp() everywhere.

Parameters:

dt (datetime) – Moment to convert.

Return type:

float

Returns:

Seconds since the UNIX epoch.

Added in version 1.5.0: The PCAP and PCAP-NG writers and the MH replay protection option convert a datetime with it (#2134).