1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 1997-2003, 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
|
|
|
*********************************************************************************/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#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"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
#define ZONEINFO "zoneinfo"
|
2001-06-28 22:57:15 +00:00
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
// Static data and constants
|
|
|
|
|
|
|
|
static const UChar GMT_ID[] = {0x47, 0x4D, 0x54, 0x00}; /* "GMT" */
|
|
|
|
static const int32_t GMT_ID_LENGTH = 3;
|
|
|
|
static const UChar CUSTOM_ID[] =
|
2001-08-31 02:21:34 +00:00
|
|
|
{
|
2003-05-08 21:08:06 +00:00
|
|
|
0x43, 0x75, 0x73, 0x74, 0x6F, 0x6D, 0x00 /* "Custom" */
|
|
|
|
};
|
|
|
|
|
|
|
|
static UMTX LOCK;
|
|
|
|
static TimeZone* DEFAULT_ZONE = NULL;
|
|
|
|
static TimeZone* _GMT = NULL; // cf. TimeZone::GMT
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
// #ifdef ICU_TIMEZONE_USE_DEPRECATES
|
|
|
|
static UnicodeString* OLSON_IDS = 0;
|
|
|
|
// #endif
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
UBool timeZone_cleanup()
|
|
|
|
{
|
2003-08-15 18:21:31 +00:00
|
|
|
// #ifdef ICU_TIMEZONE_USE_DEPRECATES
|
|
|
|
delete []OLSON_IDS;
|
|
|
|
OLSON_IDS = 0;
|
|
|
|
// #endif
|
2003-05-08 21:08:06 +00:00
|
|
|
|
|
|
|
delete DEFAULT_ZONE;
|
|
|
|
DEFAULT_ZONE = NULL;
|
|
|
|
|
|
|
|
delete _GMT;
|
|
|
|
_GMT = NULL;
|
|
|
|
|
2001-08-31 02:21:34 +00:00
|
|
|
if (LOCK) {
|
|
|
|
umtx_destroy(&LOCK);
|
|
|
|
LOCK = NULL;
|
|
|
|
}
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2001-08-31 02:21:34 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-16 00:10:56 +00:00
|
|
|
/**
|
|
|
|
* The Olson data is stored the "zoneinfo" resource bundle.
|
|
|
|
* Sub-resources are organized into three ranges of data: Zones, final
|
|
|
|
* rules, and country tables. There is also a meta-data resource
|
2003-08-19 04:38:54 +00:00
|
|
|
* which has 3 integers: The number of zones, rules, and countries,
|
|
|
|
* respectively. The country count includes the non-country '%' and
|
|
|
|
* the rule count includes the non-rule '_' (the metadata).
|
2003-08-16 00:10:56 +00:00
|
|
|
*/
|
|
|
|
static int32_t OLSON_ZONE_START = -1; // starting index of zones
|
|
|
|
static int32_t OLSON_ZONE_COUNT = 0; // count of zones
|
2003-08-15 18:21:31 +00:00
|
|
|
|
2003-08-19 04:38:54 +00:00
|
|
|
/* We could compute all of the following, but we don't need them
|
|
|
|
static int32_t OLSON_RULE_START = 0; // starting index of rules
|
|
|
|
static int32_t OLSON_RULE_COUNT = 0; // count of zones
|
|
|
|
static int32_t OLSON_COUNTRY_START = 0; // starting index of countries
|
|
|
|
static int32_t OLSON_COUNTRY_COUNT = 0; // count of zones
|
|
|
|
*/
|
|
|
|
|
2003-08-16 00:10:56 +00:00
|
|
|
/**
|
|
|
|
* Given a pointer to an open "zoneinfo" resource, load up the Olson
|
|
|
|
* meta-data. Return TRUE if successful.
|
|
|
|
*/
|
2003-08-15 18:21:31 +00:00
|
|
|
static UBool getOlsonMeta(const UResourceBundle* top) {
|
|
|
|
if (OLSON_ZONE_START < 0) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
|
|
|
ures_getByKey(top, "_", &res, &ec);
|
|
|
|
int32_t len;
|
|
|
|
const int32_t* v = ures_getIntVector(&res, &len, &ec);
|
2003-08-19 04:38:54 +00:00
|
|
|
if (U_SUCCESS(ec) && len == 3) {
|
|
|
|
OLSON_ZONE_COUNT = v[0];
|
|
|
|
// Note: Remove the following `int32_t' declarations if you
|
|
|
|
// uncomment the declarations above!
|
|
|
|
int32_t OLSON_RULE_COUNT = v[1];
|
|
|
|
int32_t OLSON_COUNTRY_COUNT = v[2];
|
|
|
|
|
|
|
|
// Try to figure out how strcmp in genrb is going to sort
|
|
|
|
// things. This has to be done here, dynamically, rather
|
|
|
|
// than at build time.
|
|
|
|
char zoneCh='A', ruleCh='_', countryCh='%'; // [sic]
|
|
|
|
OLSON_ZONE_START = ((ruleCh < zoneCh) ? OLSON_RULE_COUNT : 0) +
|
|
|
|
((countryCh < zoneCh) ? OLSON_COUNTRY_COUNT : 0);
|
|
|
|
|
|
|
|
/* We could compute the following, but we don't need them -- yet
|
|
|
|
OLSON_RULE_START = ((zoneCh < ruleCh) ? OLSON_ZONE_COUNT : 0) +
|
|
|
|
((countryCh < ruleCh) ? OLSON_COUNTRY_COUNT : 0);
|
|
|
|
OLSON_COUNTRY_START = ((ruleCh < countryCh) ? OLSON_RULE_COUNT : 0) +
|
|
|
|
((zoneCh < countryCh) ? OLSON_ZONE_COUNT : 0);
|
|
|
|
*/
|
2003-08-15 18:21:31 +00:00
|
|
|
}
|
|
|
|
ures_close(&res);
|
2002-12-09 18:10:43 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
return (OLSON_ZONE_START >= 0);
|
|
|
|
}
|
2002-12-09 18:10:43 +00:00
|
|
|
|
2003-08-16 00:10:56 +00:00
|
|
|
/**
|
|
|
|
* Load up the Olson meta-data. Return TRUE if successful.
|
|
|
|
*/
|
2003-08-15 18:21:31 +00:00
|
|
|
static UBool getOlsonMeta() {
|
|
|
|
if (OLSON_ZONE_START < 0) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *top = ures_openDirect(0, ZONEINFO, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
getOlsonMeta(top);
|
|
|
|
}
|
|
|
|
ures_close(top);
|
|
|
|
}
|
|
|
|
return (OLSON_ZONE_START >= 0);
|
|
|
|
}
|
2002-12-09 18:10:43 +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,
|
|
|
|
UErrorCode& ec) {
|
|
|
|
char buf[128];
|
|
|
|
id.extract(0, sizeof(buf)-1, buf, sizeof(buf), "");
|
|
|
|
UResourceBundle *top = ures_openDirect(0, ZONEINFO, &ec);
|
|
|
|
ures_getByKey(top, buf, &res, &ec);
|
|
|
|
// Dereference if this is an alias. Docs say result should be 1
|
|
|
|
// but it is 0 in 2.8 (?).
|
|
|
|
if (ures_getSize(&res) <= 1 && getOlsonMeta(top)) {
|
|
|
|
int32_t deref = ures_getInt(&res, &ec) + OLSON_ZONE_START;
|
|
|
|
ures_close(&res);
|
|
|
|
ures_getByIndex(top, deref, &res, &ec);
|
|
|
|
}
|
|
|
|
return top;
|
|
|
|
}
|
|
|
|
|
2003-08-16 00:10:56 +00:00
|
|
|
// TODO: #ifdef out this code after 8-Nov-2003
|
|
|
|
// #ifdef ICU_TIMEZONE_USE_DEPRECATES
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load all the ids from the "zoneinfo" resource bundle into a static
|
|
|
|
* array that we hang onto. This is _only_ used to implement the
|
|
|
|
* deprecated createAvailableIDs() API.
|
|
|
|
*/
|
2003-08-15 18:21:31 +00:00
|
|
|
static UBool loadOlsonIDs() {
|
|
|
|
if (OLSON_IDS != 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UnicodeString* ids = 0;
|
|
|
|
int32_t count = 0;
|
|
|
|
UResourceBundle *top = ures_openDirect(0, ZONEINFO, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
getOlsonMeta(top);
|
|
|
|
int32_t start = OLSON_ZONE_START;
|
|
|
|
count = OLSON_ZONE_COUNT;
|
|
|
|
ids = new UnicodeString[(count > 0) ? count : 1];
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
|
|
|
for (int32_t i=0; i<count; ++i) {
|
|
|
|
ures_getByIndex(top, start+i, &res, &ec);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
ures_close(&res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const char* key = ures_getKey(&res);
|
|
|
|
U_ASSERT(*key != '_' && *key != '%');
|
|
|
|
int32_t len = uprv_strlen(key);
|
|
|
|
ids[i] = UnicodeString(key, len+1, "");
|
|
|
|
ids[i].truncate(len); // add a NUL but don't count it so that
|
|
|
|
// getBuffer() gets a terminated string
|
|
|
|
ures_close(&res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ures_close(top);
|
2002-12-09 18:10:43 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
delete[] ids;
|
2003-05-08 21:08:06 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-12-09 18:10:43 +00:00
|
|
|
|
|
|
|
// Keep mutexed operations as short as possible by doing all
|
|
|
|
// computations first, then doing pointer copies within the mutex.
|
|
|
|
umtx_lock(&LOCK);
|
2003-08-15 18:21:31 +00:00
|
|
|
if (OLSON_IDS == 0) {
|
|
|
|
OLSON_IDS = ids;
|
|
|
|
ids = 0;
|
2003-10-06 22:37:41 +00:00
|
|
|
ucln_i18n_registerCleanup();
|
2002-12-09 18:10:43 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(&LOCK);
|
1999-12-05 06:24:22 +00:00
|
|
|
|
2002-12-09 18:10:43 +00:00
|
|
|
// If another thread initialized the statics first, then delete
|
|
|
|
// our unused data.
|
2003-08-15 18:21:31 +00:00
|
|
|
delete[] ids;
|
2003-05-08 21:08:06 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-08-16 00:10:56 +00:00
|
|
|
// #endif //ICU_TIMEZONE_USE_DEPRECATES
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
// -------------------------------------
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
const TimeZone*
|
|
|
|
TimeZone::getGMT(void)
|
|
|
|
{
|
2003-06-06 22:50:26 +00:00
|
|
|
umtx_init(&LOCK); /* This is here to prevent race conditions. */
|
2003-05-08 21:08:06 +00:00
|
|
|
Mutex lock(&LOCK);
|
|
|
|
// Initialize _GMT independently of other static data; it should
|
|
|
|
// be valid even if we can't load the time zone UDataMemory.
|
|
|
|
if (_GMT == 0) {
|
|
|
|
_GMT = new SimpleTimeZone(0, UnicodeString(GMT_ID, GMT_ID_LENGTH));
|
2003-10-06 22:37:41 +00:00
|
|
|
ucln_i18n_registerCleanup();
|
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
|
|
|
|
// *****************************************************************************
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return getDynamicClassID() == that.getDynamicClassID() &&
|
|
|
|
fID == that.fID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
1999-12-05 06:24:22 +00:00
|
|
|
TimeZone*
|
|
|
|
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) {
|
|
|
|
result = createCustomTimeZone(ID);
|
|
|
|
}
|
|
|
|
if (result == 0) {
|
2001-08-16 00:31:06 +00:00
|
|
|
result = getGMT()->clone();
|
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) {
|
|
|
|
TimeZone* z = 0;
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
2003-08-19 00:24:49 +00:00
|
|
|
UResourceBundle *top = openOlsonResource(id, res, ec);
|
2003-08-15 18:21:31 +00:00
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
z = new OlsonTimeZone(top, &res, ec);
|
2003-09-02 17:46:43 +00:00
|
|
|
if (z) z->setID(id);
|
2003-08-15 18:21:31 +00:00
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
ures_close(top);
|
|
|
|
if (U_FAILURE(ec)) {
|
|
|
|
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()
|
1999-12-05 06:24:22 +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].
|
|
|
|
{
|
|
|
|
// NOTE: Global mutex here; TimeZone mutex above
|
|
|
|
// mutexed to avoid threading issues in the platform fcns.
|
|
|
|
// Some of the locale/timezone OS functions may not be thread safe,
|
|
|
|
// so the intent is that any setting from anywhere within ICU
|
|
|
|
// happens with the ICU global mutex held.
|
|
|
|
Mutex lock;
|
|
|
|
uprv_tzset(); // Initialize tz... system data
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Invert sign because UNIX semantics are backwards
|
|
|
|
rawOffset = uprv_timezone() * -U_MILLIS_PER_SECOND;
|
|
|
|
}
|
1999-12-05 06:24:22 +00:00
|
|
|
|
2003-05-08 21:08:06 +00:00
|
|
|
TimeZone* default_zone = NULL;
|
2002-12-09 18:10:43 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
default_zone = createSystemTimeZone(hostID);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// NOTE: As of ICU 2.8, we no longer have an offsets table, since
|
|
|
|
// historical zones can change offset over time. If we add
|
|
|
|
// build-time heuristics to infer the "most frequent" raw offset
|
|
|
|
// of a zone, we can build tables and institute defaults, as done
|
|
|
|
// in ICU <= 2.6.
|
|
|
|
|
|
|
|
// If we couldn't get the time zone ID from the host, use
|
|
|
|
// the default host timezone offset. Further refinements
|
|
|
|
// to this include querying the host to determine if DST
|
|
|
|
// is in use or not and possibly using the host locale to
|
|
|
|
// select from multiple zones at a the same offset. We
|
|
|
|
// don't do any of this now, but we could easily add this.
|
|
|
|
if (default_zone == NULL) {
|
|
|
|
// Use the designated default in the time zone list that has the
|
|
|
|
// appropriate GMT offset, if there is one.
|
|
|
|
|
|
|
|
const OffsetIndex* index = INDEX_BY_OFFSET;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (index->gmtOffset > rawOffset) {
|
|
|
|
// Went past our desired offset; no match found
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (index->gmtOffset == rawOffset) {
|
|
|
|
// Found our desired offset
|
|
|
|
default_zone = createSystemTimeZone(ZONE_IDS[index->defaultZone]);
|
|
|
|
break;
|
2002-12-09 18:10:43 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
// Compute the position of the next entry. If the delta value
|
|
|
|
// in this entry is zero, then there is no next entry.
|
|
|
|
uint16_t delta = index->nextEntryDelta;
|
|
|
|
if (delta == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
index = (const OffsetIndex*)((int8_t*)index + delta);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Construct a fixed standard zone with the host's ID
|
|
|
|
// and raw offset.
|
|
|
|
if (default_zone == NULL) {
|
|
|
|
default_zone =
|
|
|
|
new SimpleTimeZone(rawOffset, UnicodeString(hostID, (char*)0));
|
|
|
|
}
|
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) {
|
|
|
|
default_zone = getGMT()->clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If DEFAULT_ZONE is still NULL, set it up.
|
|
|
|
umtx_lock(&LOCK);
|
|
|
|
if (DEFAULT_ZONE == NULL) {
|
|
|
|
DEFAULT_ZONE = default_zone;
|
|
|
|
default_zone = NULL;
|
2003-10-06 22:37:41 +00:00
|
|
|
ucln_i18n_registerCleanup();
|
2002-12-09 18:10:43 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(&LOCK);
|
|
|
|
|
|
|
|
delete default_zone;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
TimeZone*
|
|
|
|
TimeZone::createDefault()
|
|
|
|
{
|
2003-06-06 22:50:26 +00:00
|
|
|
umtx_init(&LOCK); /* This is here to prevent race conditions. */
|
2003-05-08 21:08:06 +00:00
|
|
|
umtx_lock(&LOCK);
|
|
|
|
UBool f = (DEFAULT_ZONE != 0);
|
|
|
|
umtx_unlock(&LOCK);
|
|
|
|
if (!f) {
|
|
|
|
initDefault();
|
|
|
|
}
|
|
|
|
|
1999-12-05 06:24:22 +00:00
|
|
|
Mutex lock(&LOCK); // In case adoptDefault is called
|
2001-08-31 02:21:34 +00:00
|
|
|
return DEFAULT_ZONE->clone();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
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
|
|
|
|
2003-06-06 22:50:26 +00:00
|
|
|
umtx_init(&LOCK); /* This is here to prevent race conditions. */
|
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;
|
2003-10-06 22:37:41 +00:00
|
|
|
ucln_i18n_registerCleanup();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Convert to local wall millis if necessary
|
|
|
|
if (!local) {
|
|
|
|
date += rawOffset; // now in local standard millis
|
|
|
|
}
|
|
|
|
|
|
|
|
// When local==FALSE, we might have to recompute. This loop is
|
|
|
|
// executed once, unless a recomputation is required; then it is
|
|
|
|
// executed twice.
|
|
|
|
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);
|
|
|
|
|
2003-09-02 17:46:43 +00:00
|
|
|
Grego::dayToFields(day, year, month, dom, dow);
|
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;
|
|
|
|
|
|
|
|
// Recompute if local==FALSE, dstOffset!=0, and addition of
|
|
|
|
// the dstOffset puts us in a different day.
|
|
|
|
if (pass!=0 || local || dstOffset==0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
date += dstOffset;
|
|
|
|
if (uprv_floor(date / U_MILLIS_PER_DAY) == day) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
|
// iff the zone data could not be loaded.
|
2002-11-12 23:52:27 +00:00
|
|
|
int32_t* map;
|
|
|
|
int32_t len;
|
|
|
|
int32_t pos;
|
2003-05-08 21:08:06 +00:00
|
|
|
void* _bufp;
|
|
|
|
int32_t _buflen;
|
2003-08-15 18:21:31 +00:00
|
|
|
UnicodeString id;
|
2002-11-12 23:52:27 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
TZEnumeration() {
|
|
|
|
map = NULL;
|
2003-05-08 21:08:06 +00:00
|
|
|
_bufp = NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
len = pos = _buflen = 0;
|
2003-08-15 18:21:31 +00:00
|
|
|
if (getOlsonMeta()) {
|
|
|
|
len = OLSON_ZONE_COUNT;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TZEnumeration(int32_t rawOffset) {
|
|
|
|
map = NULL;
|
2003-08-15 18:21:31 +00:00
|
|
|
_bufp = NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
len = pos = _buflen = 0;
|
2003-08-15 18:21:31 +00:00
|
|
|
if (!getOlsonMeta()) {
|
|
|
|
return;
|
|
|
|
}
|
2003-05-08 21:08:06 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
// Allocate more space than we'll need. The end of the array will
|
|
|
|
// be blank.
|
|
|
|
map = (int32_t*)uprv_malloc(OLSON_ZONE_COUNT * sizeof(int32_t));
|
|
|
|
if (map == 0) {
|
2003-05-08 21:08:06 +00:00
|
|
|
return;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
uprv_memset(map, 0, sizeof(int32_t) * OLSON_ZONE_COUNT);
|
|
|
|
|
|
|
|
UnicodeString s;
|
|
|
|
for (int32_t i=0; i<OLSON_ZONE_COUNT; ++i) {
|
|
|
|
if (getID(i)) {
|
|
|
|
// This is VERY inefficient.
|
|
|
|
TimeZone* z = TimeZone::createTimeZone(id);
|
|
|
|
// Make sure we get back the ID we wanted (if the ID is
|
|
|
|
// invalid we get back GMT).
|
|
|
|
if (z != 0 && z->getID(s) == id &&
|
|
|
|
z->getRawOffset() == rawOffset) {
|
|
|
|
map[len++] = i;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
delete z;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TZEnumeration(const char* country) {
|
|
|
|
map = NULL;
|
2003-08-15 18:21:31 +00:00
|
|
|
_bufp = NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
len = pos = _buflen = 0;
|
2003-08-15 18:21:31 +00:00
|
|
|
if (!getOlsonMeta()) {
|
2003-05-08 21:08:06 +00:00
|
|
|
return;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
char key[4] = {'%', 0, 0, 0}; // e.g., "%US", or "%" for no country
|
|
|
|
if (country) uprv_strncat(key, country, 2);
|
|
|
|
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *top = ures_openDirect(0, ZONEINFO, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
|
|
|
ures_getByKey(top, key, &res, &ec);
|
|
|
|
// The list of zones is a list of integers, from 0..n-1,
|
|
|
|
// where n is the total number of system zones.
|
|
|
|
const int32_t* v = ures_getIntVector(&res, &len, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
U_ASSERT(len > 0);
|
|
|
|
map = (int32_t*)uprv_malloc(sizeof(int32_t) * len);
|
|
|
|
if (map != 0) {
|
2002-11-12 23:52:27 +00:00
|
|
|
for (uint16_t i=0; i<len; ++i) {
|
2003-08-15 18:21:31 +00:00
|
|
|
U_ASSERT(v[i] >= 0 && v[i] < OLSON_ZONE_COUNT);
|
|
|
|
map[i] = v[i];
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
ures_close(&res);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
ures_close(top);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TZEnumeration() {
|
|
|
|
uprv_free(map);
|
2003-08-15 18:21:31 +00:00
|
|
|
uprv_free(_bufp);
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t count(UErrorCode& status) const {
|
|
|
|
return U_FAILURE(status) ? 0 : len;
|
|
|
|
}
|
|
|
|
|
2002-11-22 18:16:02 +00:00
|
|
|
const char* next(int32_t* resultLength, UErrorCode& status) {
|
2002-11-26 00:41:13 +00:00
|
|
|
// TODO: Later a subclass of StringEnumeration will be available
|
|
|
|
// that implements next() and unext() in terms of snext().
|
|
|
|
// Inherit from that class when available and remove this method
|
|
|
|
// (and its declaration).
|
2002-11-12 23:52:27 +00:00
|
|
|
const UnicodeString* us = snext(status);
|
2002-11-26 00:41:13 +00:00
|
|
|
int32_t newlen;
|
|
|
|
if (us != NULL && ensureCapacity((newlen=us->length()) + 1)) {
|
|
|
|
us->extract(0, INT32_MAX, (char*) _bufp, "");
|
|
|
|
if (resultLength) {
|
|
|
|
resultLength[0] = newlen;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2002-11-26 00:41:13 +00:00
|
|
|
return (const char*)_bufp;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-11-22 18:16:02 +00:00
|
|
|
const UChar* unext(int32_t* resultLength, UErrorCode& status) {
|
2002-11-12 23:52:27 +00:00
|
|
|
const UnicodeString* us = snext(status);
|
2002-11-22 18:16:02 +00:00
|
|
|
if (us != NULL) {
|
|
|
|
if (resultLength) {
|
|
|
|
resultLength[0] = us->length();
|
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
U_ASSERT(us == &id); // A little ugly...
|
|
|
|
return id.getTerminatedBuffer(); // ...but works
|
2002-11-22 18:16:02 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const UnicodeString* snext(UErrorCode& status) {
|
|
|
|
if (U_SUCCESS(status) && pos < len) {
|
2003-08-15 18:21:31 +00:00
|
|
|
getID((map == 0) ? pos : map[pos]);
|
|
|
|
++pos;
|
|
|
|
return &id;
|
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
|
|
|
}
|
|
|
|
|
2003-05-29 22:22:08 +00:00
|
|
|
void reset(UErrorCode& /*status*/) {
|
2002-11-12 23:52:27 +00:00
|
|
|
pos = 0;
|
|
|
|
}
|
|
|
|
|
2002-11-26 00:46:48 +00:00
|
|
|
private:
|
2003-08-15 18:21:31 +00:00
|
|
|
|
|
|
|
UBool getID(int32_t i) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *top = ures_openDirect(0, ZONEINFO, &ec);
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
|
|
|
ures_getByIndex(top, OLSON_ZONE_START + i, &res, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
const char* key = ures_getKey(&res);
|
|
|
|
id = UnicodeString(key, "");
|
|
|
|
} else {
|
|
|
|
id.truncate(0);
|
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
ures_close(top);
|
|
|
|
return U_SUCCESS(ec);
|
|
|
|
}
|
|
|
|
|
2003-05-29 22:22:08 +00:00
|
|
|
static const char fgClassID;
|
2002-11-26 00:46:48 +00:00
|
|
|
|
2003-05-29 22:22:08 +00:00
|
|
|
public:
|
|
|
|
static inline UClassID getStaticClassID(void) { return (UClassID)&fgClassID; }
|
|
|
|
virtual UClassID getDynamicClassID(void) const { return getStaticClassID(); }
|
2002-11-12 23:52:27 +00:00
|
|
|
private:
|
2002-11-26 00:41:13 +00:00
|
|
|
/**
|
|
|
|
* Guarantee that _bufp is allocated to include _buflen characters
|
|
|
|
* where _buflen >= minlen. Return TRUE if successful, FALSE
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
UBool ensureCapacity(int32_t minlen) {
|
|
|
|
if (_bufp != NULL && _buflen >= minlen) {
|
|
|
|
return TRUE;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
2002-11-26 00:41:13 +00:00
|
|
|
_buflen = minlen + 8; // add 8 to prevent thrashing
|
|
|
|
_bufp = (_bufp == NULL) ? uprv_malloc(_buflen)
|
|
|
|
: uprv_realloc(_bufp, _buflen);
|
|
|
|
return _bufp != NULL;
|
2002-11-12 23:52:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-11-26 00:46:48 +00:00
|
|
|
const char TZEnumeration::fgClassID = '\0';
|
|
|
|
|
2002-11-12 23:52:27 +00:00
|
|
|
StringEnumeration*
|
|
|
|
TimeZone::createEnumeration() {
|
|
|
|
return new TZEnumeration();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration*
|
|
|
|
TimeZone::createEnumeration(int32_t rawOffset) {
|
|
|
|
return new TZEnumeration(rawOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringEnumeration*
|
|
|
|
TimeZone::createEnumeration(const char* country) {
|
|
|
|
return new TZEnumeration(country);
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
|
|
|
// TODO: #ifdef out this code after 8-Nov-2003
|
|
|
|
// #ifdef ICU_TIMEZONE_USE_DEPRECATES
|
|
|
|
|
2002-03-20 00:15:44 +00:00
|
|
|
const UnicodeString**
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::createAvailableIDs(int32_t rawOffset, int32_t& numIDs)
|
|
|
|
{
|
|
|
|
// We are creating a new array to existing UnicodeString pointers.
|
|
|
|
// The caller will delete the array when done, but not the pointers
|
|
|
|
// in the array.
|
2003-08-15 18:21:31 +00:00
|
|
|
|
|
|
|
numIDs = 0;
|
|
|
|
if (!loadOlsonIDs()) {
|
1999-12-05 06:24:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
// Allocate more space than we'll need. The end of the array will
|
|
|
|
// be blank.
|
|
|
|
const UnicodeString** ids =
|
|
|
|
(const UnicodeString** )uprv_malloc(OLSON_ZONE_COUNT * sizeof(UnicodeString *));
|
|
|
|
if (ids == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
1999-12-05 06:24:22 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
uprv_memset(ids, 0, sizeof(UnicodeString*) * OLSON_ZONE_COUNT);
|
|
|
|
|
|
|
|
UnicodeString s;
|
|
|
|
for (int32_t i=0; i<OLSON_ZONE_COUNT; ++i) {
|
2003-08-16 00:10:56 +00:00
|
|
|
// This is VERY inefficient.
|
2003-08-15 18:21:31 +00:00
|
|
|
TimeZone* z = TimeZone::createTimeZone(OLSON_IDS[i]);
|
|
|
|
// Make sure we get back the ID we wanted (if the ID is
|
|
|
|
// invalid we get back GMT).
|
|
|
|
if (z != 0 && z->getID(s) == OLSON_IDS[i] &&
|
|
|
|
z->getRawOffset() == rawOffset) {
|
|
|
|
ids[numIDs++] = &OLSON_IDS[i]; // [sic]
|
1999-12-05 06:24:22 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
delete z;
|
1999-12-05 06:24:22 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
return ids;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2002-03-20 00:15:44 +00:00
|
|
|
const UnicodeString**
|
2001-02-02 19:26:56 +00:00
|
|
|
TimeZone::createAvailableIDs(const char* country, int32_t& numIDs) {
|
|
|
|
|
|
|
|
// We are creating a new array to existing UnicodeString pointers.
|
|
|
|
// The caller will delete the array when done, but not the pointers
|
|
|
|
// in the array.
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
numIDs = 0;
|
|
|
|
if (!loadOlsonIDs()) {
|
|
|
|
return 0;
|
2001-02-02 19:26:56 +00:00
|
|
|
}
|
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
char key[4] = {'%', 0, 0, 0}; // e.g., "%US", or "%" for non-country zones
|
|
|
|
if (country) uprv_strncat(key, country, 2);
|
|
|
|
|
|
|
|
const UnicodeString** ids = 0;
|
|
|
|
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UResourceBundle *top = ures_openDirect(0, ZONEINFO, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
getOlsonMeta(top);
|
|
|
|
UResourceBundle res;
|
|
|
|
ures_initStackObject(&res);
|
|
|
|
ures_getByKey(top, key, &res, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
/* The list of zones is a list of integers, from 0..n-1,
|
|
|
|
* where n is the total number of system zones. The
|
|
|
|
* numbering corresponds exactly to the ordering of
|
|
|
|
* OLSON_IDS.
|
|
|
|
*/
|
|
|
|
const int32_t* v = ures_getIntVector(&res, &numIDs, &ec);
|
|
|
|
ids = (const UnicodeString**)
|
|
|
|
uprv_malloc(numIDs * sizeof(UnicodeString*));
|
|
|
|
if (ids == 0) {
|
|
|
|
numIDs = 0;
|
|
|
|
} else {
|
|
|
|
for (int32_t i=0; i<numIDs; ++i) {
|
|
|
|
ids[i] = &OLSON_IDS[v[i]]; // [sic]
|
|
|
|
}
|
2001-02-02 19:26:56 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
ures_close(&res);
|
2001-02-02 19:26:56 +00:00
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
ures_close(top);
|
2001-02-02 19:26:56 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
return ids;
|
2001-02-02 19:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------
|
|
|
|
|
2002-03-20 00:15:44 +00:00
|
|
|
const UnicodeString**
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::createAvailableIDs(int32_t& numIDs)
|
|
|
|
{
|
|
|
|
// We are creating a new array to existing UnicodeString pointers.
|
|
|
|
// The caller will delete the array when done, but not the pointers
|
|
|
|
// in the array.
|
2003-08-15 18:21:31 +00:00
|
|
|
numIDs = 0;
|
|
|
|
if (!loadOlsonIDs()) {
|
1999-12-05 06:24:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-08-15 18:21:31 +00:00
|
|
|
|
|
|
|
const UnicodeString** ids =
|
|
|
|
(const UnicodeString** )uprv_malloc(OLSON_ZONE_COUNT * sizeof(UnicodeString *));
|
|
|
|
if (ids != 0) {
|
|
|
|
numIDs = OLSON_ZONE_COUNT;
|
|
|
|
for (int32_t i=0; i<numIDs; ++i) {
|
|
|
|
ids[i] = &OLSON_IDS[i];
|
|
|
|
}
|
1999-12-05 06:24:22 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-15 18:21:31 +00:00
|
|
|
return ids;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-11-12 23:52:27 +00:00
|
|
|
// ICU_TIMEZONE_USE_DEPRECATES
|
|
|
|
// #endif
|
|
|
|
// see above
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// ---------------------------------------
|
|
|
|
|
2000-09-27 19:16:54 +00:00
|
|
|
int32_t
|
|
|
|
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);
|
|
|
|
UResourceBundle *top = openOlsonResource(id, res, ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
int32_t size = ures_getSize(&res);
|
|
|
|
if (size == 4 || size == 6) {
|
|
|
|
UResourceBundle r;
|
|
|
|
ures_initStackObject(&r);
|
|
|
|
ures_getByIndex(&res, size-1, &r, &ec);
|
2003-08-19 01:02:59 +00:00
|
|
|
// result = ures_getSize(&r); // doesn't work
|
|
|
|
ures_getIntVector(&r, &result, &ec);
|
2003-08-19 00:24:49 +00:00
|
|
|
ures_close(&r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
ures_close(top);
|
|
|
|
return result;
|
2000-09-27 19:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------
|
|
|
|
|
|
|
|
const UnicodeString
|
|
|
|
TimeZone::getEquivalentID(const UnicodeString& id, int32_t 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)) {
|
|
|
|
int32_t size = ures_getSize(&res);
|
|
|
|
if (size == 4 || size == 6) {
|
|
|
|
UResourceBundle r;
|
|
|
|
ures_initStackObject(&r);
|
|
|
|
ures_getByIndex(&res, size-1, &r, &ec);
|
|
|
|
const int32_t* v = ures_getIntVector(&r, &size, &ec);
|
2003-08-19 01:02:59 +00:00
|
|
|
if (index >= 0 && index < size && getOlsonMeta()) {
|
|
|
|
zone = v[index] + OLSON_ZONE_START;
|
2003-08-19 00:24:49 +00:00
|
|
|
}
|
|
|
|
ures_close(&r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
if (zone >= 0) {
|
|
|
|
ures_getByIndex(top, zone, &res, &ec);
|
|
|
|
if (U_SUCCESS(ec)) {
|
|
|
|
const char* key = ures_getKey(&res);
|
|
|
|
result = UnicodeString(key, "");
|
|
|
|
}
|
|
|
|
ures_close(&res);
|
|
|
|
}
|
|
|
|
ures_close(top);
|
|
|
|
return result;
|
2000-09-27 19:16:54 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
SimpleDateFormat format(style == LONG ? "zzzz" : "z",locale,status);
|
|
|
|
|
1999-10-18 22:48:32 +00:00
|
|
|
if(!U_SUCCESS(status))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// *** SRL what do I do here?!!
|
|
|
|
return result.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new SimpleTimeZone as a stand-in for this zone; the
|
|
|
|
// stand-in will have no DST, or all DST, but the same ID and offset,
|
|
|
|
// and hence the same display name.
|
|
|
|
// We don't cache these because they're small and cheap to create.
|
|
|
|
UnicodeString tempID;
|
|
|
|
SimpleTimeZone *tz = daylight ?
|
1999-10-28 02:02:53 +00:00
|
|
|
// For the pure-DST zone, we use JANUARY and DECEMBER
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
new SimpleTimeZone(getRawOffset(), getID(tempID),
|
2003-02-21 08:32:22 +00:00
|
|
|
UCAL_JANUARY , 1, 0, 0,
|
|
|
|
UCAL_DECEMBER , 31, 0, U_MILLIS_PER_DAY, status) :
|
1999-08-16 21:50:52 +00:00
|
|
|
new SimpleTimeZone(getRawOffset(), getID(tempID));
|
|
|
|
|
|
|
|
format.applyPattern(style == LONG ? "zzzz" : "z");
|
|
|
|
Calendar *myCalendar = (Calendar*)format.getCalendar();
|
|
|
|
myCalendar->setTimeZone(*tz); // copy
|
|
|
|
|
|
|
|
delete tz;
|
|
|
|
|
|
|
|
FieldPosition pos(FieldPosition::DONT_CARE);
|
1999-10-28 02:02:53 +00:00
|
|
|
return format.format(UDate(196262345678.), result, pos); // Must use a valid date here.
|
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)
|
|
|
|
{
|
|
|
|
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 &&
|
1999-08-16 21:50:52 +00:00
|
|
|
idUppercase.startsWith(GMT_ID))
|
|
|
|
{
|
|
|
|
ParsePosition pos(GMT_ID_LENGTH);
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool negative = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t offset;
|
|
|
|
|
|
|
|
if (id[pos.getIndex()] == 0x002D /*'-'*/)
|
|
|
|
negative = TRUE;
|
|
|
|
else if (id[pos.getIndex()] != 0x002B /*'+'*/)
|
|
|
|
return 0;
|
|
|
|
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);
|
|
|
|
numberFormat->setParseIntegerOnly(TRUE);
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
offset = n.getLong();
|
|
|
|
|
1999-12-08 02:11:04 +00:00
|
|
|
if (pos.getIndex() < id.length() &&
|
1999-08-16 21:50:52 +00:00
|
|
|
id[pos.getIndex()] == 0x003A /*':'*/)
|
|
|
|
{
|
|
|
|
// hh:mm
|
|
|
|
offset *= 60;
|
|
|
|
pos.setIndex(pos.getIndex() + 1);
|
|
|
|
int32_t oldPos = pos.getIndex();
|
|
|
|
n.setLong(kParseFailed);
|
|
|
|
numberFormat->parse(id, n, pos);
|
2000-06-26 22:46:15 +00:00
|
|
|
if (pos.getIndex() == oldPos) {
|
|
|
|
delete numberFormat;
|
|
|
|
return 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
offset += n.getLong();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// hhmm or hh
|
|
|
|
|
|
|
|
// Be strict about interpreting something as hh; it must be
|
|
|
|
// an offset < 30, and it must be one or two digits. Thus
|
|
|
|
// 0010 is interpreted as 00:10, but 10 is interpreted as
|
|
|
|
// 10:00.
|
|
|
|
if (offset < 30 && (pos.getIndex() - start) <= 2)
|
|
|
|
offset *= 60; // hh, from 00 to 29; 30 is 00:30
|
|
|
|
else
|
|
|
|
offset = offset % 100 + offset / 100 * 60; // hhmm
|
|
|
|
}
|
|
|
|
|
|
|
|
if(negative)
|
|
|
|
offset = -offset;
|
|
|
|
|
|
|
|
delete numberFormat;
|
2000-06-26 22:46:15 +00:00
|
|
|
return new SimpleTimeZone(offset * 60000, CUSTOM_ID);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
TimeZone::hasSameRules(const TimeZone& other) const
|
|
|
|
{
|
|
|
|
return (getRawOffset() == other.getRawOffset() &&
|
|
|
|
useDaylightTime() == other.useDaylightTime());
|
|
|
|
}
|
|
|
|
|
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
|
2003-10-06 22:37:41 +00:00
|
|
|
|