This patch attempts to fix the issue with localized timezones on
authorBruce Momjian <bruce@momjian.us>
Thu, 2 Sep 2004 01:03:59 +0000 (01:03 +0000)
committerBruce Momjian <bruce@momjian.us>
Thu, 2 Sep 2004 01:03:59 +0000 (01:03 +0000)
Windows.

Recap: When running on a localized windows version, the timezone name
returned is also localized, and therefor does not match our lookup
table.

Solution: The registry contains both the name of the timezone in english
and the localized name. The patch adds code to scan the registry for the
localized name and gets the english name from that, and then rescans the
table.

I have tested this on a Swedish WinXP, and it works without problems.
The registry layout is the same in Win2k, but I haven't specifically
tested it. It's also the same on different languages but again only
Swedish is tested.

Magnus Hagander

src/timezone/pgtz.c

index 6bc0a10821bc96cf674ff039cec076dc879f0d56..c4785b7d3bab2967b357f40b6a4d6c4137f13a8b 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/timezone/pgtz.c,v 1.25 2004/09/01 16:21:50 tgl Exp $
+ *   $PostgreSQL: pgsql/src/timezone/pgtz.c,v 1.26 2004/09/02 01:03:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -953,6 +953,110 @@ identify_system_timezone(void)
        }
    }
 
+   /*
+    * Localized Windows versions return localized names for the
+    * timezones. Scan the registry to find the english name,
+    * and then try matching against the table again.
+    */
+   ZeroMemory(localtzname, sizeof(localtzname));
+   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+                    "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
+                    0,
+                    KEY_READ,
+                    &rootKey) != ERROR_SUCCESS)
+   {
+       ereport(WARNING,
+               (errmsg_internal("could not open registry key to identify Windows timezone \"%s\": %i", tzname, (int)GetLastError())));
+       return NULL;
+   }
+   
+   for (idx = 0; ; idx++) 
+   {
+       char keyname[256];
+       char zonename[256];
+       DWORD namesize = sizeof(keyname);
+       FILETIME lastwrite;
+       HKEY key;
+       LONG r;
+       
+       ZeroMemory(keyname,sizeof(keyname));
+
+       if ((r=RegEnumKeyEx(rootKey,
+                           idx,
+                           keyname,
+                           &namesize,
+                           NULL,
+                           NULL,
+                           NULL,
+                           &lastwrite)) != ERROR_SUCCESS)
+       {
+           if (r == ERROR_NO_MORE_ITEMS)
+               break;
+           ereport(WARNING,
+                   (errmsg_internal("could not enumerate registry subkeys to identify Windows timezone \"%s\": %i", tzname, (int)r)));
+           break;
+       }
+
+       if ((r=RegOpenKeyEx(rootKey,keyname,0,KEY_READ,&key)) != ERROR_SUCCESS)
+       {
+           ereport(WARNING,
+                   (errmsg_internal("could not open registry subkey to identify Windows timezone \"%s\": %i", tzname, (int)r)));
+           break;
+       }
+       
+       ZeroMemory(zonename,sizeof(zonename));
+       namesize = sizeof(zonename);
+       if ((r=RegQueryValueEx(key, "Std", NULL, NULL, zonename, &namesize)) != ERROR_SUCCESS)
+       {
+           ereport(WARNING,
+                   (errmsg_internal("could not query value for 'std' to identify Windows timezone \"%s\": %i", tzname, (int)r)));
+           RegCloseKey(key);
+           break;
+       }
+       if (!strcmp(tzname, zonename))
+       {
+           /* Matched zone */
+           strcpy(localtzname, keyname);
+           RegCloseKey(key);
+           break;
+       }
+       ZeroMemory(zonename, sizeof(zonename));
+       namesize = sizeof(zonename);
+       if ((r=RegQueryValueEx(key, "Dlt", NULL, NULL, zonename, &namesize)) != ERROR_SUCCESS)
+       {
+           ereport(WARNING,
+                   (errmsg_internal("could not query value for 'dlt' to identify Windows timezone \"%s\": %i", tzname, (int)r)));
+           RegCloseKey(key);
+           break;
+       }
+       if (!strcmp(tzname, zonename))
+       {
+           /* Matched DST zone */
+           strcpy(localtzname, keyname);
+           RegCloseKey(key);
+           break;
+       }
+
+       RegCloseKey(key);
+   }
+
+   RegCloseKey(rootKey);
+
+   if (localtzname[0])
+   {
+       /* Found a localized name, so scan for that one too */
+       for (i = 0; win32_tzmap[i].stdname != NULL; i++)
+       {
+           if (strcmp(localtzname, win32_tzmap[i].stdname) == 0 ||
+               strcmp(localtzname, win32_tzmap[i].dstname) == 0)
+           {
+               elog(DEBUG4, "TZ \"%s\" matches localized Windows timezone \"%s\" (\"%s\")",
+                    win32_tzmap[i].pgtzname, tzname, localtzname);
+               return win32_tzmap[i].pgtzname;
+           }
+       }
+   }
+
    ereport(WARNING,
            (errmsg("could not find a match for Windows timezone \"%s\"",
                    tzname)));