Converting from UNIX time to .NET DateTime.ToFileTimeUtc() (in python) -


clarification: asking if wrong method, or if there less clumsy way it.

i have 2 sets of utc timestamps in log files need establish correspondence between. 1 uses yyyy:mm:dd hh:mm:ss:ms format, , uses int64 returned .net's datetime.tofiletimeutc(). set out convert full date format int64 format in python.

.net's datetime.tofiletimeutc() documentation: http://msdn.microsoft.com/en-us/library/system.datetime.tofiletimeutc.aspx

finding correct solution confusing hunt. unfortunately, found out python , .net seem use different epochs, starting in 1970 , 1601 respectively.

python's calendar.timegm(), use quantity in seconds: http://docs.python.org/2/library/calendar.html#calendar.timegm

here's code i'm using. appears work properly. hope useful more myself.

from datetime import datetime import calendar   net_epoch = datetime(1601,1,1) unix_epoch = datetime(1970,1,1)  epoch_delta = (unix_epoch - net_epoch)  #example time in september, 2012 year = 2012 month = 9 day = 9 hour = 23 minute = 56 second = 22 millisecond = 12  #thankfully i'm not worrying timezones  log_time = datetime(year,month,day,hour,minute,second) #,millisecond) # millisecond not included since precision not used @ moment.  unix_ts = calendar.timegm(log_time.timetuple()) #units of seconds since unix epoch  net_ts = calendar.timegm( (log_time + epoch_delta).timetuple() ) #units of seconds since net epoch  net_filetime_ts = net_ts* (10**7) + millisecond * (10**4) #units of 100 ns since net epoch #corresponds .net datetime.now.tofiletimeutc() 

perhaps it's more helpful , simpler time relative unix epoch in c#? believe should this:

(datetime.utcnow - new datetime (1970, 1, 1)).totalmilliseconds 

Comments