Skip to content

Instantly share code, notes, and snippets.

@jsenin
Last active June 21, 2021 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsenin/33e3cbcbeae5eccb6933884f28344786 to your computer and use it in GitHub Desktop.
Save jsenin/33e3cbcbeae5eccb6933884f28344786 to your computer and use it in GitHub Desktop.
pack and unpack octects from snmp oids in python
import struct
import datetime
ts = 1620386580.0
dt = datetime.datetime.fromtimestamp(ts)
packed = struct.pack(">hbbbbbb", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond)
print ("packed", packed, ts )
tp = dt.timetuple() # expand values in a tuple
import time
unpacked = struct.unpack('>hbbbbbb', packed)
dt = datetime.datetime(*unpacked)
ts = time.mktime(dt.timetuple()) + (dt.microsecond / 1000000.0)
1620386580.0
@jsenin
Copy link
Author

jsenin commented Jun 21, 2021

Example from a huawei OLT response
How to 'decrypt' the date time values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment