WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Convert UTC Time


Time conversion routines...
;*******************************************************************
;  Convert Local time to Universal time.                           
; Input is the local time in YmdHms format.                        
; Output is also YmdHms, but converted to Universal time.
;*******************************************************************
#DefineFunction TimeLocalToUniversal(Time)
   ST  = BinaryAlloc(16)
   FT1 = BinaryAlloc(8)
   FT2 = BinaryAlloc(8)

   Yr = Int(ItemExtract(1,Time,":"))     ; We must have 4 digit years.
   if Yr < 100 then Yr = 2000 + Yr       ; so assume 2 digit years are 20xx.

   BinaryPoke2(ST, 0,Yr)                 ; Fill a SYSTEMTIME structure
   BinaryPoke2(ST, 2,Int(ItemExtract(2,Time,":")))
   BinaryPoke2(ST, 6,Int(ItemExtract(3,Time,":")))
   BinaryPoke2(ST, 8,Int(ItemExtract(4,Time,":")))
   BinaryPoke2(ST,10,Int(ItemExtract(5,Time,":")))
   BinaryPoke2(ST,12,Int(ItemExtract(6,Time,":")))

                                          ; Do the time conversion
   kernel32 = DllLoad(strcat(dirwindows(1),"Kernel32.DLL"))
   DllCall(kernel32,lpstr:"SystemTimeToFileTime",lpbinary:ST, lpbinary:FT1)
   DllCall(kernel32,lpstr:"LocalFileTimeToFileTime",lpbinary:FT1, lpbinary:FT2)
   DllCall(kernel32,lpstr:"FileTimeToSystemTime",lpbinary:FT2, lpbinary:ST)
   DllFree(Kernel32)

   Local = Strcat(BinaryPeek2(ST,0),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,2),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,6),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,8),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,10),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,12),"0",2))
   BinaryFree(ST)
   BinaryFree(FT1)
   BinaryFree(FT2)
   return Local
#EndFunction

;*******************************************************************
;  Convert Universal time to local time.                           
; Input is universal time in YmdHms format.                        
; Output is also YmdHms, but converted to local time.
;*******************************************************************
#DefineFunction TimeUniversalToLocal(Time)
   ST  = BinaryAlloc(16)
   FT1 = BinaryAlloc(8)
   FT2 = BinaryAlloc(8)

   Yr = Int(ItemExtract(1,Time,":"))     ; We must have 4 digit years.
   if Yr < 100 then Yr = 2000 + Yr       ; So assume 2 digit years are 20xx.

   BinaryPoke2(ST, 0,Yr)                 ; Fill a SYSTEMTIME structure
   BinaryPoke2(ST, 2,Int(ItemExtract(2,Time,":")))
   BinaryPoke2(ST, 6,Int(ItemExtract(3,Time,":")))
   BinaryPoke2(ST, 8,Int(ItemExtract(4,Time,":")))
   BinaryPoke2(ST,10,Int(ItemExtract(5,Time,":")))
   BinaryPoke2(ST,12,Int(ItemExtract(6,Time,":")))

                                          ; Do the time conversion
   kernel32 = DllLoad(strcat(dirwindows(1),"Kernel32.DLL"))
   DllCall(kernel32,lpstr:"SystemTimeToFileTime",lpbinary:ST, lpbinary:FT1)
   DllCall(kernel32,lpstr:"FileTimeToLocalFileTime",lpbinary:FT1, lpbinary:FT2)
   DllCall(kernel32,lpstr:"FileTimeToSystemTime",lpbinary:FT2, lpbinary:ST)
   DllFree(Kernel32)

   Local = Strcat(BinaryPeek2(ST,0),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,2),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,6),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,8),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,10),"0",2),":")
   Local = StrCat(Local,StrFixLeft(BinaryPeek2(ST,12),"0",2))
   BinaryFree(ST)
   BinaryFree(FT1)
   BinaryFree(FT2)
   return Local
#EndFunction


;*********************************
;
;  Test the routines
;
;*********************************
TimeNow = TimeYmdHms()
UTC = TimeLocalToUniversal(TimeNow)
Local = TimeUniversalToLocal(UTC)

Text = StrCat("Original:",@Tab,@Tab,@TAB,TimeNow,@CRLF,"Converted To Universal:",@TAB,UTC,@CRLF,"Converted to Local:",@Tab,@Tab,Local)
Message("",Text)
NOTE: Under Windows NT/2000/XP, the time zone bias (the differential between UTC and local time) is stored in the Windows Registry in the following value.
HKLM/SYSTEM/CurrentControlSet/Control/TimeZoneInformation[ActiveTimeBias]
Here is some code to get the GMT Offset
activebias = RegQueryDWORD( @REGMACHINE,'SYSTEM\CurrentControlSet\Control\TimeZoneInformation[ActiveTimeBias]') 
machinegmtoffset = int ( ( activebias / 60 ) * ( -1 ) ) ;

Article ID:   W16729
File Created: 2005:02:18:12:22:04
Last Updated: 2005:02:18:12:22:04