Sun, 13 Oct 2013

Converting Microsoft time to Real Time .:.permalink.:.

Lots of ldap attributes, registy entries, etc use microsoft's "custom" time format of 100 nanoseconds since 1601.

I suck at that much math, so here's a simple python script to convert that to my timezone:


#!/usr/bin/python2

import datetime
import sys
import pytz
from pytz import timezone
#change this to your timezone or add a sys.argv option if you'd like.
localtz=timezone('PST8PDT')

ADEPOCH = datetime.datetime(1601, 1, 1, 0, 0, 0,tzinfo=pytz.utc)

def adtotimedt(stime):
    return (ADEPOCH + datetime.timedelta(microseconds=int(stime)/10))

dt=adtotimedt(sys.argv[1])

print(dt.astimezone(localtz))

use it thusly:

./adtime.py  130041707812557947
2013-01-31 21:39:41.255794-08:00

and suffer from timezone math no longer.

Posted at: Sun, 13 Oct 2013 | category: /itsec