Re: Date/Time conversions
- Posted by gbonvehi Mar 25, 2012
- 1310 views
Hi cp,
There are probably easier ways (and there are a few time routines in the archive). But I went ahead and wrapped your desired function. Here's the example code:
include std/dll.e include std/win32/msgbox.e include std/machine.e constant gtKernel32 = open_dll("kernel32.dll"), systemTimeToTzSpecificLocalTime = define_c_func(gtKernel32, "SystemTimeToTzSpecificLocalTime", {C_POINTER,C_POINTER,C_POINTER}, C_BOOL) atom sizeOf_SystemTime = 2 --WORD wYear; +2 --WORD wMonth; +2 --WORD wDayOfWeek; +2 --WORD wDay; +2 --WORD wHour; +2 --WORD wMinute; +2 --WORD wSecond; +2 --WORD wMilliseconds; -- These are pointers to SYSTEMTIME structures atom systemTimeInPtr = allocate(sizeOf_SystemTime) -- UTC time to be converted atom systemTimeOutPtr = allocate(sizeOf_SystemTime) -- GMT time resulting -- Populate SYSTEMTIME structure procedure populateSystemTime(atom systemTimePtr, sequence date) -- systemTimePtr: pointer to allocated memory -- date: {YYYY,MM,DD,HH,MM,SS} poke2(systemTimePtr ,date[1]) -- Year poke2(systemTimePtr+2 ,date[2]) -- Month poke2(systemTimePtr+4 ,0 ) -- Day of week poke2(systemTimePtr+6 ,date[3]) -- Day poke2(systemTimePtr+8 ,date[4]) -- Hour poke2(systemTimePtr+10,date[5]) -- Minute poke2(systemTimePtr+12,date[6]) -- Second poke2(systemTimePtr+14,0 ) -- Milisecond end procedure -- Parse the time from a SYSTEMTIME structure function getSystemTime(atom systemTimePtr) return { peek2s(systemTimePtr ) -- Year ,peek2s(systemTimePtr+2 ) -- Month ,peek2s(systemTimePtr+6 ) -- Day ,peek2s(systemTimePtr+8 ) -- Hour ,peek2s(systemTimePtr+10 ) -- Minute ,peek2s(systemTimePtr+12 ) -- Second } end function sequence gmt0time = {2012,03,25,23,30,00} ?gmt0time -- Put inside SYSTEMTIME, our current time populateSystemTime(systemTimeInPtr, gmt0time) -- This is UTC (GMT 0) time -- Do the conversion, NULL as the first parameter uses current active time zone (in my case, GMT-3) if c_func(systemTimeToTzSpecificLocalTime,{NULL,systemTimeInPtr,systemTimeOutPtr}) = 0 then puts(1,"Error calling SystemTimeToTzSpecificLocalTime") abort(1) end if -- Print the converted system time ?getSystemTime(systemTimeOutPtr) -- In my machine (GMT-3) this prints {2012,3,25,20,30,0} -- Which is correct, 3 hours less than UTC -- Free used pointers free(systemTimeInPtr) free(systemTimeOutPtr)
WORDs are 16-bit (2 bytes) in memory.
Cheers, Guillermo