]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/common/wintz.c
ICU-511.25.tar.gz
[apple/icu.git] / icuSources / common / wintz.c
index ad06d7e1fe2812d394d55e0622d6c164c1c11a33..9148b11e61ba1adca74b0f1a653d312aafdc7872 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ********************************************************************************
-*   Copyright (C) 2005-2011, International Business Machines
+*   Copyright (C) 2005-2012, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 ********************************************************************************
 *
@@ -14,7 +14,6 @@
 #if U_PLATFORM_HAS_WIN32_API
 
 #include "wintz.h"
-
 #include "cmemory.h"
 #include "cstring.h"
 
@@ -29,7 +28,7 @@
 #   define NOMCX
 #include <windows.h>
 
-#define MAX_LENGTH_ID 32
+#define MAX_LENGTH_ID 40
 
 /* The layout of the Tzi value in the registry */
 typedef struct
@@ -122,7 +121,7 @@ static LONG openTZRegKey(HKEY *hkey, const char *winid)
 {
     /* subKeyName needs to be long enough for the longest TZ_REGKEY, plus the longest Windows
      * tzid (current or obsolete), plus an appended STANDARD_TIME_REGKEY, plus a 0 terminator.
-     * Currently this is 111, but the code had 110. Make it 128 for some wiggle room. */
+     * At its max point this was 111, but the code had 110. Make it 128 for some wiggle room. */
     char subKeyName[128];
     char *name;
     LONG result;
@@ -256,10 +255,13 @@ uprv_detectWindowsTimeZone() {
     char* icuid = NULL;
     UChar apiStd[MAX_LENGTH_ID];
     char apiStdName[MAX_LENGTH_ID];
-       char regStdName[MAX_LENGTH_ID];
+    char regStdName[MAX_LENGTH_ID];
     char tmpid[MAX_LENGTH_ID];
     int32_t apiStdLength = 0;
     int32_t len;
+    int id;
+    int errorCode;
+    char ISOcode[3]; /* 2 letter iso code */
 
     LONG result;
     TZI tziKey;
@@ -286,6 +288,9 @@ uprv_detectWindowsTimeZone() {
 
     tmpid[0] = 0;
 
+    id = GetUserGeoID(GEOCLASS_NATION);
+    errorCode = GetGeoInfo(id,GEO_ISO2,ISOcode,3,0);
+
     bundle = ures_openDirect(NULL, "windowsZones", &status);
     ures_getByKey(bundle, "mapTimezones", bundle, &status);
 
@@ -308,7 +313,16 @@ uprv_detectWindowsTimeZone() {
             tziKey.daylightBias = tziReg.daylightBias;
 
             if (uprv_memcmp((char *)&tziKey, (char*)&tziReg, sizeof(tziKey)) == 0) {
-                const UChar* icuTZ = ures_getStringByKey(winTZ, "001", &len, &status);
+                const UChar* icuTZ = NULL;
+                if (errorCode != 0) {
+                    icuTZ = ures_getStringByKey(winTZ, ISOcode, &len, &status);
+                }
+                if (errorCode==0 || icuTZ==NULL) {
+                    /* fallback to default "001" and reset status */
+                    status = U_ZERO_ERROR;
+                    icuTZ = ures_getStringByKey(winTZ, "001", &len, &status);
+                }
+
                 if (U_SUCCESS(status)) {
                     /* Get the standard name from the registry key to compare with
                        the one from Windows API call. */
@@ -325,8 +339,12 @@ uprv_detectWindowsTimeZone() {
                      * the current time zone information)
                      */
                     if (idFound || tmpid[0] == 0) {
-                        uprv_memset(tmpid, 0, sizeof(tmpid));
-                        u_austrncpy(tmpid, icuTZ, len);
+                        /* if icuTZ has more than one city, take only the first (i.e. terminate icuTZ at first space) */
+                        int index=0;
+                        while (! (*icuTZ == '\0' || *icuTZ ==' ')) {
+                            tmpid[index++]=(char)(*icuTZ++);  /* safe to assume 'char' is ASCII compatible on windows */
+                        }
+                        tmpid[index]='\0';
                     }
                 }
             }
@@ -349,7 +367,7 @@ uprv_detectWindowsTimeZone() {
     }
 
     ures_close(bundle);
-
+    
     return icuid;
 }