1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2011-02-18 23:03:47 +00:00
|
|
|
* Copyright (C) 1997-2011, International Business Machines Corporation and *
|
1999-11-22 20:25:35 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File TIMEZONE.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 12/05/96 clhuang Creation.
|
|
|
|
* 04/21/97 aliu General clean-up and bug fixing.
|
|
|
|
* 05/08/97 aliu Fixed Hashtable code per code review.
|
|
|
|
* 07/09/97 helena Changed createInstance to createDefault.
|
|
|
|
* 07/29/97 aliu Updated with all-new list of 96 UNIX-derived
|
|
|
|
* TimeZones. Changed mechanism to load from static
|
|
|
|
* array rather than resource bundle.
|
|
|
|
* 07/07/1998 srl Bugfixes from the Java side: UTC GMT CAT NST
|
|
|
|
* Added getDisplayName API
|
|
|
|
* going to add custom parsing.
|
|
|
|
*
|
|
|
|
* ISSUES:
|
|
|
|
* - should getDisplayName cache something?
|
|
|
|
* - should custom time zones be cached? [probably]
|
|
|
|
* 08/10/98 stephen Brought getDisplayName() API in-line w/ conventions
|
|
|
|
* 08/19/98 stephen Changed createTimeZone() to never return 0
|
|
|
|
* 09/02/98 stephen Added getOffset(monthLen) and hasSameRules()
|
|
|
|
* 09/15/98 stephen Added getStaticClassID()
|
|
|
|
* 02/22/99 stephen Removed character literals for EBCDIC safety
|
|
|
|
* 05/04/99 stephen Changed initDefault() for Mutex issues
|
|
|
|
* 07/12/99 helena HPUX 11 CC Port.
|
1999-12-05 06:24:22 +00:00
|
|
|
* 12/03/99 aliu Moved data out of static table into icudata.dll.
|
|
|
|
* Substantial rewrite of zone lookup, default zone, and
|
|
|
|
* available IDs code. Misc. cleanup.
|
1999-08-16 21:50:52 +00:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2010-05-19 17:29:33 +00:00
|
|
|
#include <typeinfo> // for 'typeid' to work
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-11-13 02:31:11 +00:00
|
|
|
#include "unicode/ustring.h"
|
2011-02-18 23:03:47 +00:00
|
|
|
#include "ustr_imp.h"
|
2003-11-13 02:31:11 +00:00
|
|
|
|
|
|
|
#ifdef U_DEBUG_TZ
|
|
|
|
# include <stdio.h>
|
|
|
|
# include "uresimp.h" // for debugging
|
|
|
|
|
|
|
|
static void debug_tz_loc(const char *f, int32_t l)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s:%d: ", f, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void debug_tz_msg(const char *pat, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, pat);
|
|
|
|
vfprintf(stderr, pat, ap);
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
static char gStrBuf[256];
|
|
|
|
#define U_DEBUG_TZ_STR(x) u_austrncpy(gStrBuf,x,sizeof(gStrBuf)-1)
|
|
|
|
// must use double parens, i.e.: U_DEBUG_TZ_MSG(("four is: %d",4));
|
|
|
|
#define U_DEBUG_TZ_MSG(x) {debug_tz_loc(__FILE__,__LINE__);debug_tz_msg x;}
|
|
|
|
#else
|
|
|
|
#define U_DEBUG_TZ_MSG(x)
|
|
|
|
#endif
|
2002-09-20 01:54:48 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/simpletz.h"
|
|
|
|
#include "unicode/smpdtfmt.h"
|
|
|
|
#include "unicode/calendar.h"
|
2003-08-15 18:21:31 +00:00
|
|
|
#include "unicode/gregocal.h"
|
|
|
|
#include "unicode/ures.h"
|
2003-09-02 17:46:43 +00:00
|
|
|
#include "gregoimp.h"
|
2003-08-15 18:21:31 +00:00
|
|
|
#include "uresimp.h" // struct UResourceBundle
|
|
|
|
#include "olsontz.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "mutex.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/udata.h"
|
2001-08-31 02:21:34 +00:00
|
|
|
#include "ucln_in.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "cstring.h"
|
2002-07-16 17:50:42 +00:00
|
|
|
#include "cmemory.h"
|
2002-11-12 23:52:27 +00:00
|
|
|
#include "unicode/strenum.h"
|
2003-05-08 21:08:06 +00:00
|
|
|
#include "uassert.h"
|
2008-01-23 23:47:13 +00:00
|
|
|
#include "zonemeta.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2010-02-24 07:54:30 +00:00
|
|
|
#define kZONEINFO "zoneinfo64"
|
2003-11-13 02:31:11 +00:00
|
|
|
#define kREGIONS "Regions"
|
|
|
|
#define kZONES "Zones"
|
|
|
|
#define kRULES "Rules"
|
|
|
|
#define kNAMES "Names"
|
2010-02-24 07:54:30 +00:00
|
|
|
#define kTZVERSION "TZVersion"
|
|
|
|
#define kLINKS "links"
|
2007-07-24 21:37:19 +00:00
|
|
|
#define kMAX_CUSTOM_HOUR 23
|
|
|
|
#define kMAX_CUSTOM_MIN 59
|
2007-11-28 20:49:58 +00:00
|
|
|
#define kMAX_CUSTOM_SEC 59
|
2007-08-09 00:04:00 +00:00
|
|
|
#define MINUS 0x002D
|
|
|
|
#define PLUS 0x002B
|
|
|
|
#define ZERO_DIGIT 0x0030
|
2009-04-01 17:13:40 +00:00
|
|
|
#define COLON 0x003A
|
2001-06-28 22:57:15 +00:00
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
// Static data and constants
|
|
|
|
|
2010-02-24 07:54:30 +00:00
|
|
|
static const UChar WORLD[] = {0x30, 0x30, 0x31, 0x00}; /* "001" */
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
static const UChar GMT_ID[] = {0x47, 0x4D, 0x54, 0x00}; /* "GMT" */
|
2011-02-18 23:03:47 +00:00
|
|
|
static const UChar UNKNOWN_ZONE_ID[] = {0x45, 0x74, 0x63, 0x2F, 0x55, 0x6E, 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x00}; /* "Etc/Unknown" */
|
2004-09-15 23:44:48 +00:00
|
|
|
static const UChar Z_STR[] = {0x7A, 0x00}; /* "z" */
|
|
|
|
static const UChar ZZZZ_STR[] = {0x7A, 0x7A, 0x7A, 0x7A, 0x00}; /* "zzzz" */
|
2009-08-26 16:55:52 +00:00
|
|
|
static const UChar Z_UC_STR[] = {0x5A, 0x00}; /* "Z" */
|
|
|
|
static const UChar ZZZZ_UC_STR[] = {0x5A, 0x5A, 0x5A, 0x5A, 0x00}; /* "ZZZZ" */
|
|
|
|
static const UChar V_STR[] = {0x76, 0x00}; /* "v" */
|
|
|
|
static const UChar VVVV_STR[] = {0x76, 0x76, 0x76, 0x76, 0x00}; /* "vvvv" */
|
|
|
|
static const UChar V_UC_STR[] = {0x56, 0x00}; /* "V" */
|
|
|
|
static const UChar VVVV_UC_STR[] = {0x56, 0x56, 0x56, 0x56, 0x00}; /* "VVVV" */
|
2003-05-08 21:08:06 +00:00
|
|
|
static const int32_t GMT_ID_LENGTH = 3;
|
2011-02-18 23:03:47 +00:00
|
|
|
static const int32_t UNKNOWN_ZONE_ID_LENGTH = 11;
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2011-07-06 04:03:35 +00:00
|
|
|
static UMTX LOCK;
|
|
|
|
static UMTX TZSET_LOCK;
|
|
|
|
static icu::TimeZone* DEFAULT_ZONE = NULL;
|
|
|
|
static icu::TimeZone* _GMT = NULL; // cf. TimeZone::GMT
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2008-01-08 03:46:30 +00:00
|
|
|
static char TZDATA_VERSION[16];
|
2008-08-06 21:01:19 +00:00
|
|
|
static UBool TZDataVersionInitialized = FALSE;
|
2007-07-19 22:29:51 +00:00
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
static int32_t* MAP_SYSTEM_ZONES = NULL;
|
|
|
|
static int32_t* MAP_CANONICAL_SYSTEM_ZONES = NULL;
|
|
|
|
static int32_t* MAP_CANONICAL_SYSTEM_LOCATION_ZONES = NULL;
|
|
|
|
|
|
|
|
int32_t LEN_SYSTEM_ZONES = 0;
|
|
|
|
int32_t LEN_CANONICAL_SYSTEM_ZONES = 0;
|
|
|
|
int32_t LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
|
|
|
|
|
2004-09-12 23:07:29 +00:00
|
|
|
U_CDECL_BEGIN
|
2005-07-21 23:27:41 +00:00
|
|
|
static UBool U_CALLCONV timeZone_cleanup(void)
|
2003-05-08 21:08:06 +00:00
|
|
|
{
|
|
|
|
delete DEFAULT_ZONE;
|
|
|
|
DEFAULT_ZONE = NULL;
|
|
|
|
|
|
|
|
delete _GMT;
|
|
|
|
_GMT = NULL;
|
|
|
|
|
2007-08-08 18:38:20 +00:00
|
|
|
uprv_memset(TZDATA_VERSION, 0, sizeof(TZDATA_VERSION));
|
2008-08-06 21:01:19 +00:00
|
|
|
TZDataVersionInitialized = FALSE;
|
2007-08-08 18:38:20 +00:00
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
LEN_SYSTEM_ZONES = 0;
|
|
|
|
uprv_free(MAP_SYSTEM_ZONES);
|
|
|
|
MAP_SYSTEM_ZONES = 0;
|
|
|
|
|
|
|
|
LEN_CANONICAL_SYSTEM_ZONES = 0;
|
|
|
|
uprv_free(MAP_CANONICAL_SYSTEM_ZONES);
|
|
|
|
MAP_CANONICAL_SYSTEM_ZONES = 0;
|
|
|
|
|
|
|
|
LEN_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
|
|
|
|
uprv_free(MAP_CANONICAL_SYSTEM_LOCATION_ZONES);
|
|
|
|
MAP_CANONICAL_SYSTEM_LOCATION_ZONES = 0;
|
|
|
|
|
2001-08-31 02:21:34 +00:00
|
|
|
if (LOCK) {
|
|
|
|
umtx_destroy(&LOCK);
|
|
|
|
LOCK = NULL;
|
|
|
|
}
|
2007-11-26 18:25:01 +00:00
|
|
|
if (TZSET_LOCK) {
|
|
|
|
umtx_destroy(&TZSET_LOCK);
|
|
|
|
TZSET_LOCK = NULL;
|
|
|
|
}
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2001-08-31 02:21:34 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2004-09-12 23:07:29 +00:00
|
|
|
U_CDECL_END
|
2001-08-31 02:21:34 +00:00
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-11-13 02:31:11 +00:00
|
|
|
static int32_t findInStringArray(UResourceBundle* array, const UnicodeString& id, UErrorCode &status)
|
|
|
|
{
|
2004-05-24 23:35:35 +00:00
|
|
|
UnicodeString copy;
|
2005-03-03 23:46:10 +00:00
|
|
|
const UChar *u;
|
|
|
|
int32_t len;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2003-11-19 22:49:41 +00:00
|
|
|
int32_t start = 0;
|
2005-03-03 23:46:10 +00:00
|
|
|
int32_t limit = ures_getSize(array);
|
|
|
|
int32_t mid;
|
|
|
|
int32_t lastMid = INT32_MAX;
|
2011-05-04 12:23:42 +00:00
|
|
|
if(U_FAILURE(status) || (limit < 1)) {
|
2003-11-19 22:49:41 +00:00
|
|
|
return -1;
|
2003-11-13 02:31:11 +00:00
|
|
|
}
|
2005-03-03 23:46:10 +00:00
|
|
|
U_DEBUG_TZ_MSG(("fisa: Looking for %s, between %d and %d\n", U_DEBUG_TZ_STR(UnicodeString(id).getTerminatedBuffer()), start, limit));
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2005-03-03 23:46:10 +00:00
|
|
|
for (;;) {
|
|
|
|
mid = (int32_t)((start + limit) / 2);
|
|
|
|
if (lastMid == mid) { /* Have we moved? */
|
|
|
|
break; /* We haven't moved, and it wasn't found. */
|
|
|
|
}
|
|
|
|
lastMid = mid;
|
|
|
|
u = ures_getStringByIndex(array, mid, &len, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
U_DEBUG_TZ_MSG(("tz: compare to %s, %d .. [%d] .. %d\n", U_DEBUG_TZ_STR(u), start, mid, limit));
|
|
|
|
copy.setTo(TRUE, u, len);
|
|
|
|
int r = id.compare(copy);
|
|
|
|
if(r==0) {
|
|
|
|
U_DEBUG_TZ_MSG(("fisa: found at %d\n", mid));
|
|
|
|
return mid;
|
2003-11-19 22:49:41 +00:00
|
|
|
} else if(r<0) {
|
2005-03-03 23:46:10 +00:00
|
|
|
limit = mid;
|
2003-11-19 22:49:41 +00:00
|
|
|
} else {
|
2005-03-03 23:46:10 +00:00
|
|
|
start = mid;
|
2003-11-19 22:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
U_DEBUG_TZ_MSG(("fisa: not found\n"));
|
|
|
|
return -1;
|
2003-11-13 02:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-04 12:23:42 +00:00
|
|
|
* Fetch a specific zone by name. Replaces the getByKey call.
|
2003-11-13 02:31:11 +00:00
|
|
|
* @param top Top timezone resource
|
|
|
|
* @param id Time zone ID
|
|
|
|
* @param oldbundle Bundle for reuse (or NULL). see 'ures_open()'
|
|
|
|
* @return the zone's bundle if found, or undefined if error. Reuses oldbundle.
|
|
|
|
*/
|
|
|
|
static UResourceBundle* getZoneByName(const UResourceBundle* top, const UnicodeString& id, UResourceBundle *oldbundle, UErrorCode& status) {
|
2003-11-19 22:49:41 +00:00
|
|
|
// load the Rules object
|
|
|
|
UResourceBundle *tmp = ures_getByKey(top, kNAMES, NULL, &status);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2003-11-19 22:49:41 +00:00
|
|
|
// search for the string
|
|
|
|
int32_t idx = findInStringArray(tmp, id, status);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2003-11-19 22:49:41 +00:00
|
|
|
if((idx == -1) && U_SUCCESS(status)) {
|
2011-05-04 12:23:42 +00:00
|
|
|
// not found
|
2003-11-19 22:49:41 +00:00
|
|
|
status = U_MISSING_RESOURCE_ERROR;
|
|
|
|
//ures_close(oldbundle);
|
|
|
|
//oldbundle = NULL;
|
|
|
|
} else {
|
|
|
|
U_DEBUG_TZ_MSG(("gzbn: oldbundle= size %d, type %d, %s\n", ures_getSize(tmp), ures_getType(tmp), u_errorName(status)));
|
|
|
|
tmp = ures_getByKey(top, kZONES, tmp, &status); // get Zones object from top
|
|
|
|
U_DEBUG_TZ_MSG(("gzbn: loaded ZONES, size %d, type %d, path %s %s\n", ures_getSize(tmp), ures_getType(tmp), ures_getPath(tmp), u_errorName(status)));
|
|
|
|
oldbundle = ures_getByIndex(tmp, idx, oldbundle, &status); // get nth Zone object
|
|
|
|
U_DEBUG_TZ_MSG(("gzbn: loaded z#%d, size %d, type %d, path %s, %s\n", idx, ures_getSize(oldbundle), ures_getType(oldbundle), ures_getPath(oldbundle), u_errorName(status)));
|
|
|
|
}
|
|
|
|
ures_close(tmp);
|
2011-05-04 12:23:42 +00:00
|
|
|
if(U_FAILURE(status)) {
|
2003-11-19 22:49:41 +00:00
|
|
|
//ures_close(oldbundle);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return oldbundle;
|
|
|
|
}
|
2003-11-13 02:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UResourceBundle* TimeZone::loadRule(const UResourceBundle* top, const UnicodeString& ruleid, UResourceBundle* oldbundle, UErrorCode& status) {
|
2003-11-19 22:49:41 +00:00
|
|
|
char key[64];
|
2004-09-30 21:34:26 +00:00
|
|
|
ruleid.extract(0, sizeof(key)-1, key, (int32_t)sizeof(key)-1, US_INV);
|
2003-11-19 22:49:41 +00:00
|
|
|
U_DEBUG_TZ_MSG(("loadRule(%s)\n", key));
|
|
|
|
UResourceBundle *r = ures_getByKey(top, kRULES, oldbundle, &status);
|
|
|
|
U_DEBUG_TZ_MSG(("loadRule(%s) -> kRULES [%s]\n", key, u_errorName(status)));
|
|
|
|
r = ures_getByKey(r, key, r, &status);
|
|
|
|
U_DEBUG_TZ_MSG(("loadRule(%s) -> item [%s]\n", key, u_errorName(status)));
|
|
|
|
return r;
|
2003-11-13 02:31:11 +00:00
|
|
|
}
|
|
|
|
|
2003-08-19 01:02:59 +00:00
|
|
|
/**
|
|
|
|
* Given an ID, open the appropriate resource for the given time zone.
|
|
|
|
* Dereference aliases if necessary.
|
|
|
|
* @param id zone id
|
|
|
|
* @param res resource, which must be ready for use (initialized but not open)
|
|
|
|
* @param ec input-output error code
|
|
|
|
* @return top-level resource bundle
|
|
|
|
*/
|
|
|
|
static UResourceBundle* openOlsonResource(const UnicodeString& id,
|
|
|
|
UResourceBundle& res,
|
2003-11-19 22:49:41 +00:00
|
|
|
UErrorCode& ec)
|
|
|
|
{
|
|
|
|
#if U_DEBUG_TZ
|
2003-08-19 01:02:59 +00:00
|
|
|
char buf[128];
|
|
|
|
id.extract(0, sizeof(buf)-1, buf, sizeof(buf), "");
|
2003-11-19 22:49:41 +00:00
|
|
|
#endif
|
2003-11-13 02:31:11 +00:00
|
|
|
UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
|
|
|
|
U_DEBUG_TZ_MSG(("pre: res sz=%d\n", ures_getSize(&res)));
|
2003-11-24 22:50:45 +00:00
|
|
|
/* &res = */ getZoneByName(top, id, &res, ec);
|
2003-08-19 01:02:59 +00:00
|
|
|
// Dereference if this is an alias. Docs say result should be 1
|
|
|
|
// but it is 0 in 2.8 (?).
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("Loading zone '%s' (%s, size %d) - %s\n", buf, ures_getKey((UResourceBundle*)&res), ures_getSize(&res), u_errorName(ec)));
|
2011-03-01 18:29:13 +00:00
|
|
|
if (ures_getType(&res) == URES_INT) {
|
2003-11-19 22:49:41 +00:00
|
|
|
int32_t deref = ures_getInt(&res, &ec) + 0;
|
|
|
|
U_DEBUG_TZ_MSG(("getInt: %s - type is %d\n", u_errorName(ec), ures_getType(&res)));
|
|
|
|
UResourceBundle *ares = ures_getByKey(top, kZONES, NULL, &ec); // dereference Zones section
|
|
|
|
ures_getByIndex(ares, deref, &res, &ec);
|
|
|
|
ures_close(ares);
|
|
|
|
U_DEBUG_TZ_MSG(("alias to #%d (%s) - %s\n", deref, "??", u_errorName(ec)));
|
2003-11-13 02:31:11 +00:00
|
|
|
} else {
|
2003-11-19 22:49:41 +00:00
|
|
|
U_DEBUG_TZ_MSG(("not an alias - size %d\n", ures_getSize(&res)));
|
2003-08-19 01:02:59 +00:00
|
|
|
}
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("%s - final status is %s\n", buf, u_errorName(ec)));
|
2003-08-19 01:02:59 +00:00
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
// -------------------------------------
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
const TimeZone* U_EXPORT2
|
2001-10-08 23:26:58 +00:00
|
|
|
TimeZone::getGMT(void)
|
|
|
|
{
|
2006-09-08 04:10:40 +00:00
|
|
|
UBool needsInit;
|
|
|
|
UMTX_CHECK(&LOCK, (_GMT == NULL), needsInit); /* This is here to prevent race conditions. */
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
// Initialize _GMT independently of other static data; it should
|
|
|
|
// be valid even if we can't load the time zone UDataMemory.
|
2006-09-08 04:10:40 +00:00
|
|
|
if (needsInit) {
|
2008-08-06 21:01:19 +00:00
|
|
|
SimpleTimeZone *tmpGMT = new SimpleTimeZone(0, UnicodeString(TRUE, GMT_ID, GMT_ID_LENGTH));
|
2006-09-08 04:10:40 +00:00
|
|
|
umtx_lock(&LOCK);
|
|
|
|
if (_GMT == 0) {
|
2008-08-06 21:01:19 +00:00
|
|
|
_GMT = tmpGMT;
|
|
|
|
tmpGMT = NULL;
|
2006-09-08 04:10:40 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(&LOCK);
|
2008-08-06 21:01:19 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
|
|
|
delete tmpGMT;
|
2001-10-08 23:26:58 +00:00
|
|
|
}
|
2002-12-09 18:10:43 +00:00
|
|
|
return _GMT;
|
2001-10-08 23:26:58 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// *****************************************************************************
|
|
|
|
// class TimeZone
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2004-06-04 01:16:05 +00:00
|
|
|
UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(TimeZone)
|
2004-05-28 20:13:11 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::TimeZone()
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(), fID()
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2001-08-16 00:31:06 +00:00
|
|
|
TimeZone::TimeZone(const UnicodeString &id)
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(), fID(id)
|
2001-08-16 00:31:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::~TimeZone()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
TimeZone::TimeZone(const TimeZone &source)
|
2002-07-02 23:58:34 +00:00
|
|
|
: UObject(source), fID(source.fID)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
TimeZone &
|
|
|
|
TimeZone::operator=(const TimeZone &right)
|
|
|
|
{
|
|
|
|
if (this != &right) fID = right.fID;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::operator==(const TimeZone& that) const
|
|
|
|
{
|
2010-05-19 17:29:33 +00:00
|
|
|
return typeid(*this) == typeid(that) &&
|
1999-08-16 21:50:52 +00:00
|
|
|
fID == that.fID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
TimeZone* U_EXPORT2
|
1999-12-05 06:24:22 +00:00
|
|
|
TimeZone::createTimeZone(const UnicodeString& ID)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-12-05 06:24:22 +00:00
|
|
|
/* We first try to lookup the zone ID in our system list. If this
|
|
|
|
* fails, we try to parse it as a custom string GMT[+-]hh:mm. If
|
|
|
|
* all else fails, we return GMT, which is probably not what the
|
|
|
|
* user wants, but at least is a functioning TimeZone object.
|
2003-08-15 18:21:31 +00:00
|
|
|
*
|
|
|
|
* We cannot return NULL, because that would break compatibility
|
|
|
|
* with the JDK.
|
1999-12-05 06:24:22 +00:00
|
|
|
*/
|
2003-08-15 18:21:31 +00:00
|
|
|
TimeZone* result = createSystemTimeZone(ID);
|
2003-05-08 21:08:06 +00:00
|
|
|
|
1999-12-05 06:24:22 +00:00
|
|
|
if (result == 0) {
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("failed to load system time zone with id - falling to custom"));
|
1999-12-05 06:24:22 +00:00
|
|
|
result = createCustomTimeZone(ID);
|
|
|
|
}
|
|
|
|
if (result == 0) {
|
2011-02-19 06:29:52 +00:00
|
|
|
U_DEBUG_TZ_MSG(("failed to load time zone with id - falling to Etc/Unknown(GMT)"));
|
2011-07-07 18:46:19 +00:00
|
|
|
result = new SimpleTimeZone(0, UnicodeString(TRUE, UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-12-05 06:24:22 +00:00
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
/**
|
|
|
|
* Lookup the given name in our system zone table. If found,
|
|
|
|
* instantiate a new zone of that name and return it. If not
|
|
|
|
* found, return 0.
|
|
|
|
*/
|
|
|
|
TimeZone*
|
2003-08-15 18:21:31 +00:00
|
|
|
TimeZone::createSystemTimeZone(const UnicodeString& id) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2011-03-01 18:29:13 +00:00
|
|
|
return createSystemTimeZone(id, ec);
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeZone*
|
|
|
|
TimeZone::createSystemTimeZone(const UnicodeString& id, UErrorCode& ec) {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
TimeZone* z = 0;
|
2003-08-15 18:21:31 +00:00
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("pre-err=%s\n", u_errorName(ec)));
|
2003-08-19 00:24:49 +00:00
|
|
|
UResourceBundle *top = openOlsonResource(id, res, ec);
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("post-err=%s\n", u_errorName(ec)));
|
2003-08-15 18:21:31 +00:00
|
|
|
if (U_SUCCESS(ec)) {
|
2011-05-03 14:59:01 +00:00
|
|
|
z = new OlsonTimeZone(top, &res, id, ec);
|
|
|
|
if (z == NULL) {
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("cstz: olson time zone failed to initialize - err %s\n", u_errorName(ec)));
|
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
ures_close(top);
|
|
|
|
if (U_FAILURE(ec)) {
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("cstz: failed to create, err %s\n", u_errorName(ec)));
|
2003-08-15 18:21:31 +00:00
|
|
|
delete z;
|
|
|
|
z = 0;
|
|
|
|
}
|
|
|
|
return z;
|
2003-05-08 21:08:06 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
/**
|
|
|
|
* Initialize DEFAULT_ZONE from the system default time zone. The
|
|
|
|
* caller should confirm that DEFAULT_ZONE is NULL before calling.
|
|
|
|
* Upon return, DEFAULT_ZONE will not be NULL, unless operator new()
|
|
|
|
* returns NULL.
|
|
|
|
*
|
|
|
|
* Must be called OUTSIDE mutex.
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
TimeZone::initDefault()
|
2011-05-04 12:23:42 +00:00
|
|
|
{
|
2002-12-09 18:10:43 +00:00
|
|
|
// We access system timezone data through TPlatformUtilities,
|
|
|
|
// including tzset(), timezone, and tzname[].
|
|
|
|
int32_t rawOffset = 0;
|
|
|
|
const char *hostID;
|
|
|
|
|
|
|
|
// First, try to create a system timezone, based
|
|
|
|
// on the string ID in tzname[0].
|
|
|
|
{
|
2007-11-26 18:25:01 +00:00
|
|
|
// NOTE: Local mutex here. TimeZone mutex below
|
|
|
|
// mutexed to avoid threading issues in the platform functions.
|
|
|
|
// Some of the locale/timezone OS functions may not be thread safe,
|
|
|
|
// so the intent is that any setting from anywhere within ICU
|
|
|
|
// happens while the ICU mutex is held.
|
|
|
|
// The operating system might actually use ICU to implement timezones.
|
|
|
|
// So we may have ICU calling ICU here, like on AIX.
|
|
|
|
// In order to prevent a double lock of a non-reentrant mutex in a
|
|
|
|
// different part of ICU, we use TZSET_LOCK to allow only one instance
|
|
|
|
// of ICU to query these thread unsafe OS functions at any given time.
|
|
|
|
Mutex lock(&TZSET_LOCK);
|
|
|
|
|
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
2002-12-09 18:10:43 +00:00
|
|
|
uprv_tzset(); // Initialize tz... system data
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2003-05-02 00:11:32 +00:00
|
|
|
// Get the timezone ID from the host. This function should do
|
|
|
|
// any required host-specific remapping; e.g., on Windows this
|
|
|
|
// function maps the Date and Time control panel setting to an
|
|
|
|
// ICU timezone ID.
|
2002-12-09 18:10:43 +00:00
|
|
|
hostID = uprv_tzname(0);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2002-12-09 18:10:43 +00:00
|
|
|
// Invert sign because UNIX semantics are backwards
|
|
|
|
rawOffset = uprv_timezone() * -U_MILLIS_PER_SECOND;
|
|
|
|
}
|
1999-12-05 06:24:22 +00:00
|
|
|
|
2007-11-26 18:25:01 +00:00
|
|
|
UBool initialized;
|
|
|
|
UMTX_CHECK(&LOCK, (DEFAULT_ZONE != NULL), initialized);
|
|
|
|
if (initialized) {
|
|
|
|
/* Hrmph? Either a race condition happened, or tzset initialized ICU. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
TimeZone* default_zone = NULL;
|
2002-12-09 18:10:43 +00:00
|
|
|
|
2004-08-31 20:31:12 +00:00
|
|
|
/* Make sure that the string is NULL terminated to prevent BoundsChecker/Purify warnings. */
|
|
|
|
UnicodeString hostStrID(hostID, -1, US_INV);
|
|
|
|
hostStrID.append((UChar)0);
|
|
|
|
hostStrID.truncate(hostStrID.length()-1);
|
2006-03-03 20:10:02 +00:00
|
|
|
default_zone = createSystemTimeZone(hostStrID);
|
|
|
|
|
2009-09-09 23:39:16 +00:00
|
|
|
#ifdef U_WINDOWS
|
|
|
|
// hostID points to a heap-allocated location on Windows.
|
|
|
|
uprv_free(const_cast<char *>(hostID));
|
|
|
|
#endif
|
|
|
|
|
2006-03-03 17:46:26 +00:00
|
|
|
int32_t hostIDLen = hostStrID.length();
|
2006-03-03 20:10:02 +00:00
|
|
|
if (default_zone != NULL && rawOffset != default_zone->getRawOffset()
|
|
|
|
&& (3 <= hostIDLen && hostIDLen <= 4))
|
2006-03-03 17:46:26 +00:00
|
|
|
{
|
2006-03-03 20:10:02 +00:00
|
|
|
// Uh oh. This probably wasn't a good id.
|
|
|
|
// It was probably an ambiguous abbreviation
|
|
|
|
delete default_zone;
|
|
|
|
default_zone = NULL;
|
2006-03-03 17:46:26 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
|
|
|
|
// Construct a fixed standard zone with the host's ID
|
|
|
|
// and raw offset.
|
|
|
|
if (default_zone == NULL) {
|
2004-08-31 20:37:20 +00:00
|
|
|
default_zone = new SimpleTimeZone(rawOffset, hostStrID);
|
2003-08-15 18:21:31 +00:00
|
|
|
}
|
2002-12-09 18:10:43 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
// If we _still_ don't have a time zone, use GMT.
|
2002-12-09 18:10:43 +00:00
|
|
|
if (default_zone == NULL) {
|
2008-02-05 19:14:43 +00:00
|
|
|
const TimeZone* temptz = getGMT();
|
|
|
|
// If we can't use GMT, get out.
|
|
|
|
if (temptz == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2008-01-16 17:13:11 +00:00
|
|
|
default_zone = temptz->clone();
|
2002-12-09 18:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If DEFAULT_ZONE is still NULL, set it up.
|
|
|
|
umtx_lock(&LOCK);
|
|
|
|
if (DEFAULT_ZONE == NULL) {
|
|
|
|
DEFAULT_ZONE = default_zone;
|
|
|
|
default_zone = NULL;
|
2004-09-12 23:07:29 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
2002-12-09 18:10:43 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(&LOCK);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2002-12-09 18:10:43 +00:00
|
|
|
delete default_zone;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
TimeZone* U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::createDefault()
|
|
|
|
{
|
2006-08-29 04:57:05 +00:00
|
|
|
/* This is here to prevent race conditions. */
|
|
|
|
UBool needsInit;
|
2007-11-26 18:25:01 +00:00
|
|
|
UMTX_CHECK(&LOCK, (DEFAULT_ZONE == NULL), needsInit);
|
2006-08-29 04:57:05 +00:00
|
|
|
if (needsInit) {
|
2003-05-08 21:08:06 +00:00
|
|
|
initDefault();
|
|
|
|
}
|
|
|
|
|
1999-12-05 06:24:22 +00:00
|
|
|
Mutex lock(&LOCK); // In case adoptDefault is called
|
2007-11-26 18:25:01 +00:00
|
|
|
return (DEFAULT_ZONE != NULL) ? DEFAULT_ZONE->clone() : NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::adoptDefault(TimeZone* zone)
|
|
|
|
{
|
|
|
|
if (zone != NULL)
|
|
|
|
{
|
2002-12-09 18:10:43 +00:00
|
|
|
TimeZone* old = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-12-09 18:10:43 +00:00
|
|
|
umtx_lock(&LOCK);
|
|
|
|
old = DEFAULT_ZONE;
|
2001-08-31 02:21:34 +00:00
|
|
|
DEFAULT_ZONE = zone;
|
2002-12-09 18:10:43 +00:00
|
|
|
umtx_unlock(&LOCK);
|
|
|
|
|
|
|
|
delete old;
|
2004-09-12 23:07:29 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// -------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
void U_EXPORT2
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::setDefault(const TimeZone& zone)
|
|
|
|
{
|
|
|
|
adoptDefault(zone.clone());
|
|
|
|
}
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the default implementation for subclasses that do not
|
|
|
|
* override this method. This implementation calls through to the
|
|
|
|
* 8-argument getOffset() method after suitable computations, and
|
|
|
|
* correctly adjusts GMT millis to local millis when necessary.
|
|
|
|
*/
|
|
|
|
void TimeZone::getOffset(UDate date, UBool local, int32_t& rawOffset,
|
|
|
|
int32_t& dstOffset, UErrorCode& ec) const {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rawOffset = getRawOffset();
|
|
|
|
if (!local) {
|
|
|
|
date += rawOffset; // now in local standard millis
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
// When local == TRUE, date might not be in local standard
|
|
|
|
// millis. getOffset taking 7 parameters used here assume
|
|
|
|
// the given time in day is local standard time.
|
|
|
|
// At STD->DST transition, there is a range of time which
|
|
|
|
// does not exist. When 'date' is in this time range
|
|
|
|
// (and local == TRUE), this method interprets the specified
|
|
|
|
// local time as DST. At DST->STD transition, there is a
|
|
|
|
// range of time which occurs twice. In this case, this
|
|
|
|
// method interprets the specified local time as STD.
|
|
|
|
// To support the behavior above, we need to call getOffset
|
|
|
|
// (with 7 args) twice when local == true and DST is
|
|
|
|
// detected in the initial call.
|
2003-08-15 18:21:31 +00:00
|
|
|
for (int32_t pass=0; ; ++pass) {
|
|
|
|
int32_t year, month, dom, dow;
|
|
|
|
double day = uprv_floor(date / U_MILLIS_PER_DAY);
|
|
|
|
int32_t millis = (int32_t) (date - day * U_MILLIS_PER_DAY);
|
2007-11-28 20:49:58 +00:00
|
|
|
|
2003-09-02 17:46:43 +00:00
|
|
|
Grego::dayToFields(day, year, month, dom, dow);
|
2007-11-28 20:49:58 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
dstOffset = getOffset(GregorianCalendar::AD, year, month, dom,
|
|
|
|
(uint8_t) dow, millis,
|
2003-09-02 17:46:43 +00:00
|
|
|
Grego::monthLength(year, month),
|
2003-08-15 18:21:31 +00:00
|
|
|
ec) - rawOffset;
|
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
// Recompute if local==TRUE, dstOffset!=0.
|
|
|
|
if (pass!=0 || !local || dstOffset == 0) {
|
2003-08-15 18:21:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-11-28 20:49:58 +00:00
|
|
|
// adjust to local standard millis
|
|
|
|
date -= dstOffset;
|
2003-08-15 18:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// -------------------------------------
|
|
|
|
|
2002-11-12 23:52:27 +00:00
|
|
|
// New available IDs API as of ICU 2.4. Uses StringEnumeration API.
|
|
|
|
|
|
|
|
class TZEnumeration : public StringEnumeration {
|
2004-12-30 07:45:48 +00:00
|
|
|
private:
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
// Map into to zones. Our results are zone[map[i]] for
|
|
|
|
// i=0..len-1, where zone[i] is the i-th Olson zone. If map==NULL
|
|
|
|
// then our results are zone[i] for i=0..len-1. Len will be zero
|
2011-03-01 18:29:13 +00:00
|
|
|
// if the zone data could not be loaded.
|
2002-11-12 23:52:27 +00:00
|
|
|
int32_t* map;
|
2011-05-04 22:34:13 +00:00
|
|
|
int32_t* localMap;
|
2002-11-12 23:52:27 +00:00
|
|
|
int32_t len;
|
|
|
|
int32_t pos;
|
2011-03-01 18:29:13 +00:00
|
|
|
|
|
|
|
TZEnumeration(int32_t* mapData, int32_t mapLen, UBool adoptMapData) : pos(0) {
|
|
|
|
map = mapData;
|
|
|
|
localMap = adoptMapData ? mapData : NULL;
|
|
|
|
len = mapLen;
|
|
|
|
}
|
2002-11-12 23:52:27 +00:00
|
|
|
|
2004-12-30 07:45:48 +00:00
|
|
|
UBool getID(int32_t i) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
int32_t idLen = 0;
|
|
|
|
const UChar* id = NULL;
|
|
|
|
UResourceBundle *top = ures_openDirect(0, kZONEINFO, &ec);
|
|
|
|
top = ures_getByKey(top, kNAMES, top, &ec); // dereference Zones section
|
|
|
|
id = ures_getStringByIndex(top, i, &idLen, &ec);
|
|
|
|
if(U_FAILURE(ec)) {
|
|
|
|
unistr.truncate(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unistr.fastCopyFrom(UnicodeString(TRUE, id, idLen));
|
|
|
|
}
|
|
|
|
ures_close(top);
|
|
|
|
return U_SUCCESS(ec);
|
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
static int32_t* getMap(USystemTimeZoneType type, int32_t& len, UErrorCode& ec) {
|
|
|
|
len = 0;
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
int32_t* m = NULL;
|
|
|
|
switch (type) {
|
|
|
|
case UCAL_ZONE_TYPE_ANY:
|
|
|
|
m = MAP_SYSTEM_ZONES;
|
|
|
|
len = LEN_SYSTEM_ZONES;
|
|
|
|
break;
|
|
|
|
case UCAL_ZONE_TYPE_CANONICAL:
|
|
|
|
m = MAP_CANONICAL_SYSTEM_ZONES;
|
|
|
|
len = LEN_CANONICAL_SYSTEM_ZONES;
|
|
|
|
break;
|
|
|
|
case UCAL_ZONE_TYPE_CANONICAL_LOCATION:
|
|
|
|
m = MAP_CANONICAL_SYSTEM_LOCATION_ZONES;
|
|
|
|
len = LEN_CANONICAL_SYSTEM_LOCATION_ZONES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UBool needsInit = FALSE;
|
|
|
|
UMTX_CHECK(&LOCK, (len == 0), needsInit);
|
|
|
|
if (needsInit) {
|
|
|
|
m = initMap(type, len, ec);
|
|
|
|
}
|
|
|
|
return m;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
static int32_t* initMap(USystemTimeZoneType type, int32_t& len, UErrorCode& ec) {
|
|
|
|
len = 0;
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
2003-08-15 18:21:31 +00:00
|
|
|
}
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
int32_t *result = NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec);
|
|
|
|
res = ures_getByKey(res, kNAMES, res, &ec); // dereference Zones section
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
int32_t size = ures_getSize(res);
|
|
|
|
int32_t *m = (int32_t *)uprv_malloc(size * sizeof(int32_t));
|
|
|
|
if (m == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
} else {
|
|
|
|
int32_t numEntries = 0;
|
|
|
|
for (int32_t i = 0; i < size; i++) {
|
2011-07-07 18:46:19 +00:00
|
|
|
UnicodeString id = ures_getUnicodeStringByIndex(res, i, &ec);
|
2011-03-01 18:29:13 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 18:46:19 +00:00
|
|
|
if (0 == id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH)) {
|
2011-03-01 18:29:13 +00:00
|
|
|
// exclude Etc/Unknown
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (type == UCAL_ZONE_TYPE_CANONICAL || type == UCAL_ZONE_TYPE_CANONICAL_LOCATION) {
|
|
|
|
UnicodeString canonicalID;
|
|
|
|
ZoneMeta::getCanonicalCLDRID(id, canonicalID, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
break;
|
|
|
|
}
|
2011-07-07 18:46:19 +00:00
|
|
|
if (canonicalID != id) {
|
2011-03-01 18:29:13 +00:00
|
|
|
// exclude aliases
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == UCAL_ZONE_TYPE_CANONICAL_LOCATION) {
|
|
|
|
const UChar *region = TimeZone::getRegion(id, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (u_strcmp(region, WORLD) == 0) {
|
|
|
|
// exclude non-location ("001")
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m[numEntries++] = i;
|
|
|
|
}
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
int32_t *tmp = m;
|
|
|
|
m = (int32_t *)uprv_realloc(tmp, numEntries * sizeof(int32_t));
|
|
|
|
if (m == NULL) {
|
|
|
|
// realloc failed.. use the original one even it has unused
|
|
|
|
// area at the end
|
|
|
|
m = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
umtx_lock(&LOCK);
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case UCAL_ZONE_TYPE_ANY:
|
|
|
|
if (MAP_SYSTEM_ZONES == NULL) {
|
|
|
|
MAP_SYSTEM_ZONES = m;
|
|
|
|
LEN_SYSTEM_ZONES = numEntries;
|
|
|
|
m = NULL;
|
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
|
|
|
}
|
|
|
|
result = MAP_SYSTEM_ZONES;
|
|
|
|
len = LEN_SYSTEM_ZONES;
|
|
|
|
break;
|
|
|
|
case UCAL_ZONE_TYPE_CANONICAL:
|
|
|
|
if (MAP_CANONICAL_SYSTEM_ZONES == NULL) {
|
|
|
|
MAP_CANONICAL_SYSTEM_ZONES = m;
|
|
|
|
LEN_CANONICAL_SYSTEM_ZONES = numEntries;
|
|
|
|
m = NULL;
|
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
|
|
|
}
|
|
|
|
result = MAP_CANONICAL_SYSTEM_ZONES;
|
|
|
|
len = LEN_CANONICAL_SYSTEM_ZONES;
|
|
|
|
break;
|
|
|
|
case UCAL_ZONE_TYPE_CANONICAL_LOCATION:
|
|
|
|
if (MAP_CANONICAL_SYSTEM_LOCATION_ZONES == NULL) {
|
|
|
|
MAP_CANONICAL_SYSTEM_LOCATION_ZONES = m;
|
|
|
|
LEN_CANONICAL_SYSTEM_LOCATION_ZONES = numEntries;
|
|
|
|
m = NULL;
|
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
|
|
|
}
|
|
|
|
result = MAP_CANONICAL_SYSTEM_LOCATION_ZONES;
|
|
|
|
len = LEN_CANONICAL_SYSTEM_LOCATION_ZONES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
umtx_unlock(&LOCK);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
uprv_free(m);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
|
|
|
|
ures_close(res);
|
|
|
|
return result;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
#define DEFAULT_FILTERED_MAP_SIZE 8
|
|
|
|
#define MAP_INCREMENT_SIZE 8
|
|
|
|
|
|
|
|
static TZEnumeration* create(USystemTimeZoneType type, const char* region, const int32_t* rawOffset, UErrorCode& ec) {
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
int32_t baseLen;
|
|
|
|
int32_t *baseMap = getMap(type, baseLen, ec);
|
|
|
|
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any additional conditions are available,
|
|
|
|
// create instance local map filtered by the conditions.
|
|
|
|
|
|
|
|
int32_t *filteredMap = NULL;
|
|
|
|
int32_t numEntries = 0;
|
|
|
|
|
|
|
|
if (region != NULL || rawOffset != NULL) {
|
|
|
|
int32_t filteredMapSize = DEFAULT_FILTERED_MAP_SIZE;
|
|
|
|
filteredMap = (int32_t *)uprv_malloc(filteredMapSize * sizeof(int32_t));
|
|
|
|
if (filteredMap == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
2010-02-24 07:54:30 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
// Walk through the base map
|
|
|
|
UResourceBundle *res = ures_openDirect(0, kZONEINFO, &ec);
|
|
|
|
res = ures_getByKey(res, kNAMES, res, &ec); // dereference Zones section
|
|
|
|
for (int32_t i = 0; i < baseLen; i++) {
|
|
|
|
int32_t zidx = baseMap[i];
|
2011-07-07 18:46:19 +00:00
|
|
|
UnicodeString id = ures_getUnicodeStringByIndex(res, zidx, &ec);
|
2010-02-24 07:54:30 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
break;
|
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
if (region != NULL) {
|
|
|
|
// Filter by region
|
|
|
|
char tzregion[4]; // max 3 letters + null term
|
|
|
|
TimeZone::getRegion(id, tzregion, sizeof(tzregion), ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (uprv_stricmp(tzregion, region) != 0) {
|
|
|
|
// region does not match
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-24 07:54:30 +00:00
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
if (rawOffset != NULL) {
|
|
|
|
// Filter by raw offset
|
|
|
|
// Note: This is VERY inefficient
|
|
|
|
TimeZone *z = TimeZone::createSystemTimeZone(id, ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int32_t tzoffset = z->getRawOffset();
|
|
|
|
delete z;
|
2010-02-24 07:54:30 +00:00
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
if (tzoffset != *rawOffset) {
|
|
|
|
continue;
|
2010-02-24 07:54:30 +00:00
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (filteredMapSize <= numEntries) {
|
|
|
|
filteredMapSize += MAP_INCREMENT_SIZE;
|
|
|
|
int32_t *tmp = (int32_t *)uprv_realloc(filteredMap, filteredMapSize * sizeof(int32_t));
|
|
|
|
if (tmp == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
break;
|
2010-02-24 07:54:30 +00:00
|
|
|
} else {
|
2011-03-01 18:29:13 +00:00
|
|
|
filteredMap = tmp;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
|
|
|
|
filteredMap[numEntries++] = zidx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
uprv_free(filteredMap);
|
|
|
|
filteredMap = NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
|
|
|
|
ures_close(res);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
TZEnumeration *result = NULL;
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
// Finally, create a new enumeration instance
|
|
|
|
if (filteredMap == NULL) {
|
|
|
|
result = new TZEnumeration(baseMap, baseLen, FALSE);
|
2003-11-05 21:59:41 +00:00
|
|
|
} else {
|
2011-03-01 18:29:13 +00:00
|
|
|
result = new TZEnumeration(filteredMap, numEntries, TRUE);
|
|
|
|
filteredMap = NULL;
|
|
|
|
}
|
|
|
|
if (result == NULL) {
|
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filteredMap != NULL) {
|
|
|
|
uprv_free(filteredMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TZEnumeration(const TZEnumeration &other) : StringEnumeration(), map(NULL), localMap(NULL), len(0), pos(0) {
|
|
|
|
if (other.localMap != NULL) {
|
|
|
|
localMap = (int32_t *)uprv_malloc(other.len * sizeof(int32_t));
|
|
|
|
if (localMap != NULL) {
|
2003-11-05 21:59:41 +00:00
|
|
|
len = other.len;
|
2011-03-01 18:29:13 +00:00
|
|
|
uprv_memcpy(localMap, other.localMap, len * sizeof(int32_t));
|
2003-11-05 21:59:41 +00:00
|
|
|
pos = other.pos;
|
2011-03-01 18:29:13 +00:00
|
|
|
map = localMap;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
pos = 0;
|
|
|
|
map = NULL;
|
2003-11-05 21:59:41 +00:00
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
} else {
|
|
|
|
map = other.map;
|
|
|
|
localMap = NULL;
|
|
|
|
len = other.len;
|
|
|
|
pos = other.pos;
|
2003-11-05 21:59:41 +00:00
|
|
|
}
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2003-11-05 21:59:41 +00:00
|
|
|
virtual ~TZEnumeration() {
|
2011-03-01 18:29:13 +00:00
|
|
|
if (localMap != NULL) {
|
|
|
|
uprv_free(localMap);
|
|
|
|
}
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2003-11-05 21:59:41 +00:00
|
|
|
virtual StringEnumeration *clone() const {
|
|
|
|
return new TZEnumeration(*this);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-27 16:45:16 +00:00
|
|
|
virtual int32_t count(UErrorCode& status) const {
|
2003-11-05 21:59:41 +00:00
|
|
|
return U_FAILURE(status) ? 0 : len;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-27 16:45:16 +00:00
|
|
|
virtual const UnicodeString* snext(UErrorCode& status) {
|
2011-03-01 18:29:13 +00:00
|
|
|
if (U_SUCCESS(status) && map != NULL && pos < len) {
|
|
|
|
getID(map[pos]);
|
2003-08-15 18:21:31 +00:00
|
|
|
++pos;
|
2003-11-05 21:59:41 +00:00
|
|
|
return &unistr;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
return 0;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-27 16:45:16 +00:00
|
|
|
virtual void reset(UErrorCode& /*status*/) {
|
2002-11-12 23:52:27 +00:00
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-29 22:22:08 +00:00
|
|
|
public:
|
2004-08-24 17:38:33 +00:00
|
|
|
static UClassID U_EXPORT2 getStaticClassID(void);
|
|
|
|
virtual UClassID getDynamicClassID(void) const;
|
2002-11-12 23:52:27 +00:00
|
|
|
};
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(TZEnumeration)
|
2002-11-26 00:46:48 +00:00
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
|
|
|
TimeZone::createTimeZoneIDEnumeration(
|
|
|
|
USystemTimeZoneType zoneType,
|
|
|
|
const char* region,
|
|
|
|
const int32_t* rawOffset,
|
|
|
|
UErrorCode& ec) {
|
|
|
|
return TZEnumeration::create(zoneType, region, rawOffset, ec);
|
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
2002-11-12 23:52:27 +00:00
|
|
|
TimeZone::createEnumeration() {
|
2011-03-01 18:29:13 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, NULL, ec);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
2002-11-12 23:52:27 +00:00
|
|
|
TimeZone::createEnumeration(int32_t rawOffset) {
|
2011-03-01 18:29:13 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, NULL, &rawOffset, ec);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
StringEnumeration* U_EXPORT2
|
2002-11-12 23:52:27 +00:00
|
|
|
TimeZone::createEnumeration(const char* country) {
|
2011-03-01 18:29:13 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
return TZEnumeration::create(UCAL_ZONE_TYPE_ANY, country, NULL, ec);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// ---------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
int32_t U_EXPORT2
|
2000-09-27 19:16:54 +00:00
|
|
|
TimeZone::countEquivalentIDs(const UnicodeString& id) {
|
2003-08-19 00:24:49 +00:00
|
|
|
int32_t result = 0;
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("countEquivalentIDs..\n"));
|
2003-08-19 00:24:49 +00:00
|
|
|
UResourceBundle *top = openOlsonResource(id, res, ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
2010-02-24 07:54:30 +00:00
|
|
|
UResourceBundle r;
|
|
|
|
ures_initStackObject(&r);
|
|
|
|
ures_getByKey(&res, kLINKS, &r, &ec);
|
|
|
|
ures_getIntVector(&r, &result, &ec);
|
|
|
|
ures_close(&r);
|
2003-08-19 00:24:49 +00:00
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
ures_close(top);
|
|
|
|
return result;
|
2000-09-27 19:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
|
2004-08-24 17:38:33 +00:00
|
|
|
const UnicodeString U_EXPORT2
|
2000-09-27 19:16:54 +00:00
|
|
|
TimeZone::getEquivalentID(const UnicodeString& id, int32_t index) {
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("gEI(%d)\n", index));
|
2003-08-19 00:24:49 +00:00
|
|
|
UnicodeString result;
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
|
|
|
UResourceBundle *top = openOlsonResource(id, res, ec);
|
|
|
|
int32_t zone = -1;
|
|
|
|
if (U_SUCCESS(ec)) {
|
2010-02-24 07:54:30 +00:00
|
|
|
UResourceBundle r;
|
|
|
|
ures_initStackObject(&r);
|
|
|
|
int32_t size;
|
|
|
|
ures_getByKey(&res, kLINKS, &r, &ec);
|
|
|
|
const int32_t* v = ures_getIntVector(&r, &size, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
2011-03-01 18:29:13 +00:00
|
|
|
if (index >= 0 && index < size) {
|
2003-11-13 02:31:11 +00:00
|
|
|
zone = v[index];
|
2003-08-19 00:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-24 07:54:30 +00:00
|
|
|
ures_close(&r);
|
2003-08-19 00:24:49 +00:00
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
if (zone >= 0) {
|
2003-11-13 02:31:11 +00:00
|
|
|
UResourceBundle *ares = ures_getByKey(top, kNAMES, NULL, &ec); // dereference Zones section
|
2003-08-19 00:24:49 +00:00
|
|
|
if (U_SUCCESS(ec)) {
|
2003-11-19 20:07:43 +00:00
|
|
|
int32_t idLen = 0;
|
|
|
|
const UChar* id = ures_getStringByIndex(ares, zone, &idLen, &ec);
|
|
|
|
result.fastCopyFrom(UnicodeString(TRUE, id, idLen));
|
|
|
|
U_DEBUG_TZ_MSG(("gei(%d) -> %d, len%d, %s\n", index, zone, result.length(), u_errorName(ec)));
|
2003-08-19 00:24:49 +00:00
|
|
|
}
|
2003-11-13 02:31:11 +00:00
|
|
|
ures_close(ares);
|
2003-08-19 00:24:49 +00:00
|
|
|
}
|
|
|
|
ures_close(top);
|
2003-11-13 02:31:11 +00:00
|
|
|
#if defined(U_DEBUG_TZ)
|
|
|
|
if(result.length() ==0) {
|
|
|
|
U_DEBUG_TZ_MSG(("equiv [__, #%d] -> 0 (%s)\n", index, u_errorName(ec)));
|
|
|
|
}
|
|
|
|
#endif
|
2003-08-19 00:24:49 +00:00
|
|
|
return result;
|
2000-09-27 19:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
|
2011-05-03 14:59:01 +00:00
|
|
|
// These methods are used by ZoneMeta class only.
|
|
|
|
|
|
|
|
const UChar*
|
|
|
|
TimeZone::findID(const UnicodeString& id) {
|
|
|
|
const UChar *result = NULL;
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec);
|
|
|
|
|
|
|
|
// resolve zone index by name
|
|
|
|
UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec);
|
|
|
|
int32_t idx = findInStringArray(names, id, ec);
|
|
|
|
result = ures_getStringByIndex(names, idx, NULL, &ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
ures_close(names);
|
|
|
|
ures_close(rb);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-02-12 19:00:53 +00:00
|
|
|
|
|
|
|
const UChar*
|
|
|
|
TimeZone::dereferOlsonLink(const UnicodeString& id) {
|
|
|
|
const UChar *result = NULL;
|
2007-11-28 20:49:58 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2010-02-12 19:00:53 +00:00
|
|
|
UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &ec);
|
|
|
|
|
|
|
|
// resolve zone index by name
|
|
|
|
UResourceBundle *names = ures_getByKey(rb, kNAMES, NULL, &ec);
|
|
|
|
int32_t idx = findInStringArray(names, id, ec);
|
|
|
|
result = ures_getStringByIndex(names, idx, NULL, &ec);
|
|
|
|
|
|
|
|
// open the zone bundle by index
|
|
|
|
ures_getByKey(rb, kZONES, rb, &ec);
|
|
|
|
ures_getByIndex(rb, idx, rb, &ec);
|
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
if (U_SUCCESS(ec)) {
|
2010-02-24 07:54:30 +00:00
|
|
|
if (ures_getType(rb) == URES_INT) {
|
2010-02-12 19:00:53 +00:00
|
|
|
// this is a link - dereference the link
|
|
|
|
int32_t deref = ures_getInt(rb, &ec);
|
|
|
|
const UChar* tmp = ures_getStringByIndex(names, deref, NULL, &ec);
|
2007-11-28 20:49:58 +00:00
|
|
|
if (U_SUCCESS(ec)) {
|
2010-02-12 19:00:53 +00:00
|
|
|
result = tmp;
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-12 19:00:53 +00:00
|
|
|
|
|
|
|
ures_close(names);
|
|
|
|
ures_close(rb);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-02-24 07:54:30 +00:00
|
|
|
const UChar*
|
|
|
|
TimeZone::getRegion(const UnicodeString& id) {
|
2011-03-01 18:29:13 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
return getRegion(id, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar*
|
|
|
|
TimeZone::getRegion(const UnicodeString& id, UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-18 23:03:47 +00:00
|
|
|
const UChar *result = NULL;
|
2011-03-01 18:29:13 +00:00
|
|
|
UResourceBundle *rb = ures_openDirect(NULL, kZONEINFO, &status);
|
2010-02-24 07:54:30 +00:00
|
|
|
|
|
|
|
// resolve zone index by name
|
2011-03-01 18:29:13 +00:00
|
|
|
UResourceBundle *res = ures_getByKey(rb, kNAMES, NULL, &status);
|
|
|
|
int32_t idx = findInStringArray(res, id, status);
|
2010-02-24 07:54:30 +00:00
|
|
|
|
|
|
|
// get region mapping
|
2011-03-01 18:29:13 +00:00
|
|
|
ures_getByKey(rb, kREGIONS, res, &status);
|
|
|
|
const UChar *tmp = ures_getStringByIndex(res, idx, NULL, &status);
|
|
|
|
if (U_SUCCESS(status)) {
|
2010-02-24 07:54:30 +00:00
|
|
|
result = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
ures_close(res);
|
|
|
|
ures_close(rb);
|
|
|
|
|
|
|
|
return result;
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
|
|
|
|
2011-03-01 18:29:13 +00:00
|
|
|
|
2011-02-18 23:03:47 +00:00
|
|
|
// ---------------------------------------
|
|
|
|
int32_t
|
|
|
|
TimeZone::getRegion(const UnicodeString& id, char *region, int32_t capacity, UErrorCode& status)
|
|
|
|
{
|
|
|
|
int32_t resultLen = 0;
|
|
|
|
*region = 0;
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar *uregion = NULL;
|
|
|
|
// "Etc/Unknown" is not a system zone ID,
|
|
|
|
// but in the zone data
|
|
|
|
if (id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH) != 0) {
|
|
|
|
uregion = getRegion(id);
|
|
|
|
}
|
|
|
|
if (uregion == NULL) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
resultLen = u_strlen(uregion);
|
|
|
|
// A region code is represented by invariant characters
|
|
|
|
u_UCharsToChars(uregion, region, uprv_min(resultLen, capacity));
|
|
|
|
|
|
|
|
if (capacity < resultLen) {
|
|
|
|
status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
return resultLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return u_terminateChars(region, capacity, resultLen, &status);
|
|
|
|
}
|
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
// ---------------------------------------
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
TimeZone::getDisplayName(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
return getDisplayName(FALSE,LONG,Locale::getDefault(), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
TimeZone::getDisplayName(const Locale& locale, UnicodeString& result) const
|
|
|
|
{
|
|
|
|
return getDisplayName(FALSE, LONG, locale, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2000-05-18 22:08:39 +00:00
|
|
|
TimeZone::getDisplayName(UBool daylight, EDisplayType style, UnicodeString& result) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
return getDisplayName(daylight,style, Locale::getDefault(), result);
|
|
|
|
}
|
2005-11-04 21:57:24 +00:00
|
|
|
//--------------------------------------
|
2011-05-04 12:23:42 +00:00
|
|
|
int32_t
|
2005-11-04 21:57:24 +00:00
|
|
|
TimeZone::getDSTSavings()const {
|
|
|
|
if (useDaylightTime()) {
|
2006-08-19 21:27:08 +00:00
|
|
|
return 3600000;
|
2005-11-04 21:57:24 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//---------------------------------------
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
2000-05-18 22:08:39 +00:00
|
|
|
TimeZone::getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// SRL TODO: cache the SDF, just like java.
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2003-11-13 02:31:11 +00:00
|
|
|
#ifdef U_DEBUG_TZ
|
|
|
|
char buf[128];
|
|
|
|
fID.extract(0, sizeof(buf)-1, buf, sizeof(buf), "");
|
|
|
|
#endif
|
2009-08-26 16:55:52 +00:00
|
|
|
|
|
|
|
// select the proper format string
|
2011-07-07 18:46:19 +00:00
|
|
|
const UChar* patUChars;
|
2009-08-26 16:55:52 +00:00
|
|
|
switch(style){
|
|
|
|
case LONG:
|
2011-07-07 18:46:19 +00:00
|
|
|
patUChars = ZZZZ_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
case SHORT_GENERIC:
|
2011-07-07 18:46:19 +00:00
|
|
|
patUChars = V_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
case LONG_GENERIC:
|
2011-07-07 18:46:19 +00:00
|
|
|
patUChars = VVVV_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
case SHORT_GMT:
|
2011-07-07 18:46:19 +00:00
|
|
|
patUChars = Z_UC_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
case LONG_GMT:
|
2011-07-07 18:46:19 +00:00
|
|
|
patUChars = ZZZZ_UC_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
case SHORT_COMMONLY_USED:
|
2011-07-07 18:46:19 +00:00
|
|
|
//patUChars = V_UC_STR;
|
|
|
|
patUChars = Z_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
case GENERIC_LOCATION:
|
2011-07-07 18:46:19 +00:00
|
|
|
patUChars = VVVV_UC_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
default: // SHORT
|
2011-07-07 18:46:19 +00:00
|
|
|
//patUChars = Z_STR;
|
|
|
|
patUChars = V_UC_STR;
|
2009-08-26 16:55:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-07-07 18:46:19 +00:00
|
|
|
UnicodeString pat(TRUE, patUChars, -1);
|
2009-08-26 16:55:52 +00:00
|
|
|
|
|
|
|
SimpleDateFormat format(pat, locale, status);
|
2003-11-13 02:31:11 +00:00
|
|
|
U_DEBUG_TZ_MSG(("getDisplayName(%s)\n", buf));
|
1999-10-18 22:48:32 +00:00
|
|
|
if(!U_SUCCESS(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-11-13 02:31:11 +00:00
|
|
|
#ifdef U_DEBUG_TZ
|
|
|
|
char buf2[128];
|
|
|
|
result.extract(0, sizeof(buf2)-1, buf2, sizeof(buf2), "");
|
|
|
|
U_DEBUG_TZ_MSG(("getDisplayName(%s) -> %s\n", buf, buf2));
|
|
|
|
#endif
|
|
|
|
return result.remove();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2008-06-09 03:46:34 +00:00
|
|
|
UDate d = Calendar::getNow();
|
|
|
|
int32_t rawOffset;
|
|
|
|
int32_t dstOffset;
|
|
|
|
this->getOffset(d, FALSE, rawOffset, dstOffset, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result.remove();
|
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2009-08-26 16:55:52 +00:00
|
|
|
if ((daylight && dstOffset != 0) ||
|
|
|
|
(!daylight && dstOffset == 0) ||
|
|
|
|
(style == SHORT_GENERIC) ||
|
|
|
|
(style == LONG_GENERIC)
|
|
|
|
) {
|
2008-06-09 03:46:34 +00:00
|
|
|
// Current time and the request (daylight / not daylight) agree.
|
|
|
|
format.setTimeZone(*this);
|
|
|
|
return format.format(d, result);
|
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// Create a new SimpleTimeZone as a stand-in for this zone; the
|
2008-06-09 03:46:34 +00:00
|
|
|
// stand-in will have no DST, or DST during July, but the same ID and offset,
|
1999-08-16 21:50:52 +00:00
|
|
|
// and hence the same display name.
|
|
|
|
// We don't cache these because they're small and cheap to create.
|
|
|
|
UnicodeString tempID;
|
2008-06-09 03:46:34 +00:00
|
|
|
getID(tempID);
|
2005-11-04 21:57:24 +00:00
|
|
|
SimpleTimeZone *tz = NULL;
|
2008-06-09 03:46:34 +00:00
|
|
|
if(daylight && useDaylightTime()){
|
|
|
|
// The display name for daylight saving time was requested, but currently not in DST
|
|
|
|
// Set a fixed date (July 1) in this Gregorian year
|
|
|
|
GregorianCalendar cal(*this, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return result.remove();
|
|
|
|
}
|
|
|
|
cal.set(UCAL_MONTH, UCAL_JULY);
|
|
|
|
cal.set(UCAL_DATE, 1);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2008-06-09 03:46:34 +00:00
|
|
|
// Get July 1 date
|
|
|
|
d = cal.getTime(status);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2008-06-09 03:46:34 +00:00
|
|
|
// Check if it is in DST
|
|
|
|
if (cal.get(UCAL_DST_OFFSET, status) == 0) {
|
|
|
|
// We need to create a fake time zone
|
|
|
|
tz = new SimpleTimeZone(rawOffset, tempID,
|
|
|
|
UCAL_JUNE, 1, 0, 0,
|
|
|
|
UCAL_AUGUST, 1, 0, 0,
|
|
|
|
getDSTSavings(), status);
|
|
|
|
if (U_FAILURE(status) || tz == NULL) {
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return result.remove();
|
|
|
|
}
|
|
|
|
format.adoptTimeZone(tz);
|
2008-10-27 15:22:18 +00:00
|
|
|
} else {
|
|
|
|
format.setTimeZone(*this);
|
2008-06-09 03:46:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The display name for standard time was requested, but currently in DST
|
2009-03-30 20:55:05 +00:00
|
|
|
// or display name for daylight saving time was requested, but this zone no longer
|
|
|
|
// observes DST.
|
2008-06-09 03:46:34 +00:00
|
|
|
tz = new SimpleTimeZone(rawOffset, tempID);
|
|
|
|
if (U_FAILURE(status) || tz == NULL) {
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
return result.remove();
|
|
|
|
}
|
|
|
|
format.adoptTimeZone(tz);
|
2005-11-04 21:57:24 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2008-06-09 03:46:34 +00:00
|
|
|
format.format(d, result, status);
|
|
|
|
return result;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a custom time zone identifier and return a corresponding zone.
|
|
|
|
* @param id a string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
|
|
|
|
* GMT[+-]hh.
|
|
|
|
* @return a newly created SimpleTimeZone with the given offset and
|
|
|
|
* no Daylight Savings Time, or null if the id cannot be parsed.
|
|
|
|
*/
|
|
|
|
TimeZone*
|
|
|
|
TimeZone::createCustomTimeZone(const UnicodeString& id)
|
|
|
|
{
|
2008-01-23 23:47:13 +00:00
|
|
|
int32_t sign, hour, min, sec;
|
|
|
|
if (parseCustomID(id, sign, hour, min, sec)) {
|
|
|
|
UnicodeString customID;
|
|
|
|
formatCustomID(hour, min, sec, (sign < 0), customID);
|
|
|
|
int32_t offset = sign * ((hour * 60 + min) * 60 + sec) * 1000;
|
|
|
|
return new SimpleTimeZone(offset, customID);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
TimeZone::getCustomID(const UnicodeString& id, UnicodeString& normalized, UErrorCode& status) {
|
|
|
|
normalized.remove();
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return normalized;
|
|
|
|
}
|
|
|
|
int32_t sign, hour, min, sec;
|
|
|
|
if (parseCustomID(id, sign, hour, min, sec)) {
|
|
|
|
formatCustomID(hour, min, sec, (sign < 0), normalized);
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool
|
|
|
|
TimeZone::parseCustomID(const UnicodeString& id, int32_t& sign,
|
|
|
|
int32_t& hour, int32_t& min, int32_t& sec) {
|
1999-08-16 21:50:52 +00:00
|
|
|
static const int32_t kParseFailed = -99999;
|
|
|
|
|
|
|
|
NumberFormat* numberFormat = 0;
|
|
|
|
UnicodeString idUppercase = id;
|
|
|
|
idUppercase.toUpper();
|
|
|
|
|
1999-12-08 02:11:04 +00:00
|
|
|
if (id.length() > GMT_ID_LENGTH &&
|
2011-07-07 18:46:19 +00:00
|
|
|
idUppercase.startsWith(GMT_ID, GMT_ID_LENGTH))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
ParsePosition pos(GMT_ID_LENGTH);
|
2008-01-23 23:47:13 +00:00
|
|
|
sign = 1;
|
|
|
|
hour = 0;
|
|
|
|
min = 0;
|
|
|
|
sec = 0;
|
|
|
|
|
|
|
|
if (id[pos.getIndex()] == MINUS /*'-'*/) {
|
|
|
|
sign = -1;
|
|
|
|
} else if (id[pos.getIndex()] != PLUS /*'+'*/) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
pos.setIndex(pos.getIndex() + 1);
|
|
|
|
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode success = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
numberFormat = NumberFormat::createInstance(success);
|
2005-07-05 22:10:04 +00:00
|
|
|
if(U_FAILURE(success)){
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2005-07-05 22:10:04 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
numberFormat->setParseIntegerOnly(TRUE);
|
2011-05-04 12:23:42 +00:00
|
|
|
//numberFormat->setLenient(TRUE); // TODO: May need to set this, depends on latest timezone parsing
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Look for either hh:mm, hhmm, or hh
|
|
|
|
int32_t start = pos.getIndex();
|
|
|
|
Formattable n(kParseFailed);
|
|
|
|
numberFormat->parse(id, n, pos);
|
2000-06-26 22:46:15 +00:00
|
|
|
if (pos.getIndex() == start) {
|
|
|
|
delete numberFormat;
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2000-06-26 22:46:15 +00:00
|
|
|
}
|
2007-07-24 21:37:19 +00:00
|
|
|
hour = n.getLong();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
if (pos.getIndex() < id.length()) {
|
|
|
|
if (pos.getIndex() - start > 2
|
2009-04-01 17:13:40 +00:00
|
|
|
|| id[pos.getIndex()] != COLON) {
|
2007-11-28 20:49:58 +00:00
|
|
|
delete numberFormat;
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
// hh:mm
|
|
|
|
pos.setIndex(pos.getIndex() + 1);
|
|
|
|
int32_t oldPos = pos.getIndex();
|
|
|
|
n.setLong(kParseFailed);
|
|
|
|
numberFormat->parse(id, n, pos);
|
2007-11-28 20:49:58 +00:00
|
|
|
if ((pos.getIndex() - oldPos) != 2) {
|
|
|
|
// must be 2 digits
|
2000-06-26 22:46:15 +00:00
|
|
|
delete numberFormat;
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2000-06-26 22:46:15 +00:00
|
|
|
}
|
2007-07-24 21:37:19 +00:00
|
|
|
min = n.getLong();
|
2007-11-28 20:49:58 +00:00
|
|
|
if (pos.getIndex() < id.length()) {
|
2009-04-01 17:13:40 +00:00
|
|
|
if (id[pos.getIndex()] != COLON) {
|
2007-11-28 20:49:58 +00:00
|
|
|
delete numberFormat;
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
|
|
|
// [:ss]
|
|
|
|
pos.setIndex(pos.getIndex() + 1);
|
|
|
|
oldPos = pos.getIndex();
|
|
|
|
n.setLong(kParseFailed);
|
|
|
|
numberFormat->parse(id, n, pos);
|
|
|
|
if (pos.getIndex() != id.length()
|
|
|
|
|| (pos.getIndex() - oldPos) != 2) {
|
|
|
|
delete numberFormat;
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
|
|
|
sec = n.getLong();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Supported formats are below -
|
|
|
|
//
|
|
|
|
// HHmmss
|
|
|
|
// Hmmss
|
|
|
|
// HHmm
|
|
|
|
// Hmm
|
|
|
|
// HH
|
|
|
|
// H
|
|
|
|
|
|
|
|
int32_t length = pos.getIndex() - start;
|
|
|
|
if (length <= 0 || 6 < length) {
|
|
|
|
// invalid length
|
|
|
|
delete numberFormat;
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2007-11-28 20:49:58 +00:00
|
|
|
}
|
|
|
|
switch (length) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
// already set to hour
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
min = hour % 100;
|
|
|
|
hour /= 100;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
sec = hour % 100;
|
|
|
|
min = (hour/100) % 100;
|
|
|
|
hour /= 10000;
|
|
|
|
break;
|
2007-07-24 21:37:19 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete numberFormat;
|
2007-07-24 21:37:19 +00:00
|
|
|
|
2007-11-28 20:49:58 +00:00
|
|
|
if (hour > kMAX_CUSTOM_HOUR || min > kMAX_CUSTOM_MIN || sec > kMAX_CUSTOM_SEC) {
|
2008-01-23 23:47:13 +00:00
|
|
|
return FALSE;
|
2007-07-24 21:37:19 +00:00
|
|
|
}
|
2008-01-23 23:47:13 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-07-24 21:37:19 +00:00
|
|
|
|
2008-01-23 23:47:13 +00:00
|
|
|
UnicodeString&
|
|
|
|
TimeZone::formatCustomID(int32_t hour, int32_t min, int32_t sec,
|
|
|
|
UBool negative, UnicodeString& id) {
|
|
|
|
// Create time zone ID - GMT[+|-]hhmm[ss]
|
2011-07-07 18:46:19 +00:00
|
|
|
id.setTo(GMT_ID, GMT_ID_LENGTH);
|
2008-01-23 23:47:13 +00:00
|
|
|
if (hour | min | sec) {
|
|
|
|
if (negative) {
|
|
|
|
id += (UChar)MINUS;
|
|
|
|
} else {
|
|
|
|
id += (UChar)PLUS;
|
|
|
|
}
|
2007-07-24 21:37:19 +00:00
|
|
|
|
2008-01-23 23:47:13 +00:00
|
|
|
if (hour < 10) {
|
|
|
|
id += (UChar)ZERO_DIGIT;
|
|
|
|
} else {
|
|
|
|
id += (UChar)(ZERO_DIGIT + hour/10);
|
|
|
|
}
|
|
|
|
id += (UChar)(ZERO_DIGIT + hour%10);
|
2009-04-01 17:13:40 +00:00
|
|
|
id += (UChar)COLON;
|
2008-01-23 23:47:13 +00:00
|
|
|
if (min < 10) {
|
|
|
|
id += (UChar)ZERO_DIGIT;
|
|
|
|
} else {
|
|
|
|
id += (UChar)(ZERO_DIGIT + min/10);
|
2007-07-24 21:37:19 +00:00
|
|
|
}
|
2008-01-23 23:47:13 +00:00
|
|
|
id += (UChar)(ZERO_DIGIT + min%10);
|
2007-07-24 21:37:19 +00:00
|
|
|
|
2008-01-23 23:47:13 +00:00
|
|
|
if (sec) {
|
2009-04-01 17:13:40 +00:00
|
|
|
id += (UChar)COLON;
|
2008-01-23 23:47:13 +00:00
|
|
|
if (sec < 10) {
|
|
|
|
id += (UChar)ZERO_DIGIT;
|
|
|
|
} else {
|
|
|
|
id += (UChar)(ZERO_DIGIT + sec/10);
|
|
|
|
}
|
|
|
|
id += (UChar)(ZERO_DIGIT + sec%10);
|
2007-07-24 21:37:19 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2008-01-23 23:47:13 +00:00
|
|
|
return id;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::hasSameRules(const TimeZone& other) const
|
|
|
|
{
|
2011-05-04 12:23:42 +00:00
|
|
|
return (getRawOffset() == other.getRawOffset() &&
|
1999-08-16 21:50:52 +00:00
|
|
|
useDaylightTime() == other.useDaylightTime());
|
|
|
|
}
|
|
|
|
|
2007-07-19 22:29:51 +00:00
|
|
|
const char*
|
|
|
|
TimeZone::getTZDataVersion(UErrorCode& status)
|
|
|
|
{
|
|
|
|
/* This is here to prevent race conditions. */
|
|
|
|
UBool needsInit;
|
2008-08-06 21:01:19 +00:00
|
|
|
UMTX_CHECK(&LOCK, !TZDataVersionInitialized, needsInit);
|
2007-07-19 22:29:51 +00:00
|
|
|
if (needsInit) {
|
2008-08-06 21:01:19 +00:00
|
|
|
int32_t len = 0;
|
2010-02-24 07:54:30 +00:00
|
|
|
UResourceBundle *bundle = ures_openDirect(NULL, kZONEINFO, &status);
|
|
|
|
const UChar *tzver = ures_getStringByKey(bundle, kTZVERSION,
|
2007-08-08 18:38:20 +00:00
|
|
|
&len, &status);
|
|
|
|
|
|
|
|
if (U_SUCCESS(status)) {
|
2008-08-06 21:14:53 +00:00
|
|
|
if (len >= (int32_t)sizeof(TZDATA_VERSION)) {
|
2008-08-06 21:01:19 +00:00
|
|
|
// Ensure that there is always space for a trailing nul in TZDATA_VERSION
|
|
|
|
len = sizeof(TZDATA_VERSION) - 1;
|
|
|
|
}
|
|
|
|
umtx_lock(&LOCK);
|
|
|
|
if (!TZDataVersionInitialized) {
|
|
|
|
u_UCharsToChars(tzver, TZDATA_VERSION, len);
|
|
|
|
TZDataVersionInitialized = TRUE;
|
|
|
|
}
|
|
|
|
umtx_unlock(&LOCK);
|
2007-08-08 18:38:20 +00:00
|
|
|
ucln_i18n_registerCleanup(UCLN_I18N_TIMEZONE, timeZone_cleanup);
|
2007-07-19 22:29:51 +00:00
|
|
|
}
|
2007-08-08 18:38:20 +00:00
|
|
|
|
|
|
|
ures_close(bundle);
|
2007-07-19 22:29:51 +00:00
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (const char*)TZDATA_VERSION;
|
|
|
|
}
|
|
|
|
|
2008-01-23 23:47:13 +00:00
|
|
|
UnicodeString&
|
|
|
|
TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UErrorCode& status)
|
2008-02-05 19:14:43 +00:00
|
|
|
{
|
|
|
|
UBool isSystemID = FALSE;
|
|
|
|
return getCanonicalID(id, canonicalID, isSystemID, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
TimeZone::getCanonicalID(const UnicodeString& id, UnicodeString& canonicalID, UBool& isSystemID,
|
|
|
|
UErrorCode& status)
|
2008-01-23 23:47:13 +00:00
|
|
|
{
|
|
|
|
canonicalID.remove();
|
2008-02-05 19:14:43 +00:00
|
|
|
isSystemID = FALSE;
|
2008-01-23 23:47:13 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return canonicalID;
|
|
|
|
}
|
2011-03-01 18:29:13 +00:00
|
|
|
if (id.compare(UNKNOWN_ZONE_ID, UNKNOWN_ZONE_ID_LENGTH) == 0) {
|
|
|
|
// special case - Etc/Unknown is a canonical ID, but not system ID
|
|
|
|
canonicalID.fastCopyFrom(id);
|
|
|
|
isSystemID = FALSE;
|
2008-02-05 19:14:43 +00:00
|
|
|
} else {
|
2011-03-01 18:29:13 +00:00
|
|
|
ZoneMeta::getCanonicalCLDRID(id, canonicalID, status);
|
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
isSystemID = TRUE;
|
|
|
|
} else {
|
|
|
|
// Not a system ID
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
getCustomID(id, canonicalID, status);
|
|
|
|
}
|
2008-01-23 23:47:13 +00:00
|
|
|
}
|
|
|
|
return canonicalID;
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
2003-08-19 04:38:54 +00:00
|
|
|
//eof
|