ICU 78.3  78.3
tzfmt.h
Go to the documentation of this file.
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2011-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 */
9 #ifndef __TZFMT_H
10 #define __TZFMT_H
11 
17 #include "unicode/utypes.h"
18 
19 #if U_SHOW_CPLUSPLUS_API
20 
21 #if !UCONFIG_NO_FORMATTING
22 
23 #include "unicode/format.h"
24 #include "unicode/timezone.h"
25 #include "unicode/tznames.h"
26 
33 typedef enum UTimeZoneFormatStyle {
156 
192 
193  /* The following cannot be #ifndef U_HIDE_INTERNAL_API, needed for other .h declarations */
200 
223 
250 
252 
253 U_NAMESPACE_BEGIN
254 
255 class TimeZoneGenericNames;
256 class TZDBTimeZoneNames;
257 class UVector;
258 
275 public:
281 
286  U_I18N_API virtual ~TimeZoneFormat();
287 
293 
302  U_I18N_API virtual bool operator==(const Format& other) const override;
303 
310  U_I18N_API virtual TimeZoneFormat* clone() const override;
311 
320  U_I18N_API static TimeZoneFormat* U_EXPORT2 createInstance(const Locale& locale, UErrorCode& status);
321 
327  U_I18N_API const TimeZoneNames* getTimeZoneNames() const;
328 
336  U_I18N_API void adoptTimeZoneNames(TimeZoneNames* tznames);
337 
343  U_I18N_API void setTimeZoneNames(const TimeZoneNames& tznames);
344 
352  U_I18N_API UnicodeString& getGMTPattern(UnicodeString& pattern) const;
353 
362  U_I18N_API void setGMTPattern(const UnicodeString& pattern, UErrorCode& status);
363 
373  UnicodeString& pattern) const;
374 
383  U_I18N_API void setGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type,
384  const UnicodeString& pattern, UErrorCode& status);
385 
395  U_I18N_API UnicodeString& getGMTOffsetDigits(UnicodeString& digits) const;
396 
409  U_I18N_API void setGMTOffsetDigits(const UnicodeString& digits, UErrorCode& status);
410 
418  U_I18N_API UnicodeString& getGMTZeroFormat(UnicodeString& gmtZeroFormat) const;
419 
427  U_I18N_API void setGMTZeroFormat(const UnicodeString& gmtZeroFormat, UErrorCode& status);
428 
436  U_I18N_API uint32_t getDefaultParseOptions() const;
437 
446  U_I18N_API void setDefaultParseOptions(uint32_t flags);
447 
463  U_I18N_API UnicodeString& formatOffsetISO8601Basic(int32_t offset,
464  UBool useUtcIndicator,
465  UBool isShort,
466  UBool ignoreSeconds,
467  UnicodeString& result,
468  UErrorCode& status) const;
469 
485  U_I18N_API UnicodeString& formatOffsetISO8601Extended(int32_t offset,
486  UBool useUtcIndicator,
487  UBool isShort,
488  UBool ignoreSeconds,
489  UnicodeString& result,
490  UErrorCode& status) const;
491 
511  U_I18N_API UnicodeString& formatOffsetLocalizedGMT(int32_t offset,
512  UnicodeString& result,
513  UErrorCode& status) const;
514 
534  U_I18N_API UnicodeString& formatOffsetShortLocalizedGMT(int32_t offset,
535  UnicodeString& result,
536  UErrorCode& status) const;
537 
538  using Format::format;
539 
554  const TimeZone& tz,
555  UDate date,
556  UnicodeString& name,
557  UTimeZoneFormatTimeType* timeType = nullptr) const;
558 
573  U_I18N_API int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos) const;
574 
587  U_I18N_API int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
588 
601  U_I18N_API int32_t parseOffsetShortLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
602 
619  U_I18N_API virtual TimeZone* parse(UTimeZoneFormatStyle style,
620  const UnicodeString& text,
621  ParsePosition& pos,
622  int32_t parseOptions,
623  UTimeZoneFormatTimeType* timeType = nullptr) const;
624 
641  const UnicodeString& text,
642  ParsePosition& pos,
643  UTimeZoneFormatTimeType* timeType = nullptr) const;
644 
645  /* ----------------------------------------------
646  * Format APIs
647  * ---------------------------------------------- */
648 
660  U_I18N_API virtual UnicodeString& format(const Formattable& obj,
661  UnicodeString& appendTo,
662  FieldPosition& pos,
663  UErrorCode& status) const override;
664 
677  U_I18N_API virtual void parseObject(const UnicodeString& source,
678  Formattable& result,
679  ParsePosition& parse_pos) const override;
680 
685  U_I18N_API static UClassID getStaticClassID();
686 
691  U_I18N_API virtual UClassID getDynamicClassID() const override;
692 
693 protected:
700  U_I18N_API TimeZoneFormat(const Locale& locale, UErrorCode& status);
701 
702 private:
703  /* Locale of this object */
704  Locale fLocale;
705 
706  /* Stores the region (could be implicit default) */
707  char fTargetRegion[ULOC_COUNTRY_CAPACITY];
708 
709  /* TimeZoneNames object used by this formatter */
710  TimeZoneNames* fTimeZoneNames;
711 
712  /* TimeZoneGenericNames object used by this formatter - lazily instantiated */
713  TimeZoneGenericNames* fTimeZoneGenericNames;
714 
715  /* Localized GMT format pattern - e.g. "GMT{0}" */
716  UnicodeString fGMTPattern;
717 
718  /* Array of offset patterns used by Localized GMT format - e.g. "+HH:mm" */
719  UnicodeString fGMTOffsetPatterns[UTZFMT_PAT_COUNT];
720 
721  /* Localized decimal digits used by Localized GMT format */
722  UChar32 fGMTOffsetDigits[10];
723 
724  /* Localized GMT zero format - e.g. "GMT" */
725  UnicodeString fGMTZeroFormat;
726 
727  /* Bit flags representing parse options */
728  uint32_t fDefParseOptionFlags;
729 
730  /* Constant parts of GMT format pattern, populated from localized GMT format pattern*/
731  UnicodeString fGMTPatternPrefix; /* Substring before {0} */
732  UnicodeString fGMTPatternSuffix; /* Substring after {0} */
733 
734  /* Compiled offset patterns generated from fGMTOffsetPatterns[] */
735  UVector* fGMTOffsetPatternItems[UTZFMT_PAT_COUNT];
736 
737  UBool fAbuttingOffsetHoursAndMinutes;
738 
739  /* TZDBTimeZoneNames object used for parsing */
740  TZDBTimeZoneNames* fTZDBTimeZoneNames;
741 
752  UnicodeString& formatSpecific(const TimeZone& tz, UTimeZoneNameType stdType, UTimeZoneNameType dstType,
753  UDate date, UnicodeString& name, UTimeZoneFormatTimeType *timeType) const;
754 
763  UnicodeString& formatGeneric(const TimeZone& tz, int32_t genType, UDate date, UnicodeString& name) const;
764 
770  const TimeZoneGenericNames* getTimeZoneGenericNames(UErrorCode& status) const;
771 
777  const TZDBTimeZoneNames* getTZDBTimeZoneNames(UErrorCode& status) const;
778 
786  UnicodeString& formatExemplarLocation(const TimeZone& tz, UnicodeString& name) const;
787 
791  enum OffsetFields {
792  FIELDS_H,
793  FIELDS_HM,
794  FIELDS_HMS
795  };
796 
804  void initGMTPattern(const UnicodeString& gmtPattern, UErrorCode& status);
805 
814  static UVector* parseOffsetPattern(const UnicodeString& pattern, OffsetFields required, UErrorCode& status);
815 
824  static UnicodeString& expandOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
825 
834  static UnicodeString& truncateOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
835 
846  static UBool toCodePoints(const UnicodeString& str, UChar32* codeArray, int32_t capacity);
847 
858  UnicodeString& formatOffsetISO8601(int32_t offset, UBool isBasic, UBool useUtcIndicator,
859  UBool isShort, UBool ignoreSeconds, UnicodeString& result, UErrorCode& status) const;
860 
868  UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UBool isShort, UnicodeString& result, UErrorCode& status) const;
869 
884  int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos, UBool extendedOnly,
885  UBool* hasDigitOffset = nullptr) const;
886 
894  void appendOffsetDigits(UnicodeString& buf, int32_t n, uint8_t minDigits) const;
895 
908  int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos,
909  UBool isShort, UBool* hasDigitOffset) const;
910 
920  int32_t parseOffsetLocalizedGMTPattern(const UnicodeString& text, int32_t start,
921  UBool isShort, int32_t& parsedLen) const;
922 
931  int32_t parseOffsetFields(const UnicodeString& text, int32_t start, UBool isShort, int32_t& parsedLen) const;
932 
944  int32_t parseOffsetFieldsWithPattern(const UnicodeString& text, int32_t start,
945  UVector* patternItems, UBool forceSingleHourDigit, int32_t& hour, int32_t& min, int32_t& sec) const;
946 
954  int32_t parseAbuttingOffsetFields(const UnicodeString& text, int32_t start, int32_t& parsedLen) const;
955 
963  int32_t parseOffsetDefaultLocalizedGMT(const UnicodeString& text, int start, int32_t& parsedLen) const;
964 
973  int32_t parseDefaultOffsetFields(const UnicodeString& text, int32_t start, char16_t separator,
974  int32_t& parsedLen) const;
975 
990  int32_t parseOffsetFieldWithLocalizedDigits(const UnicodeString& text, int32_t start,
991  uint8_t minDigits, uint8_t maxDigits, uint16_t minVal, uint16_t maxVal, int32_t& parsedLen) const;
992 
1002  int32_t parseSingleLocalizedDigit(const UnicodeString& text, int32_t start, int32_t& len) const;
1003 
1013  static UnicodeString& formatOffsetWithAsciiDigits(int32_t offset, char16_t sep,
1014  OffsetFields minFields, OffsetFields maxFields, UnicodeString& result);
1015 
1028  static int32_t parseAbuttingAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos,
1029  OffsetFields minFields, OffsetFields maxFields, UBool fixedHourWidth);
1030 
1043  static int32_t parseAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, char16_t sep,
1044  OffsetFields minFields, OffsetFields maxFields);
1045 
1052  static UnicodeString& unquote(const UnicodeString& pattern, UnicodeString& result);
1053 
1059  void initGMTOffsetPatterns(UErrorCode& status);
1060 
1067  void checkAbuttingHoursAndMinutes();
1068 
1074  TimeZone* createTimeZoneForOffset(int32_t offset) const;
1075 
1081  static UTimeZoneFormatTimeType getTimeType(UTimeZoneNameType nameType);
1082 
1091  UnicodeString& getTimeZoneID(const TimeZoneNames::MatchInfoCollection* matches, int32_t idx, UnicodeString& tzID) const;
1092 
1093 
1101  UnicodeString& parseZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
1102 
1110  UnicodeString& parseShortZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
1111 
1119  UnicodeString& parseExemplarLocation(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
1120 };
1121 
1122 U_NAMESPACE_END
1123 
1124 #endif /* !UCONFIG_NO_FORMATTING */
1125 
1126 #endif /* U_SHOW_CPLUSPLUS_API */
1127 
1128 #endif
When a time zone display name is not found within a set of display names used for the specified style...
Definition: tzfmt.h:240
Base class for all formats.
Definition: format.h:98
UTimeZoneFormatGMTOffsetPatternType
Constants for GMT offset pattern types.
Definition: tzfmt.h:161
Negative offset with hours and minutes fields.
Definition: tzfmt.h:176
TimeZoneFormat supports time zone display name formatting and parsing.
Definition: tzfmt.h:274
Fixed width ISO 8601 local time difference (basic format) or the UTC indicator.
Definition: tzfmt.h:90
ISO 8601 local time difference (basic format) with optional seconds field.
Definition: tzfmt.h:111
UTimeZoneFormatParseOption
Constants for parse option flags, used for specifying optional parse behavior.
Definition: tzfmt.h:228
Negative offset with hours field.
Definition: tzfmt.h:191
UTimeZoneFormatTimeType
Constants for time types used by TimeZoneFormat APIs for receiving time type (standard time...
Definition: tzfmt.h:206
Generic location format, such as "United States Time (New York)", "Italy Time".
Definition: tzfmt.h:38
ISO 8601 local time difference (extended format) with optional seconds field, or the UTC indicator...
Definition: tzfmt.h:132
Short localized GMT offset format, such as "GMT-5", "UTC+1:30" This style is equivalent to the LDML d...
Definition: tzfmt.h:69
Positive offset with hours field.
Definition: tzfmt.h:186
C++ API: TimeZone object.
double UDate
Date and Time data type.
Definition: utypes.h:218
#define ULOC_COUNTRY_CAPACITY
Useful constant for the maximum size of the country part of a locale ID (including the terminating NU...
Definition: uloc.h:258
U_I18N_API Format & operator=(const Format &)
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Negative offset with hours, minutes and seconds fields.
Definition: tzfmt.h:181
virtual U_I18N_API Format * clone() const =0
Clone this object polymorphically.
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
C++ API: Base class for all formats.
#define U_CDECL_BEGIN
This is used to begin a declaration of a library private ICU C API.
Definition: umachine.h:85
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:457
Fixed width ISO 8601 local time difference (basic format).
Definition: tzfmt.h:97
Exemplar location, such as "Los Angeles" and "Paris".
Definition: tzfmt.h:154
MatchInfoCollection represents a collection of time zone name matches used by TimeZoneNames#find.
Definition: tznames.h:310
Fixed width ISO 8601 local time difference (extended format) or the UTC indicator.
Definition: tzfmt.h:118
Daylight saving time.
Definition: tzfmt.h:221
Positive offset with hours and minutes fields.
Definition: tzfmt.h:166
TimeZoneNames is an abstract class representing the time zone display name data model defined by UTS#...
Definition: tznames.h:131
Short ISO 8601 locale time difference (basic format).
Definition: tzfmt.h:83
C++ API: TimeZoneNames.
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:449
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
ISO 8601 local time difference (basic format) with optional seconds field, or the UTC indicator...
Definition: tzfmt.h:104
UTimeZoneFormatStyle
Constants for time zone display format style used by format/parse APIs in TimeZoneFormat.
Definition: tzfmt.h:33
Time Zone ID, such as "America/Los_Angeles".
Definition: tzfmt.h:144
Specific short format, such as "EST", "PDT".
Definition: tzfmt.h:58
#define U_CDECL_END
This is used to end a declaration of a library private ICU C API.
Definition: umachine.h:86
Positive offset with hours, minutes and seconds fields.
Definition: tzfmt.h:171
Short Time Zone ID (BCP 47 Unicode location extension, time zone type value), such as "uslax"...
Definition: tzfmt.h:149
Generic long non-location format, such as "Eastern Time".
Definition: tzfmt.h:43
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
virtual U_I18N_API void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
Standard time.
Definition: tzfmt.h:216
Fixed width ISO 8601 local time difference (extended format).
Definition: tzfmt.h:125
Basic definitions for ICU, for both C and C++ APIs.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:302
Specific long format, such as "Eastern Standard Time".
Definition: tzfmt.h:53
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
UTimeZoneNameType
Constants for time zone display name types.
Definition: tznames.h:31
Short ISO 8601 local time difference (basic format) or the UTC indicator.
Definition: tzfmt.h:76
ISO 8601 local time difference (extended format) with optional seconds field.
Definition: tzfmt.h:139
When parsing a time zone display name in UTZFMT_STYLE_SPECIFIC_SHORT, look for the IANA tz database c...
Definition: tzfmt.h:248
virtual U_I18N_API bool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
Localized GMT offset format, such as "GMT-05:00", "UTC+0100".
Definition: tzfmt.h:63
Number of UTimeZoneFormatGMTOffsetPatternType types.
Definition: tzfmt.h:198
Generic short non-location format, such as "ET".
Definition: tzfmt.h:48
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198