+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*************************************************************************
-* Copyright (c) 1997-2010, International Business Machines Corporation
+* Copyright (c) 1997-2016, International Business Machines Corporation
* and others. All Rights Reserved.
**************************************************************************
*
#include "unicode/uobject.h"
#include "unicode/unistr.h"
#include "unicode/ures.h"
+#include "unicode/ucal.h"
+#if U_SHOW_CPLUSPLUS_API
U_NAMESPACE_BEGIN
class StringEnumeration;
* TimeZone *tz = TimeZone::createTimeZone("America/Los_Angeles");
* </pre>
* \htmlonly</blockquote>\endhtmlonly
- * You can use <code>getAvailableIDs</code> method to iterate through
- * all the supported time zone IDs. You can then choose a
+ * You can use the <code>createEnumeration</code> method to iterate through
+ * all the supported time zone IDs, or the <code>getCanonicalID</code> method to check
+ * if a time zone ID is supported or not. You can then choose a
* supported ID to get a <code>TimeZone</code>.
* If the time zone you want is not represented by one of the
* supported IDs, then you can create a custom time zone ID with
*
* For example, you might specify GMT+14:00 as a custom
* time zone ID. The <code>TimeZone</code> that is returned
- * when you specify a custom time zone ID does not include
- * daylight savings time.
+ * when you specify a custom time zone ID uses the specified
+ * offset from GMT(=UTC) and does not observe daylight saving
+ * time. For example, you might specify GMT+14:00 as a custom
+ * time zone ID to create a TimeZone representing 14 hours ahead
+ * of GMT (with no daylight saving time). In addition,
+ * <code>getCanonicalID</code> can also be used to
+ * normalize a custom time zone ID.
*
* TimeZone is an abstract class representing a time zone. A TimeZone is needed for
* Calendar to produce local time for a particular time zone. A TimeZone comprises
* </ul>
*
* (Only the ID is actually implemented in TimeZone; subclasses of TimeZone may handle
- * daylight savings time and GMT offset in different ways. Currently we only have one
- * TimeZone subclass: SimpleTimeZone.)
+ * daylight savings time and GMT offset in different ways. Currently we have the following
+ * TimeZone subclasses: RuleBasedTimeZone, SimpleTimeZone, and VTimeZone.)
* <P>
* The TimeZone class contains a static list containing a TimeZone object for every
* combination of GMT offset and daylight-savings time rules currently in use in the
virtual ~TimeZone();
/**
- * The GMT time zone has a raw offset of zero and does not use daylight
+ * Returns the "unknown" time zone.
+ * It behaves like the GMT/UTC time zone but has the
+ * <code>UCAL_UNKNOWN_ZONE_ID</code> = "Etc/Unknown".
+ * createTimeZone() returns a mutable clone of this time zone if the input ID is not recognized.
+ *
+ * @return the "unknown" time zone.
+ * @see UCAL_UNKNOWN_ZONE_ID
+ * @see createTimeZone
+ * @see getGMT
+ * @stable ICU 49
+ */
+ static const TimeZone& U_EXPORT2 getUnknown();
+
+ /**
+ * The GMT (=UTC) time zone has a raw offset of zero and does not use daylight
* savings time. This is a commonly used time zone.
- * @return the GMT time zone.
+ *
+ * <p>Note: For backward compatibility reason, the ID used by the time
+ * zone returned by this method is "GMT", although the ICU's canonical
+ * ID for the GMT time zone is "Etc/GMT".
+ *
+ * @return the GMT/UTC time zone.
+ * @see getUnknown
* @stable ICU 2.0
*/
static const TimeZone* U_EXPORT2 getGMT(void);
* Creates a <code>TimeZone</code> for the given ID.
* @param ID the ID for a <code>TimeZone</code>, such as "America/Los_Angeles",
* or a custom ID such as "GMT-8:00".
- * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
- * cannot be understood. Return result guaranteed to be non-null. If you
- * require that the specific zone asked for be returned, check the ID of the
- * return result.
+ * @return the specified <code>TimeZone</code>, or a mutable clone of getUnknown()
+ * if the given ID cannot be understood or if the given ID is "Etc/Unknown".
+ * The return result is guaranteed to be non-NULL.
+ * If you require that the specific zone asked for be returned,
+ * compare the result with getUnknown() or check the ID of the return result.
* @stable ICU 2.0
*/
static TimeZone* U_EXPORT2 createTimeZone(const UnicodeString& ID);
+ /**
+ * Returns an enumeration over system time zone IDs with the given
+ * filter conditions.
+ * @param zoneType The system time zone type.
+ * @param region The ISO 3166 two-letter country code or UN M.49
+ * three-digit area code. When NULL, no filtering
+ * done by region.
+ * @param rawOffset An offset from GMT in milliseconds, ignoring
+ * the effect of daylight savings time, if any.
+ * When NULL, no filtering done by zone offset.
+ * @param ec Output param to filled in with a success or
+ * an error.
+ * @return an enumeration object, owned by the caller.
+ * @stable ICU 4.8
+ */
+ static StringEnumeration* U_EXPORT2 createTimeZoneIDEnumeration(
+ USystemTimeZoneType zoneType,
+ const char* region,
+ const int32_t* rawOffset,
+ UErrorCode& ec);
+
/**
* Returns an enumeration over all recognized time zone IDs. (i.e.,
* all strings that createTimeZone() accepts)
static const UnicodeString U_EXPORT2 getEquivalentID(const UnicodeString& id,
int32_t index);
+ /**
+ * Creates an instance of TimeZone detected from the current host
+ * system configuration. If the host system detection routines fail,
+ * or if they specify a TimeZone or TimeZone offset which is not
+ * recognized, then the special TimeZone "Etc/Unknown" is returned.
+ *
+ * Note that ICU4C does not change the default time zone unless
+ * `TimeZone::adoptDefault(TimeZone*)` or
+ * `TimeZone::setDefault(const TimeZone&)` is explicitly called by a
+ * user. This method does not update the current ICU's default,
+ * and may return a different TimeZone from the one returned by
+ * `TimeZone::createDefault()`.
+ *
+ * <p>This function is not thread safe.</p>
+ *
+ * @return A new instance of TimeZone detected from the current host system
+ * configuration.
+ * @see adoptDefault
+ * @see setDefault
+ * @see createDefault
+ * @see getUnknown
+ * @stable ICU 55
+ */
+ static TimeZone* U_EXPORT2 detectHostTimeZone();
+
/**
* Creates a new copy of the default TimeZone for this host. Unless the default time
* zone has already been set using adoptDefault() or setDefault(), the default is
- * determined by querying the system using methods in TPlatformUtilities. If the
- * system routines fail, or if they specify a TimeZone or TimeZone offset which is not
- * recognized, the TimeZone indicated by the ID kLastResortID is instantiated
- * and made the default.
+ * determined by querying the host system configuration. If the host system detection
+ * routines fail, or if they specify a TimeZone or TimeZone offset which is not
+ * recognized, then the special TimeZone "Etc/Unknown" is instantiated and made the
+ * default.
*
* @return A default TimeZone. Clients are responsible for deleting the time zone
* object returned.
+ * @see getUnknown
* @stable ICU 2.0
*/
static TimeZone* U_EXPORT2 createDefault(void);
* Sets the default time zone (i.e., what's returned by createDefault()) to be the
* specified time zone. If NULL is specified for the time zone, the default time
* zone is set to the default host time zone. This call adopts the TimeZone object
- * passed in; the clent is no longer responsible for deleting it.
+ * passed in; the client is no longer responsible for deleting it.
+ *
+ * <p>This function is not thread safe. It is an error for multiple threads
+ * to concurrently attempt to set the default time zone, or for any thread
+ * to attempt to reference the default zone while another thread is setting it.
*
* @param zone A pointer to the new TimeZone object to use as the default.
* @stable ICU 2.0
*/
static void U_EXPORT2 adoptDefault(TimeZone* zone);
+#ifndef U_HIDE_SYSTEM_API
/**
* Same as adoptDefault(), except that the TimeZone object passed in is NOT adopted;
* the caller remains responsible for deleting it.
*
+ * <p>See the thread safety note under adoptDefault().
+ *
* @param zone The given timezone.
* @system
+ * @stable ICU 2.0
*/
static void U_EXPORT2 setDefault(const TimeZone& zone);
+#endif /* U_HIDE_SYSTEM_API */
/**
* Returns the timezone data version currently used by ICU.
* @param id The input time zone ID to be canonicalized.
* @param canonicalID Receives the canonical system time zone ID
* or the custom time zone ID in normalized format.
- * @param status Recevies the status. When the given time zone ID
+ * @param status Receives the status. When the given time zone ID
* is neither a known system time zone ID nor a
* valid custom time zone ID, U_ILLEGAL_ARGUMENT_ERROR
* is set.
* or the custom time zone ID in normalized format.
* @param isSystemID Receives if the given ID is a known system
* time zone ID.
- * @param status Recevies the status. When the given time zone ID
+ * @param status Receives the status. When the given time zone ID
* is neither a known system time zone ID nor a
* valid custom time zone ID, U_ILLEGAL_ARGUMENT_ERROR
* is set.
static UnicodeString& U_EXPORT2 getCanonicalID(const UnicodeString& id,
UnicodeString& canonicalID, UBool& isSystemID, UErrorCode& status);
+ /**
+ * Converts a system time zone ID to an equivalent Windows time zone ID. For example,
+ * Windows time zone ID "Pacific Standard Time" is returned for input "America/Los_Angeles".
+ *
+ * <p>There are system time zones that cannot be mapped to Windows zones. When the input
+ * system time zone ID is unknown or unmappable to a Windows time zone, then the result will be
+ * empty, but the operation itself remains successful (no error status set on return).
+ *
+ * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html">
+ * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes,
+ * please read the ICU user guide section <a href="http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data">
+ * Updating the Time Zone Data</a>.
+ *
+ * @param id A system time zone ID.
+ * @param winid Receives a Windows time zone ID. When the input system time zone ID is unknown
+ * or unmappable to a Windows time zone ID, then an empty string is set on return.
+ * @param status Receives the status.
+ * @return A reference to the result (<code>winid</code>).
+ * @see getIDForWindowsID
+ *
+ * @stable ICU 52
+ */
+ static UnicodeString& U_EXPORT2 getWindowsID(const UnicodeString& id,
+ UnicodeString& winid, UErrorCode& status);
+
+ /**
+ * Converts a Windows time zone ID to an equivalent system time zone ID
+ * for a region. For example, system time zone ID "America/Los_Angeles" is returned
+ * for input Windows ID "Pacific Standard Time" and region "US" (or <code>null</code>),
+ * "America/Vancouver" is returned for the same Windows ID "Pacific Standard Time" and
+ * region "CA".
+ *
+ * <p>Not all Windows time zones can be mapped to system time zones. When the input
+ * Windows time zone ID is unknown or unmappable to a system time zone, then the result
+ * will be empty, but the operation itself remains successful (no error status set on return).
+ *
+ * <p>This implementation utilizes <a href="http://unicode.org/cldr/charts/supplemental/zone_tzid.html">
+ * Zone-Tzid mapping data</a>. The mapping data is updated time to time. To get the latest changes,
+ * please read the ICU user guide section <a href="http://userguide.icu-project.org/datetime/timezone#TOC-Updating-the-Time-Zone-Data">
+ * Updating the Time Zone Data</a>.
+ *
+ * @param winid A Windows time zone ID.
+ * @param region A null-terminated region code, or <code>NULL</code> if no regional preference.
+ * @param id Receives a system time zone ID. When the input Windows time zone ID is unknown
+ * or unmappable to a system time zone ID, then an empty string is set on return.
+ * @param status Receives the status.
+ * @return A reference to the result (<code>id</code>).
+ * @see getWindowsID
+ *
+ * @stable ICU 52
+ */
+ static UnicodeString& U_EXPORT2 getIDForWindowsID(const UnicodeString& winid, const char* region,
+ UnicodeString& id, UErrorCode& status);
+
/**
* Returns true if the two TimeZones are equal. (The TimeZone version only compares
* IDs, but subclasses are expected to also compare the fields they add.)
* in the default locale.
* This method returns the long name, not including daylight savings.
* If the display name is not available for the locale,
- * then this method returns a string in the format
- * <code>GMT[+-]hh:mm</code>.
+ * then this method returns a string in the localized GMT offset format
+ * such as <code>GMT[+-]HH:mm</code>.
* @param result the human-readable name of this time zone in the default locale.
* @return A reference to 'result'.
* @stable ICU 2.0
* in the specified locale.
* This method returns the long name, not including daylight savings.
* If the display name is not available for the locale,
- * then this method returns a string in the format
- * <code>GMT[+-]hh:mm</code>.
+ * then this method returns a string in the localized GMT offset format
+ * such as <code>GMT[+-]HH:mm</code>.
* @param locale the locale in which to supply the display name.
* @param result the human-readable name of this time zone in the given locale
* or in the default locale if the given locale is not recognized.
* Returns a name of this time zone suitable for presentation to the user
* in the default locale.
* If the display name is not available for the locale,
- * then this method returns a string in the format
- * <code>GMT[+-]hh:mm</code>.
- * @param daylight if true, return the daylight savings name.
+ * then this method returns a string in the localized GMT offset format
+ * such as <code>GMT[+-]HH:mm</code>.
+ * @param inDaylight if true, return the daylight savings name.
* @param style
* @param result the human-readable name of this time zone in the default locale.
* @return A reference to 'result'.
* @stable ICU 2.0
*/
- UnicodeString& getDisplayName(UBool daylight, EDisplayType style, UnicodeString& result) const;
+ UnicodeString& getDisplayName(UBool inDaylight, EDisplayType style, UnicodeString& result) const;
/**
* Returns a name of this time zone suitable for presentation to the user
* in the specified locale.
* If the display name is not available for the locale,
- * then this method returns a string in the format
- * <code>GMT[+-]hh:mm</code>.
- * @param daylight if true, return the daylight savings name.
+ * then this method returns a string in the localized GMT offset format
+ * such as <code>GMT[+-]HH:mm</code>.
+ * @param inDaylight if true, return the daylight savings name.
* @param style
* @param locale the locale in which to supply the display name.
* @param result the human-readable name of this time zone in the given locale
* or in the default locale if the given locale is not recognized.
- * @return A refence to 'result'.
+ * @return A reference to 'result'.
* @stable ICU 2.0
*/
- UnicodeString& getDisplayName(UBool daylight, EDisplayType style, const Locale& locale, UnicodeString& result) const;
+ UnicodeString& getDisplayName(UBool inDaylight, EDisplayType style, const Locale& locale, UnicodeString& result) const;
/**
* Queries if this time zone uses daylight savings time.
* @return true if this time zone uses daylight savings time,
* false, otherwise.
+ * <p><strong>Note:</strong>The default implementation of
+ * ICU TimeZone uses the tz database, which supports historic
+ * rule changes, for system time zones. With the implementation,
+ * there are time zones that used daylight savings time in the
+ * past, but no longer used currently. For example, Asia/Tokyo has
+ * never used daylight savings time since 1951. Most clients would
+ * expect that this method to return <code>FALSE</code> for such case.
+ * The default implementation of this method returns <code>TRUE</code>
+ * when the time zone uses daylight savings time in the current
+ * (Gregorian) calendar year.
+ * <p>In Java 7, <code>observesDaylightTime()</code> was added in
+ * addition to <code>useDaylightTime()</code>. In Java, <code>useDaylightTime()</code>
+ * only checks if daylight saving time is observed by the last known
+ * rule. This specification might not be what most users would expect
+ * if daylight saving time is currently observed, but not scheduled
+ * in future. In this case, Java's <code>userDaylightTime()</code> returns
+ * <code>false</code>. To resolve the issue, Java 7 added <code>observesDaylightTime()</code>,
+ * which takes the current rule into account. The method <code>observesDaylightTime()</code>
+ * was added in ICU4J for supporting API signature compatibility with JDK.
+ * In general, ICU4C also provides JDK compatible methods, but the current
+ * implementation <code>userDaylightTime()</code> serves the purpose
+ * (takes the current rule into account), <code>observesDaylightTime()</code>
+ * is not added in ICU4C. In addition to <code>useDaylightTime()</code>, ICU4C
+ * <code>BasicTimeZone</code> class (Note that <code>TimeZone::createTimeZone(const UnicodeString &ID)</code>
+ * always returns a <code>BasicTimeZone</code>) provides a series of methods allowing
+ * historic and future time zone rule iteration, so you can check if daylight saving
+ * time is observed or not within a given period.
+ *
* @stable ICU 2.0
*/
virtual UBool useDaylightTime(void) const = 0;
*/
virtual int32_t getDSTSavings() const;
+ /**
+ * Gets the region code associated with the given
+ * system time zone ID. The region code is either ISO 3166
+ * 2-letter country code or UN M.49 3-digit area code.
+ * When the time zone is not associated with a specific location,
+ * for example - "Etc/UTC", "EST5EDT", then this method returns
+ * "001" (UN M.49 area code for World).
+ *
+ * @param id The system time zone ID.
+ * @param region Output buffer for receiving the region code.
+ * @param capacity The size of the output buffer.
+ * @param status Receives the status. When the given time zone ID
+ * is not a known system time zone ID,
+ * U_ILLEGAL_ARGUMENT_ERROR is set.
+ * @return The length of the output region code.
+ * @stable ICU 4.8
+ */
+ static int32_t U_EXPORT2 getRegion(const UnicodeString& id,
+ char *region, int32_t capacity, UErrorCode& status);
+
protected:
/**
*/
TimeZone& operator=(const TimeZone& right);
+#ifndef U_HIDE_INTERNAL_API
/**
* Utility function. For internally loading rule data.
* @param top Top resource bundle for tz data
* @internal
*/
static UResourceBundle* loadRule(const UResourceBundle* top, const UnicodeString& ruleid, UResourceBundle* oldbundle, UErrorCode&status);
+#endif /* U_HIDE_INTERNAL_API */
private:
friend class ZoneMeta;
static TimeZone* createCustomTimeZone(const UnicodeString&); // Creates a time zone based on the string.
+ /**
+ * Finds the given ID in the Olson tzdata. If the given ID is found in the tzdata,
+ * returns the pointer to the ID resource. This method is exposed through ZoneMeta class
+ * for ICU internal implementation and useful for building hashtable using a time zone
+ * ID as a key.
+ * @param id zone id string
+ * @return the pointer of the ID resource, or NULL.
+ */
+ static const char16_t* findID(const UnicodeString& id);
+
/**
* Resolve a link in Olson tzdata. When the given id is known and it's not a link,
* the id itself is returned. When the given id is known and it is a link, then
* @param id zone id string
* @return the dereferenced zone or NULL
*/
- static const UChar* dereferOlsonLink(const UnicodeString& id);
+ static const char16_t* dereferOlsonLink(const UnicodeString& id);
/**
- * Returns the region code associated with the given zone.
+ * Returns the region code associated with the given zone,
+ * or NULL if the zone is not known.
* @param id zone id string
* @return the region associated with the given zone
*/
- static const UChar* getRegion(const UnicodeString& id);
+ static const char16_t* getRegion(const UnicodeString& id);
+ public:
+#ifndef U_HIDE_INTERNAL_API
+ /**
+ * Returns the region code associated with the given zone,
+ * or NULL if the zone is not known.
+ * @param id zone id string
+ * @param status Status parameter
+ * @return the region associated with the given zone
+ * @internal
+ */
+ static const char16_t* getRegion(const UnicodeString& id, UErrorCode& status);
+#endif /* U_HIDE_INTERNAL_API */
+
+ private:
/**
* Parses the given custom time zone identifier
* @param id id A string of the form GMT[+-]hh:mm, GMT[+-]hhmm, or
* @return Returns TRUE when the given custom id is valid.
*/
static UBool parseCustomID(const UnicodeString& id, int32_t& sign, int32_t& hour,
- int32_t& min, int32_t& sec);
+ int32_t& minute, int32_t& second);
/**
* Parse a custom time zone identifier and return the normalized
UErrorCode& status);
/**
- * Returns the normalized custome time zone ID for the given offset fields.
+ * Returns the normalized custom time zone ID for the given offset fields.
* @param hour offset hours
* @param min offset minutes
* @param sec offset seconds
- * @param netative sign of the offset, TRUE for negative offset.
+ * @param negative sign of the offset, TRUE for negative offset.
* @param id Receves the format result (normalized custom ID)
* @return The reference to id
*/
static UnicodeString& formatCustomID(int32_t hour, int32_t min, int32_t sec,
UBool negative, UnicodeString& id);
- /**
- * Responsible for setting up DEFAULT_ZONE. Uses routines in TPlatformUtilities
- * (i.e., platform-specific calls) to get the current system time zone. Failing
- * that, uses the platform-specific default time zone. Failing that, uses GMT.
- */
- static void initDefault(void);
-
- // See source file for documentation
- /**
- * 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.
- * @param name tthe given name of a system time zone.
- * @return the TimeZone indicated by the 'name'.
- */
- static TimeZone* createSystemTimeZone(const UnicodeString& name);
-
UnicodeString fID; // this time zone's ID
+ friend class TZEnumeration;
};
fID = ID;
}
U_NAMESPACE_END
+#endif // U_SHOW_CPLUSPLUS_API
#endif /* #if !UCONFIG_NO_FORMATTING */