datetime - MS Timestamp parsing Python -
is there built-in function in python can use parse ms timestamps ? example, "\x3a\xcf\x84\x72\x66\x42\xcd\x01" correspond date & time: 06/04/2012, 15:26:43.901625
the timestamp (as @mandel mentioned) looks filetime -- 64-bit value representing number of 100-nanosecond intervals since january 1, 1601 (utc):
import struct datetime import datetime, timedelta def filetime_bytes_to_datetime(timestamp_bytes): quadword, = struct.unpack('<q', timestamp_bytes) = quadword // 10 - 11644473600000000 return datetime(1970, 1, 1) + timedelta(microseconds=us) example:
>>> filetime_bytes_to_datetime(b"\x3a\xcf\x84\x72\x66\x42\xcd\x01") datetime.datetime(2012, 6, 4, 15, 26, 43, 901625) '<q' (little-endian unsigned 64-bit integer) might not work on platforms (due alignment of filetime structure fields, endianness).
to parse value on windows, use ctypes.wintypes.filetime structure:
import ctypes import os datetime import datetime, timedelta if os.name == 'nt': import ctypes.wintypes filetime = ctypes.wintypes.filetime else: # fallback dword = ctypes.c_uint32 class filetime(ctypes.structure): _fields_ = [('dwlowdatetime', dword), ('dwhighdatetime', dword)] def filetime_bytes_to_datetime(timestamp_bytes): t = filetime.from_buffer_copy(timestamp_bytes) quadword = (t.dwhighdatetime << 32) + t.dwlowdatetime = quadword // 10 - 11644473600000000 return datetime(1970, 1, 1) + timedelta(microseconds=us) it uses native byte order, alignment (you can override if necessary).
Comments
Post a Comment