The following structure, external function declaration, and object function will convert a PB Datetime value from the user's local time zone to GMT/UTC/Zulu:
You need a PB structure that mimics the WinAPI SYSTEMTIME structure. I named it s_systemtime:
global type s_systemtime from structure
integer i_year
integer i_month
integer i_dayofweek
integer i_day
integer i_hour
integer i_minute
integer i_second
integer i_milliseconds
end type
You'll need an External Function Declaration for the Windows API function that will be called:
FUNCTION Boolean TzSpecificLocalTimeToSystemTime ( &
Longptr lpTimeZoneInformation, &
s_systemtime lpLocalTime, &
REF s_systemtime lpUniversalTime &
) LIBRARY "kernel32.dll"
Here's an example of an object function that calls the Windows API function:
// Public Function of_Convert_To_UTC(Datetime adt_input) Returns Datetime
// Converts a Datetime value from the "local" timezone into GMT/UTC
// by utilizing a Windows API function.
Date ld_temp
Time lt_temp
Datetime ldt_utc
s_systemtime lstr_local, lstr_utc
// Extract date and time components of datetime argument value.
ld_temp = Date(adt_input)
lt_temp = Time(adt_input)
// Load the date and time components into the Windows API SYSTEMTIME structure.
lstr_local.i_Year = Year(ld_temp)
lstr_local.i_Month = Month(ld_temp)
lstr_local.i_Day = Day(ld_temp)
lstr_local.i_Hour = Hour(lt_temp)
lstr_local.i_Minute = Minute(lt_temp)
lstr_local.i_Second = Second(lt_temp)
lstr_local.i_Milliseconds = Integer(Right(String(lt_temp,"hh:mm:ss.fff"),3))
// Convert from local time to GMT/UTC. The zero first argument tells Windows
// to use the currently active time zone for the first SYSTEMTIME structure.
TzSpecificLocalTimeToSystemTime(0,lstr_local,lstr_utc)
// Construct the datetime value to be returned from the output SYSTEMTIME structure.
ld_temp = Date(lstr_utc.i_Year, &
lstr_utc.i_Month, &
lstr_utc.i_Day)
lt_temp = Time(lstr_utc.i_Hour, &
lstr_utc.i_Minute, &
lstr_utc.i_Second, &
lstr_utc.i_Milliseconds)
ldt_utc = Datetime(ld_temp,lt_temp)
Return ldt_utc
Finally, here's an example that calls the object function, to test:
Datetime ldt_local, ldt_utc
ldt_local = Datetime(Today(),Now())
ldt_utc = of_Convert_To_UTC(ldt_local)
MessageBox("Convert to UTC","Current Local Date/Time:~t" + &
String(ldt_local,"yyyy-mm-dd hh:mm:ss") + &
"~r~n~r~nCurrent UTC Date/Time:~t" + &
String(ldt_utc,"yyyy-mm-dd hh:mm:ss"))
As Roland suggests, you can format the date/time value as needed.
Good luck!