Skip to content

Instantly share code, notes, and snippets.

@alex-pat
Last active March 1, 2024 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-pat/fd6f2f9f311c639bb22cb7eaf6ec47bc to your computer and use it in GitHub Desktop.
Save alex-pat/fd6f2f9f311c639bb22cb7eaf6ec47bc to your computer and use it in GitHub Desktop.
Convert dmesg time offsets to real time
#!/usr/bin/env python3
USAGE = """ ./timesg.py TIMESTAMP [HOURS] <dmesg >dmesg-with-time """
import sys
import re
import datetime
RE = re.compile(r'^\[\s*(\d+\.\d+)\]')
def main(starttime, hours=0., *_):
starttime = starttime + hours * 3600
for line in sys.stdin:
parts = RE.split(line)
if len(parts) != 3:
print(line, end='')
continue
_, t_off, tail = parts
t_off = float(t_off)
time = datetime.datetime.fromtimestamp(starttime + t_off)
print('[' + time.strftime('%Y-%m-%d %H:%M:%S') + ']' + tail, end='')
if __name__ == '__main__':
if len(sys.argv) > 1:
main(*map(float, sys.argv[1:]))
else:
print(USAGE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment