#!/usr/bin/env python import datetime import calendar import time import sys import re from optparse import OptionParser # Author: Joakim Svendsen, "joakimbs" using Google's mail services. # Copyright (c) 2013. Svendsen Tech. All rights reserved. parser = OptionParser() parser.add_option("-n", "--nano2date", type="long", dest="nano_time", help="Specify a Windows nanosecond (AD) timestamp to convert to a date/time.") parser.add_option("-d", "--date2nano", dest="human_datetime", help="Specify a date and time string in the format: \"yyyy-MM-ddTHH:mm:ss\" to be converted to a \ Windows nanosecond (AD) timestamp. Example: \"2013-01-01T17:00:00\"") (options, args) = parser.parse_args() if options.nano_time: seconds = options.nano_time / 10000000 epoch = seconds - 11644473600 dt = datetime.datetime(2000, 1, 1, 0, 0, 0) print dt.fromtimestamp(epoch) if options.human_datetime: m = re.compile(r'^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$').match(options.human_datetime) if m: dt = datetime.datetime(*map(int, m.groups())) #unix_timestamp = time.mktime(dt.timetuple()) windows_timestamp = long((time.mktime(dt.timetuple()) + 11644473600) * 10000000) print windows_timestamp else: print "Invalid date format specified with --date2nano: " + options.human_datetime + \ ".\nUse the --help parameter for more information." sys.exit()