Python how do I convert a STCK to readable time stamp?

As part of writing a GTF trace formatter in Python I needed to covert a STCK value to a printable value. I could do it in C – but I did not find a Python equivalent.

from datetime import datetime
# Pass in a 8 bytes value
def stck(value):
value = int.from_bytes(value)
t = value/4096 # remove the bottom 12 bits to get value in micro seconds
tsm = (t /1000000 ) - 2208988800 # // number of seconds from Jan 1 1970 as float
ts = datetime.fromtimestamp(tsm) # create the timestamp
print("TS",tsm,ts.isoformat()) # format it

it produced

TS 1735804391.575975 2025-01-02T07:53:11.575975

4 thoughts on “Python how do I convert a STCK to readable time stamp?

  1. Figured I’d ask, have you written a Python program that converts STCKE to a timestamp?Wouldn’t it be the same logic but instead of removing the bottom 12 bits you would remove the bottom 4 bits and pass in a 9 byte value?Any help would be appreciated…

    Like

    1. This is some code I was working on … it hasn’t been tested, but should give you an idea

      t = value/4096 # remove the bottom 12 bits to get value in micro seconds
      tsm = (t /1000000 ) – 2208988800 # // number of seconds from Jan 1 1970
      ts = datetime.datetime.fromtimestamp(tsm)
      printable = ts.isoformat()

      Like

Leave a comment