Time zone information in Windows

Source: Internet
Author: User
Tags time zones

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
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #define PWIN32ERROR (Dwsyserr, SMSG)
  4. typedef struct _reg_tzi_format
  5. {
  6. LONG Bias;
  7. LONG StandardBias;
  8. LONG DaylightBias;
  9. SYSTEMTIME standarddate;
  10. SYSTEMTIME daylightdate;
  11. } Reg_tzi_format;
  12. #define Reg_time_zones "Software\\microsoft\\windows nt\\currentversion\\time zones\\"
  13. #define Reg_time_zones_len (sizeof (Reg_time_zones)-1)
  14. #define Reg_tz_key_maxlen (Reg_time_zones_len + (sizeof ((time_zone_information*) 0)->standardname)/2)-1)
  15. int Gettimezoneinformationbyname (time_zone_information *ptzi, const Char standardname[]) {
  16. int RC;
  17. HKEY Hkey_tz;
  18. DWORD DW;
  19. Reg_tzi_format Regtzi;
  20. char tzsubkey[reg_tz_key_maxlen+1] = reg_time_zones;
  21. strncpy (Tzsubkey + Reg_time_zones_len, StandardName, sizeof (Tzsubkey)-Reg_time_zones_len);
  22. if (tzsubkey[sizeof (Tzsubkey)-1]! = 0) {
  23. fprintf (stderr, "timezone name Too long\n");
  24. return-1;
  25. }
  26. if (error_success! = (DW = RegOpenKey (HKEY_LOCAL_MACHINE, Tzsubkey, &hkey_tz))) {
  27. fprintf (stderr, "failed to open:hkey_local_machine\\%s\n", Tzsubkey);
  28. Pwin32error (DW, "RegOpenKey () failed");
  29. return-1;
  30. }
  31. rc = 0;
  32. #define X (param, type, var) \
  33. do if (DW = sizeof (VAR)), (Error_success! = (DW = REGGETVALUEW (Hkey_tz, NULL, param, type, null, &var, &A MP;DW ))) {\
  34. fprintf (stderr, "failed to read:hkey_local_machine\\%s\\%s\n", Tzsubkey, param);
  35. Pwin32error (DW, "Reggetvalue () failed"); \
  36. rc =-1; \
  37. goto ennd; \
  38. } while (0)
  39. X (L"TZI", Rrf_rt_reg_binary, Regtzi);
  40. X (L"Std", RRF_RT_REG_SZ, Ptzi->standardname);
  41. X (L"Dlt", RRF_RT_REG_SZ, Ptzi->daylightname);
  42. #undef X
  43. Ptzi->bias = Regtzi. Bias;
  44. Ptzi->daylightbias = Regtzi. DaylightBias;
  45. Ptzi->daylightdate = Regtzi. DaylightDate;
  46. Ptzi->standardbias = Regtzi. StandardBias;
  47. Ptzi->standarddate = Regtzi. StandardDate;
  48. Ennd:
  49. RegCloseKey (Hkey_tz);
  50. return RC;
  51. }
  52. #define ZONE "Russian Standard Time"
  53. int main (int argc, char* argv[])
  54. {
  55. DWORD DW;
  56. Time_zone_information Tzi;
  57. DW = Gettimezoneinformationbyname (&tzi, ZONE);
  58. if (DW! = 0) return 1;
  59. SYSTEMTIME lt;
  60. SYSTEMTIME ut = {
  61. /*word, wyear;*/
  62. 1, /*word wmonth;*/
  63. 0, /*word wdayofweek;*/
  64. 1, /*word wday;*/
  65. /*word whour;*/
  66. 0, /*word wminute;*/
  67. 0, /*word wsecond;*/
  68. 0 /*word wmilliseconds;*/
  69. };
  70. Systemtimetotzspecificlocaltime (&tzi, &ut, <);
  71. printf ("%d-%02d-%02d%02d:%02d:%02d utc\n", Ut.wyear, Ut.wmonth, Ut.wday, Ut.whour, Ut.wminute, Ut.wsecond);
  72. printf ("=\n");
  73. printf ("%d-%02d-%02d%02d:%02d:%02d europe/moscow\n", Lt.wyear, Lt.wmonth, Lt.wday, Lt.whour, Lt.wminute,  Lt.wsecond);
  74. return 0;
  75. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.