August 27th, 2005, 12:20 PM
Python FILETIME Structure
With the project I am currently working on I was in need of Windows FILETIME structures, while at the same time be able to support all platforms. I could not use ctypes because the kernel32.dll will not function the same on most systems that are not Windows-based. So I decided to create my own function that converts a LocalTime or a SystemTime into a valid FILETIME. Below is the function I wrote and how it works. It will convert either LocalTime or SystemTime. If you do not know what a FILETIME, LocalTime, or a SystemTime is, then this thread may not be for you because you will have no clue what is going on.
Line one declares important integer variables that I will need to use. The maximum amount of days, hours, minutes, seconds, and nps.
days = 365; hours = 24; minutes = 60; seconds = 60; nps = 1000000000
y = int(ltime[:4]); d = int(ltime[8:10])
h = int(ltime[11:13]); m = int(ltime[14:16]); s = int(ltime[17:19])
yd = y - 1601; ld = int(strftime('%j')); lh = h; lm = m; ls = s
total_days = yd * days - (days - ld)
total_hours = total_days * hours - (hours - lh)
total_min = total_hours * minutes - (minutes - lm)
total_sec = total_min * seconds - (seconds - ls)
total_nano = total_sec * nps
return total_nano / 100
nps is the number of nanoseconds that occur per second. As you can see the number is 1 billion.
Line two extracts the year and date from ltime.
Line three extracts the hour, minute, and second from ltime.
yd is the year difference. This subtracts the current year (y) from 1601.
ld is the "last day". This equals the day of the year (int).
lh is the "last hour". It equals h.
lm is the "last minute". It equals m.
ls is the "last second". It equals s.
total_days is the total amount of days from 1601 until now.
total_hours is the amount of hours from January 1, 1601 midnight.
total_min is the total amount of minutes since January 1, 1601 midnight.
total_sec is the total amount of seconds since January 1, 1601 midnight.
total_nano is the total amount of nanoseconds since January 1, 1601 midnight.
The return value is the number of 100-nanosecond intervals total.
This function disregards DST.
I don't need a function to convert this back to LocalTime or SystemTime so I probably won't end up writing one.
Hope this helps someone.