ICU 73.2  73.2
tzfmt.h
Go to the documentation of this file.
00001 // © 2016 and later: Unicode, Inc. and others.
00002 // License & terms of use: http://www.unicode.org/copyright.html
00003 /*
00004 *******************************************************************************
00005 * Copyright (C) 2011-2015, International Business Machines Corporation and
00006 * others. All Rights Reserved.
00007 *******************************************************************************
00008 */
00009 #ifndef __TZFMT_H
00010 #define __TZFMT_H
00011 
00017 #include "unicode/utypes.h"
00018 
00019 #if U_SHOW_CPLUSPLUS_API
00020 
00021 #if !UCONFIG_NO_FORMATTING
00022 
00023 #include "unicode/format.h"
00024 #include "unicode/timezone.h"
00025 #include "unicode/tznames.h"
00026 
00027 U_CDECL_BEGIN
00033 typedef enum UTimeZoneFormatStyle {
00038     UTZFMT_STYLE_GENERIC_LOCATION,
00043     UTZFMT_STYLE_GENERIC_LONG,
00048     UTZFMT_STYLE_GENERIC_SHORT,
00053     UTZFMT_STYLE_SPECIFIC_LONG,
00058     UTZFMT_STYLE_SPECIFIC_SHORT,
00063     UTZFMT_STYLE_LOCALIZED_GMT,
00069     UTZFMT_STYLE_LOCALIZED_GMT_SHORT,
00076     UTZFMT_STYLE_ISO_BASIC_SHORT,
00083     UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT,
00090     UTZFMT_STYLE_ISO_BASIC_FIXED,
00097     UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED,
00104     UTZFMT_STYLE_ISO_BASIC_FULL,
00111     UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL,
00118     UTZFMT_STYLE_ISO_EXTENDED_FIXED,
00125     UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED,
00132     UTZFMT_STYLE_ISO_EXTENDED_FULL,
00139     UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL,
00144     UTZFMT_STYLE_ZONE_ID,
00149     UTZFMT_STYLE_ZONE_ID_SHORT,
00154     UTZFMT_STYLE_EXEMPLAR_LOCATION
00155 } UTimeZoneFormatStyle;
00156 
00161 typedef enum UTimeZoneFormatGMTOffsetPatternType {
00166     UTZFMT_PAT_POSITIVE_HM,
00171     UTZFMT_PAT_POSITIVE_HMS,
00176     UTZFMT_PAT_NEGATIVE_HM,
00181     UTZFMT_PAT_NEGATIVE_HMS,
00186     UTZFMT_PAT_POSITIVE_H,
00191     UTZFMT_PAT_NEGATIVE_H,
00192 
00193     /* The following cannot be #ifndef U_HIDE_INTERNAL_API, needed for other .h declarations */
00198     UTZFMT_PAT_COUNT = 6
00199 } UTimeZoneFormatGMTOffsetPatternType;
00200 
00206 typedef enum UTimeZoneFormatTimeType {
00211     UTZFMT_TIME_TYPE_UNKNOWN,
00216     UTZFMT_TIME_TYPE_STANDARD,
00221     UTZFMT_TIME_TYPE_DAYLIGHT
00222 } UTimeZoneFormatTimeType;
00223 
00228 typedef enum UTimeZoneFormatParseOption {
00233     UTZFMT_PARSE_OPTION_NONE        = 0x00,
00240     UTZFMT_PARSE_OPTION_ALL_STYLES  = 0x01,
00248     UTZFMT_PARSE_OPTION_TZ_DATABASE_ABBREVIATIONS = 0x02
00249 } UTimeZoneFormatParseOption;
00250 
00251 U_CDECL_END
00252 
00253 U_NAMESPACE_BEGIN
00254 
00255 class TimeZoneGenericNames;
00256 class TZDBTimeZoneNames;
00257 class UVector;
00258 
00274 class U_I18N_API TimeZoneFormat : public Format {
00275 public:
00280     TimeZoneFormat(const TimeZoneFormat& other);
00281 
00286     virtual ~TimeZoneFormat();
00287 
00292     TimeZoneFormat& operator=(const TimeZoneFormat& other);
00293 
00302     virtual bool operator==(const Format& other) const override;
00303 
00310     virtual TimeZoneFormat* clone() const override;
00311 
00320     static TimeZoneFormat* U_EXPORT2 createInstance(const Locale& locale, UErrorCode& status);
00321 
00327     const TimeZoneNames* getTimeZoneNames() const;
00328 
00336     void adoptTimeZoneNames(TimeZoneNames *tznames);
00337 
00343     void setTimeZoneNames(const TimeZoneNames &tznames);
00344 
00352     UnicodeString& getGMTPattern(UnicodeString& pattern) const;
00353 
00362     void setGMTPattern(const UnicodeString& pattern, UErrorCode& status);
00363 
00372     UnicodeString& getGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, UnicodeString& pattern) const;
00373 
00382     void setGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, const UnicodeString& pattern, UErrorCode& status);
00383 
00393     UnicodeString& getGMTOffsetDigits(UnicodeString& digits) const;
00394 
00407     void setGMTOffsetDigits(const UnicodeString& digits, UErrorCode& status);
00408 
00416     UnicodeString& getGMTZeroFormat(UnicodeString& gmtZeroFormat) const;
00417 
00425     void setGMTZeroFormat(const UnicodeString& gmtZeroFormat, UErrorCode& status);
00426 
00434     uint32_t getDefaultParseOptions(void) const;
00435 
00444     void setDefaultParseOptions(uint32_t flags);
00445 
00461     UnicodeString& formatOffsetISO8601Basic(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
00462         UnicodeString& result, UErrorCode& status) const;
00463 
00479     UnicodeString& formatOffsetISO8601Extended(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
00480         UnicodeString& result, UErrorCode& status) const;
00481 
00501     UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const;
00502 
00522     UnicodeString& formatOffsetShortLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const;
00523 
00524     using Format::format;
00525 
00539     virtual UnicodeString& format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate date,
00540         UnicodeString& name, UTimeZoneFormatTimeType* timeType = nullptr) const;
00541 
00556     int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos) const;
00557 
00570     int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
00571 
00584     int32_t parseOffsetShortLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
00585 
00602     virtual TimeZone* parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
00603         int32_t parseOptions, UTimeZoneFormatTimeType* timeType = nullptr) const;
00604 
00620     TimeZone* parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
00621         UTimeZoneFormatTimeType* timeType = nullptr) const;
00622 
00623     /* ----------------------------------------------
00624      * Format APIs
00625      * ---------------------------------------------- */
00626 
00638     virtual UnicodeString& format(const Formattable& obj, UnicodeString& appendTo,
00639         FieldPosition& pos, UErrorCode& status) const override;
00640 
00653     virtual void parseObject(const UnicodeString& source, Formattable& result, ParsePosition& parse_pos) const override;
00654 
00659     static UClassID U_EXPORT2 getStaticClassID(void);
00660 
00665     virtual UClassID getDynamicClassID() const override;
00666 
00667 protected:
00674     TimeZoneFormat(const Locale& locale, UErrorCode& status);
00675 
00676 private:
00677     /* Locale of this object */
00678     Locale fLocale;
00679 
00680     /* Stores the region (could be implicit default) */
00681     char fTargetRegion[ULOC_COUNTRY_CAPACITY];
00682 
00683     /* TimeZoneNames object used by this formatter */
00684     TimeZoneNames* fTimeZoneNames;
00685 
00686     /* TimeZoneGenericNames object used by this formatter - lazily instantiated */
00687     TimeZoneGenericNames* fTimeZoneGenericNames;
00688 
00689     /* Localized GMT format pattern - e.g. "GMT{0}" */
00690     UnicodeString fGMTPattern;
00691 
00692     /* Array of offset patterns used by Localized GMT format - e.g. "+HH:mm" */
00693     UnicodeString fGMTOffsetPatterns[UTZFMT_PAT_COUNT];
00694 
00695     /* Localized decimal digits used by Localized GMT format */
00696     UChar32 fGMTOffsetDigits[10];
00697 
00698     /* Localized GMT zero format - e.g. "GMT" */
00699     UnicodeString fGMTZeroFormat;
00700 
00701     /* Bit flags representing parse options */
00702     uint32_t fDefParseOptionFlags;
00703 
00704     /* Constant parts of GMT format pattern, populated from localized GMT format pattern*/
00705     UnicodeString fGMTPatternPrefix;    /* Substring before {0} */
00706     UnicodeString fGMTPatternSuffix;    /* Substring after {0} */
00707 
00708     /* Compiled offset patterns generated from fGMTOffsetPatterns[] */
00709     UVector* fGMTOffsetPatternItems[UTZFMT_PAT_COUNT];
00710 
00711     UBool fAbuttingOffsetHoursAndMinutes;
00712 
00713     /* TZDBTimeZoneNames object used for parsing */
00714     TZDBTimeZoneNames* fTZDBTimeZoneNames;
00715 
00726     UnicodeString& formatSpecific(const TimeZone& tz, UTimeZoneNameType stdType, UTimeZoneNameType dstType,
00727         UDate date, UnicodeString& name, UTimeZoneFormatTimeType *timeType) const;
00728 
00737     UnicodeString& formatGeneric(const TimeZone& tz, int32_t genType, UDate date, UnicodeString& name) const;
00738 
00744     const TimeZoneGenericNames* getTimeZoneGenericNames(UErrorCode& status) const;
00745 
00751     const TZDBTimeZoneNames* getTZDBTimeZoneNames(UErrorCode& status) const;
00752 
00760     UnicodeString& formatExemplarLocation(const TimeZone& tz, UnicodeString& name) const;
00761 
00765     enum OffsetFields {
00766         FIELDS_H,
00767         FIELDS_HM,
00768         FIELDS_HMS
00769     };
00770 
00778     void initGMTPattern(const UnicodeString& gmtPattern, UErrorCode& status);
00779 
00788     static UVector* parseOffsetPattern(const UnicodeString& pattern, OffsetFields required, UErrorCode& status);
00789 
00798     static UnicodeString& expandOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
00799 
00808     static UnicodeString& truncateOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
00809 
00820     static UBool toCodePoints(const UnicodeString& str, UChar32* codeArray, int32_t capacity);
00821 
00832     UnicodeString& formatOffsetISO8601(int32_t offset, UBool isBasic, UBool useUtcIndicator,
00833         UBool isShort, UBool ignoreSeconds, UnicodeString& result, UErrorCode& status) const;
00834 
00842     UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UBool isShort, UnicodeString& result, UErrorCode& status) const;
00843 
00858     int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos, UBool extendedOnly,
00859         UBool* hasDigitOffset = nullptr) const;
00860 
00868     void appendOffsetDigits(UnicodeString& buf, int32_t n, uint8_t minDigits) const;
00869 
00882     int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos,
00883         UBool isShort, UBool* hasDigitOffset) const;
00884 
00894     int32_t parseOffsetLocalizedGMTPattern(const UnicodeString& text, int32_t start,
00895         UBool isShort, int32_t& parsedLen) const;
00896 
00905     int32_t parseOffsetFields(const UnicodeString& text, int32_t start, UBool isShort, int32_t& parsedLen) const;
00906 
00918     int32_t parseOffsetFieldsWithPattern(const UnicodeString& text, int32_t start,
00919         UVector* patternItems, UBool forceSingleHourDigit, int32_t& hour, int32_t& min, int32_t& sec) const;
00920 
00928     int32_t parseAbuttingOffsetFields(const UnicodeString& text, int32_t start, int32_t& parsedLen) const;
00929 
00937     int32_t parseOffsetDefaultLocalizedGMT(const UnicodeString& text, int start, int32_t& parsedLen) const;
00938 
00947     int32_t parseDefaultOffsetFields(const UnicodeString& text, int32_t start, char16_t separator,
00948         int32_t& parsedLen) const;
00949 
00964     int32_t parseOffsetFieldWithLocalizedDigits(const UnicodeString& text, int32_t start,
00965         uint8_t minDigits, uint8_t maxDigits, uint16_t minVal, uint16_t maxVal, int32_t& parsedLen) const;
00966 
00976     int32_t parseSingleLocalizedDigit(const UnicodeString& text, int32_t start, int32_t& len) const;
00977 
00987     static UnicodeString& formatOffsetWithAsciiDigits(int32_t offset, char16_t sep,
00988         OffsetFields minFields, OffsetFields maxFields, UnicodeString& result);
00989 
01002     static int32_t parseAbuttingAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos,
01003         OffsetFields minFields, OffsetFields maxFields, UBool fixedHourWidth);
01004 
01017     static int32_t parseAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, char16_t sep,
01018         OffsetFields minFields, OffsetFields maxFields);
01019 
01026     static UnicodeString& unquote(const UnicodeString& pattern, UnicodeString& result);
01027 
01033     void initGMTOffsetPatterns(UErrorCode& status);
01034 
01041     void checkAbuttingHoursAndMinutes();
01042 
01048     TimeZone* createTimeZoneForOffset(int32_t offset) const;
01049 
01055     static UTimeZoneFormatTimeType getTimeType(UTimeZoneNameType nameType);
01056 
01065     UnicodeString& getTimeZoneID(const TimeZoneNames::MatchInfoCollection* matches, int32_t idx, UnicodeString& tzID) const;
01066 
01067 
01075     UnicodeString& parseZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
01076 
01084     UnicodeString& parseShortZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
01085 
01093     UnicodeString& parseExemplarLocation(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
01094 };
01095 
01096 U_NAMESPACE_END
01097 
01098 #endif /* !UCONFIG_NO_FORMATTING */
01099 
01100 #endif /* U_SHOW_CPLUSPLUS_API */
01101 
01102 #endif