Time
Windows API does not provide the ability to directly get information for specific time zones, and time zone information in Windows systems is stored in the registry in the following location:
HKEY_Local_Machine
Software
Microsoft
Windows NT
CurrentVersion
Time Zones
Time_zone_name
The following code demonstrates how to enumerate the time zone information from the registry:
[CPP]View Plaincopy
- #include <stdio.h>
- #include <windows.h>
- #define PWIN32ERROR (Dwsyserr, SMSG)
- typedef struct _reg_tzi_format
- {
- LONG Bias;
- LONG StandardBias;
- LONG DaylightBias;
- SYSTEMTIME standarddate;
- SYSTEMTIME daylightdate;
- } Reg_tzi_format;
- #define Reg_time_zones "Software\\microsoft\\windows nt\\currentversion\\time zones\\"
- #define Reg_time_zones_len (sizeof (Reg_time_zones)-1)
- #define Reg_tz_key_maxlen (Reg_time_zones_len + (sizeof ((time_zone_information*) 0)->standardname)/2)-1)
- int Gettimezoneinformationbyname (time_zone_information *ptzi, const Char standardname[]) {
- int RC;
- HKEY Hkey_tz;
- DWORD DW;
- Reg_tzi_format Regtzi;
- char tzsubkey[reg_tz_key_maxlen+1] = reg_time_zones;
- strncpy (Tzsubkey + Reg_time_zones_len, StandardName, sizeof (Tzsubkey)-Reg_time_zones_len);
- if (tzsubkey[sizeof (Tzsubkey)-1]! = 0) {
- fprintf (stderr, "timezone name Too long\n");
- return-1;
- }
- if (error_success! = (DW = RegOpenKey (HKEY_LOCAL_MACHINE, Tzsubkey, &hkey_tz))) {
- fprintf (stderr, "failed to open:hkey_local_machine\\%s\n", Tzsubkey);
- Pwin32error (DW, "RegOpenKey () failed");
- return-1;
- }
- rc = 0;
- #define X (param, type, var) \
- do if (DW = sizeof (VAR)), (Error_success! = (DW = REGGETVALUEW (Hkey_tz, NULL, param, type, null, &var, &A MP;DW ))) {\
- fprintf (stderr, "failed to read:hkey_local_machine\\%s\\%s\n", Tzsubkey, param);
- Pwin32error (DW, "Reggetvalue () failed"); \
- rc =-1; \
- goto ennd; \
- } while (0)
- X (L"TZI", Rrf_rt_reg_binary, Regtzi);
- X (L"Std", RRF_RT_REG_SZ, Ptzi->standardname);
- X (L"Dlt", RRF_RT_REG_SZ, Ptzi->daylightname);
- #undef X
- Ptzi->bias = Regtzi. Bias;
- Ptzi->daylightbias = Regtzi. DaylightBias;
- Ptzi->daylightdate = Regtzi. DaylightDate;
- Ptzi->standardbias = Regtzi. StandardBias;
- Ptzi->standarddate = Regtzi. StandardDate;
- Ennd:
- RegCloseKey (Hkey_tz);
- return RC;
- }
- #define ZONE "Russian Standard Time"
- int main (int argc, char* argv[])
- {
- DWORD DW;
- Time_zone_information Tzi;
- DW = Gettimezoneinformationbyname (&tzi, ZONE);
- if (DW! = 0) return 1;
- SYSTEMTIME lt;
- SYSTEMTIME ut = {
- /*word, wyear;*/
- 1, /*word wmonth;*/
- 0, /*word wdayofweek;*/
- 1, /*word wday;*/
- /*word whour;*/
- 0, /*word wminute;*/
- 0, /*word wsecond;*/
- 0 /*word wmilliseconds;*/
- };
- Systemtimetotzspecificlocaltime (&tzi, &ut, <);
- printf ("%d-%02d-%02d%02d:%02d:%02d utc\n", Ut.wyear, Ut.wmonth, Ut.wday, Ut.whour, Ut.wminute, Ut.wsecond);
- printf ("=\n");
- printf ("%d-%02d-%02d%02d:%02d:%02d europe/moscow\n", Lt.wyear, Lt.wmonth, Lt.wday, Lt.whour, Lt.wminute, Lt.wsecond);
- return 0;
- }