1/*
2*******************************************************************************
3* Copyright (C) 2011-2014, International Business Machines Corporation and
4* others. All Rights Reserved.
5*******************************************************************************
6*/
7#ifndef __TZFMT_H
8#define __TZFMT_H
9
10/**
11 * \file
12 * \brief C++ API: TimeZoneFormat
13 */
14
15#include "unicode/utypes.h"
16
17#if !UCONFIG_NO_FORMATTING
18
19#include "unicode/format.h"
20#include "unicode/timezone.h"
21#include "unicode/tznames.h"
22
23U_CDECL_BEGIN
24/**
25 * Constants for time zone display format style used by format/parse APIs
26 * in TimeZoneFormat.
27 * @stable ICU 50
28 */
29typedef enum UTimeZoneFormatStyle {
30    /**
31     * Generic location format, such as "United States Time (New York)", "Italy Time"
32     * @stable ICU 50
33     */
34    UTZFMT_STYLE_GENERIC_LOCATION,
35    /**
36     * Generic long non-location format, such as "Eastern Time".
37     * @stable ICU 50
38     */
39    UTZFMT_STYLE_GENERIC_LONG,
40    /**
41     * Generic short non-location format, such as "ET".
42     * @stable ICU 50
43     */
44    UTZFMT_STYLE_GENERIC_SHORT,
45    /**
46     * Specific long format, such as "Eastern Standard Time".
47     * @stable ICU 50
48     */
49    UTZFMT_STYLE_SPECIFIC_LONG,
50    /**
51     * Specific short format, such as "EST", "PDT".
52     * @stable ICU 50
53     */
54    UTZFMT_STYLE_SPECIFIC_SHORT,
55    /**
56     * Localized GMT offset format, such as "GMT-05:00", "UTC+0100"
57     * @stable ICU 50
58     */
59    UTZFMT_STYLE_LOCALIZED_GMT,
60    /**
61     * Short localized GMT offset format, such as "GMT-5", "UTC+1:30"
62     * This style is equivalent to the LDML date format pattern "O".
63     * @stable ICU 51
64     */
65    UTZFMT_STYLE_LOCALIZED_GMT_SHORT,
66    /**
67     * Short ISO 8601 local time difference (basic format) or the UTC indicator.
68     * For example, "-05", "+0530", and "Z"(UTC).
69     * This style is equivalent to the LDML date format pattern "X".
70     * @stable ICU 51
71     */
72    UTZFMT_STYLE_ISO_BASIC_SHORT,
73    /**
74     * Short ISO 8601 locale time difference (basic format).
75     * For example, "-05" and "+0530".
76     * This style is equivalent to the LDML date format pattern "x".
77     * @stable ICU 51
78     */
79    UTZFMT_STYLE_ISO_BASIC_LOCAL_SHORT,
80    /**
81     * Fixed width ISO 8601 local time difference (basic format) or the UTC indicator.
82     * For example, "-0500", "+0530", and "Z"(UTC).
83     * This style is equivalent to the LDML date format pattern "XX".
84     * @stable ICU 51
85     */
86    UTZFMT_STYLE_ISO_BASIC_FIXED,
87    /**
88     * Fixed width ISO 8601 local time difference (basic format).
89     * For example, "-0500" and "+0530".
90     * This style is equivalent to the LDML date format pattern "xx".
91     * @stable ICU 51
92     */
93    UTZFMT_STYLE_ISO_BASIC_LOCAL_FIXED,
94    /**
95     * ISO 8601 local time difference (basic format) with optional seconds field, or the UTC indicator.
96     * For example, "-0500", "+052538", and "Z"(UTC).
97     * This style is equivalent to the LDML date format pattern "XXXX".
98     * @stable ICU 51
99     */
100    UTZFMT_STYLE_ISO_BASIC_FULL,
101    /**
102     * ISO 8601 local time difference (basic format) with optional seconds field.
103     * For example, "-0500" and "+052538".
104     * This style is equivalent to the LDML date format pattern "xxxx".
105     * @stable ICU 51
106     */
107    UTZFMT_STYLE_ISO_BASIC_LOCAL_FULL,
108    /**
109     * Fixed width ISO 8601 local time difference (extended format) or the UTC indicator.
110     * For example, "-05:00", "+05:30", and "Z"(UTC).
111     * This style is equivalent to the LDML date format pattern "XXX".
112     * @stable ICU 51
113     */
114    UTZFMT_STYLE_ISO_EXTENDED_FIXED,
115    /**
116     * Fixed width ISO 8601 local time difference (extended format).
117     * For example, "-05:00" and "+05:30".
118     * This style is equivalent to the LDML date format pattern "xxx" and "ZZZZZ".
119     * @stable ICU 51
120     */
121    UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FIXED,
122    /**
123     * ISO 8601 local time difference (extended format) with optional seconds field, or the UTC indicator.
124     * For example, "-05:00", "+05:25:38", and "Z"(UTC).
125     * This style is equivalent to the LDML date format pattern "XXXXX".
126     * @stable ICU 51
127     */
128    UTZFMT_STYLE_ISO_EXTENDED_FULL,
129    /**
130     * ISO 8601 local time difference (extended format) with optional seconds field.
131     * For example, "-05:00" and "+05:25:38".
132     * This style is equivalent to the LDML date format pattern "xxxxx".
133     * @stable ICU 51
134     */
135    UTZFMT_STYLE_ISO_EXTENDED_LOCAL_FULL,
136    /**
137     * Time Zone ID, such as "America/Los_Angeles".
138     * @stable ICU 51
139     */
140    UTZFMT_STYLE_ZONE_ID,
141    /**
142     * Short Time Zone ID (BCP 47 Unicode location extension, time zone type value), such as "uslax".
143     * @stable ICU 51
144     */
145    UTZFMT_STYLE_ZONE_ID_SHORT,
146    /**
147     * Exemplar location, such as "Los Angeles" and "Paris".
148     * @stable ICU 51
149     */
150    UTZFMT_STYLE_EXEMPLAR_LOCATION
151} UTimeZoneFormatStyle;
152
153/**
154 * Constants for GMT offset pattern types.
155 * @stable ICU 50
156 */
157typedef enum UTimeZoneFormatGMTOffsetPatternType {
158    /**
159     * Positive offset with hours and minutes fields
160     * @stable ICU 50
161     */
162    UTZFMT_PAT_POSITIVE_HM,
163    /**
164     * Positive offset with hours, minutes and seconds fields
165     * @stable ICU 50
166     */
167    UTZFMT_PAT_POSITIVE_HMS,
168    /**
169     * Negative offset with hours and minutes fields
170     * @stable ICU 50
171     */
172    UTZFMT_PAT_NEGATIVE_HM,
173    /**
174     * Negative offset with hours, minutes and seconds fields
175     * @stable ICU 50
176     */
177    UTZFMT_PAT_NEGATIVE_HMS,
178    /**
179     * Positive offset with hours field
180     * @stable ICU 51
181     */
182    UTZFMT_PAT_POSITIVE_H,
183    /**
184     * Negative offset with hours field
185     * @stable ICU 51
186     */
187    UTZFMT_PAT_NEGATIVE_H,
188
189    /* The following cannot be #ifndef U_HIDE_INTERNAL_API, needed for other .h declarations */
190    /**
191     * Number of UTimeZoneFormatGMTOffsetPatternType types.
192     * @internal
193     */
194    UTZFMT_PAT_COUNT = 6
195} UTimeZoneFormatGMTOffsetPatternType;
196
197/**
198 * Constants for time types used by TimeZoneFormat APIs for
199 * receiving time type (standard time, daylight time or unknown).
200 * @stable ICU 50
201 */
202typedef enum UTimeZoneFormatTimeType {
203    /**
204     * Unknown
205     * @stable ICU 50
206     */
207    UTZFMT_TIME_TYPE_UNKNOWN,
208    /**
209     * Standard time
210     * @stable ICU 50
211     */
212    UTZFMT_TIME_TYPE_STANDARD,
213    /**
214     * Daylight saving time
215     * @stable ICU 50
216     */
217    UTZFMT_TIME_TYPE_DAYLIGHT
218} UTimeZoneFormatTimeType;
219
220/**
221 * Constants for parse option flags, used for specifying optional parse behavior.
222 * @stable ICU 50
223 */
224typedef enum UTimeZoneFormatParseOption {
225    /**
226     * No option.
227     * @stable ICU 50
228     */
229    UTZFMT_PARSE_OPTION_NONE        = 0x00,
230    /**
231     * When a time zone display name is not found within a set of display names
232     * used for the specified style, look for the name from display names used
233     * by other styles.
234     * @stable ICU 50
235     */
236    UTZFMT_PARSE_OPTION_ALL_STYLES  = 0x01
237} UTimeZoneFormatParseOption;
238
239U_CDECL_END
240
241U_NAMESPACE_BEGIN
242
243class TimeZoneGenericNames;
244class UVector;
245
246/**
247 * <code>TimeZoneFormat</code> supports time zone display name formatting and parsing.
248 * An instance of TimeZoneFormat works as a subformatter of {@link SimpleDateFormat},
249 * but you can also directly get a new instance of <code>TimeZoneFormat</code> and
250 * formatting/parsing time zone display names.
251 * <p>
252 * ICU implements the time zone display names defined by <a href="http://www.unicode.org/reports/tr35/">UTS#35
253 * Unicode Locale Data Markup Language (LDML)</a>. {@link TimeZoneNames} represents the
254 * time zone display name data model and this class implements the algorithm for actual
255 * formatting and parsing.
256 *
257 * @see SimpleDateFormat
258 * @see TimeZoneNames
259 * @stable ICU 50
260 */
261class U_I18N_API TimeZoneFormat : public Format {
262public:
263    /**
264     * Copy constructor.
265     * @stable ICU 50
266     */
267    TimeZoneFormat(const TimeZoneFormat& other);
268
269    /**
270     * Destructor.
271     * @stable ICU 50
272     */
273    virtual ~TimeZoneFormat();
274
275    /**
276     * Assignment operator.
277     * @stable ICU 50
278     */
279    TimeZoneFormat& operator=(const TimeZoneFormat& other);
280
281    /**
282     * Return true if the given Format objects are semantically equal.
283     * Objects of different subclasses are considered unequal.
284     * @param other The object to be compared with.
285     * @return Return TRUE if the given Format objects are semantically equal.
286     *                Objects of different subclasses are considered unequal.
287     * @stable ICU 50
288     */
289    virtual UBool operator==(const Format& other) const;
290
291    /**
292     * Clone this object polymorphically. The caller is responsible
293     * for deleting the result when done.
294     * @return A copy of the object
295     * @stable ICU 50
296     */
297    virtual Format* clone() const;
298
299    /**
300     * Creates an instance of <code>TimeZoneFormat</code> for the given locale.
301     * @param locale The locale.
302     * @param status Receives the status.
303     * @return An instance of <code>TimeZoneFormat</code> for the given locale,
304     *          owned by the caller.
305     * @stable ICU 50
306     */
307    static TimeZoneFormat* U_EXPORT2 createInstance(const Locale& locale, UErrorCode& status);
308
309    /**
310     * Returns the time zone display name data used by this instance.
311     * @return The time zone display name data.
312     * @stable ICU 50
313     */
314    const TimeZoneNames* getTimeZoneNames() const;
315
316    /**
317     * Sets the time zone display name data to this format instnace.
318     * The caller should not delete the TimeZoenNames object after it is adopted
319     * by this call.
320     * @param tznames TimeZoneNames object to be adopted.
321     * @stable ICU 50
322     */
323    void adoptTimeZoneNames(TimeZoneNames *tznames);
324
325    /**
326     * Sets the time zone display name data to this format instnace.
327     * @param tznames TimeZoneNames object to be set.
328     * @stable ICU 50
329     */
330    void setTimeZoneNames(const TimeZoneNames &tznames);
331
332    /**
333     * Returns the localized GMT format pattern.
334     * @param pattern Receives the localized GMT format pattern.
335     * @return A reference to the result pattern.
336     * @see #setGMTPattern
337     * @stable ICU 50
338     */
339    UnicodeString& getGMTPattern(UnicodeString& pattern) const;
340
341    /**
342     * Sets the localized GMT format pattern. The pattern must contain
343     * a single argument {0}, for example "GMT {0}".
344     * @param pattern The localized GMT format pattern to be used by this object.
345     * @param status Recieves the status.
346     * @see #getGMTPattern
347     * @stable ICU 50
348     */
349    void setGMTPattern(const UnicodeString& pattern, UErrorCode& status);
350
351    /**
352     * Returns the offset pattern used for localized GMT format.
353     * @param type The offset pattern type enum.
354     * @param pattern Receives the offset pattern.
355     * @return A reference to the result pattern.
356     * @see #setGMTOffsetPattern
357     * @stable ICU 50
358     */
359    UnicodeString& getGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, UnicodeString& pattern) const;
360
361    /**
362     * Sets the offset pattern for the given offset type.
363     * @param type The offset pattern type enum.
364     * @param pattern The offset pattern used for localized GMT format for the type.
365     * @param status Receives the status.
366     * @see #getGMTOffsetPattern
367     * @stable ICU 50
368     */
369    void setGMTOffsetPattern(UTimeZoneFormatGMTOffsetPatternType type, const UnicodeString& pattern, UErrorCode& status);
370
371    /**
372     * Returns the decimal digit characters used for localized GMT format.
373     * The return string contains exactly 10 code points (may include Unicode
374     * supplementary character) representing digit 0 to digit 9 in the ascending
375     * order.
376     * @param digits Receives the decimal digits used for localized GMT format.
377     * @see #setGMTOffsetDigits
378     * @stable ICU 50
379     */
380    UnicodeString& getGMTOffsetDigits(UnicodeString& digits) const;
381
382    /**
383     * Sets the decimal digit characters used for localized GMT format.
384     * The input <code>digits</code> must contain exactly 10 code points
385     * (Unicode supplementary characters are also allowed) representing
386     * digit 0 to digit 9 in the ascending order. When the input <code>digits</code>
387     * does not satisfy the condition, <code>U_ILLEGAL_ARGUMENT_ERROR</code>
388     * will be set to the return status.
389     * @param digits The decimal digits used for localized GMT format.
390     * @param status Receives the status.
391     * @see #getGMTOffsetDigits
392     * @stable ICU 50
393     */
394    void setGMTOffsetDigits(const UnicodeString& digits, UErrorCode& status);
395
396    /**
397     * Returns the localized GMT format string for GMT(UTC) itself (GMT offset is 0).
398     * @param gmtZeroFormat Receives the localized GMT string string for GMT(UTC) itself.
399     * @return A reference to the result GMT string.
400     * @see #setGMTZeroFormat
401     * @stable ICU 50
402     */
403    UnicodeString& getGMTZeroFormat(UnicodeString& gmtZeroFormat) const;
404
405    /**
406     * Sets the localized GMT format string for GMT(UTC) itself (GMT offset is 0).
407     * @param gmtZeroFormat The localized GMT format string for GMT(UTC).
408     * @param status Receives the status.
409     * @see #getGMTZeroFormat
410     * @stable ICU 50
411     */
412    void setGMTZeroFormat(const UnicodeString& gmtZeroFormat, UErrorCode& status);
413
414    /**
415     * Returns the bitwise flags of UTimeZoneFormatParseOption representing the default parse
416     * options used by this object.
417     * @return the default parse options.
418     * @see ParseOption
419     * @stable ICU 50
420     */
421    uint32_t getDefaultParseOptions(void) const;
422
423    /**
424     * Sets the default parse options.
425     * <p><b>Note</b>: By default, an instance of <code>TimeZoneFormat</code>
426     * created by {@link #createInstance} has no parse options set (UTZFMT_PARSE_OPTION_NONE).
427     * To specify multipe options, use bitwise flags of UTimeZoneFormatParseOption.
428     * @see #UTimeZoneFormatParseOption
429     * @stable ICU 50
430     */
431    void setDefaultParseOptions(uint32_t flags);
432
433    /**
434     * Returns the ISO 8601 basic time zone string for the given offset.
435     * For example, "-08", "-0830" and "Z"
436     *
437     * @param offset the offset from GMT(UTC) in milliseconds.
438     * @param useUtcIndicator true if ISO 8601 UTC indicator "Z" is used when the offset is 0.
439     * @param isShort true if shortest form is used.
440     * @param ignoreSeconds true if non-zero offset seconds is appended.
441     * @param result Receives the ISO format string.
442     * @param status Receives the status
443     * @return the ISO 8601 basic format.
444     * @see #formatOffsetISO8601Extended
445     * @see #parseOffsetISO8601
446     * @stable ICU 51
447     */
448    UnicodeString& formatOffsetISO8601Basic(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
449        UnicodeString& result, UErrorCode& status) const;
450
451    /**
452     * Returns the ISO 8601 extended time zone string for the given offset.
453     * For example, "-08:00", "-08:30" and "Z"
454     *
455     * @param offset the offset from GMT(UTC) in milliseconds.
456     * @param useUtcIndicator true if ISO 8601 UTC indicator "Z" is used when the offset is 0.
457     * @param isShort true if shortest form is used.
458     * @param ignoreSeconds true if non-zero offset seconds is appended.
459     * @param result Receives the ISO format string.
460     * @param status Receives the status
461     * @return the ISO 8601 basic format.
462     * @see #formatOffsetISO8601Extended
463     * @see #parseOffsetISO8601
464     * @stable ICU 51
465     */
466    UnicodeString& formatOffsetISO8601Extended(int32_t offset, UBool useUtcIndicator, UBool isShort, UBool ignoreSeconds,
467        UnicodeString& result, UErrorCode& status) const;
468
469    /**
470     * Returns the localized GMT(UTC) offset format for the given offset.
471     * The localized GMT offset is defined by;
472     * <ul>
473     * <li>GMT format pattern (e.g. "GMT {0}" - see {@link #getGMTPattern})
474     * <li>Offset time pattern (e.g. "+HH:mm" - see {@link #getGMTOffsetPattern})
475     * <li>Offset digits (e.g. "0123456789" - see {@link #getGMTOffsetDigits})
476     * <li>GMT zero format (e.g. "GMT" - see {@link #getGMTZeroFormat})
477     * </ul>
478     * This format always uses 2 digit hours and minutes. When the given offset has non-zero
479     * seconds, 2 digit seconds field will be appended. For example,
480     * GMT+05:00 and GMT+05:28:06.
481     * @param offset the offset from GMT(UTC) in milliseconds.
482     * @param status Receives the status
483     * @param result Receives the localized GMT format string.
484     * @return A reference to the result.
485     * @see #parseOffsetLocalizedGMT
486     * @stable ICU 50
487     */
488    UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const;
489
490    /**
491     * Returns the short localized GMT(UTC) offset format for the given offset.
492     * The short localized GMT offset is defined by;
493     * <ul>
494     * <li>GMT format pattern (e.g. "GMT {0}" - see {@link #getGMTPattern})
495     * <li>Offset time pattern (e.g. "+HH:mm" - see {@link #getGMTOffsetPattern})
496     * <li>Offset digits (e.g. "0123456789" - see {@link #getGMTOffsetDigits})
497     * <li>GMT zero format (e.g. "GMT" - see {@link #getGMTZeroFormat})
498     * </ul>
499     * This format uses the shortest representation of offset. The hours field does not
500     * have leading zero and lower fields with zero will be truncated. For example,
501     * GMT+5 and GMT+530.
502     * @param offset the offset from GMT(UTC) in milliseconds.
503     * @param status Receives the status
504     * @param result Receives the short localized GMT format string.
505     * @return A reference to the result.
506     * @see #parseOffsetShortLocalizedGMT
507     * @stable ICU 51
508     */
509    UnicodeString& formatOffsetShortLocalizedGMT(int32_t offset, UnicodeString& result, UErrorCode& status) const;
510
511    using Format::format;
512
513    /**
514     * Returns the display name of the time zone at the given date for the style.
515     * @param style The style (e.g. <code>UTZFMT_STYLE_GENERIC_LONG</code>, <code>UTZFMT_STYLE_LOCALIZED_GMT</code>...)
516     * @param tz The time zone.
517     * @param date The date.
518     * @param name Receives the display name.
519     * @param timeType the output argument for receiving the time type (standard/daylight/unknown)
520     * used for the display name, or NULL if the information is not necessary.
521     * @return A reference to the result
522     * @see #UTimeZoneFormatStyle
523     * @see #UTimeZoneFormatTimeType
524     * @stable ICU 50
525     */
526    virtual UnicodeString& format(UTimeZoneFormatStyle style, const TimeZone& tz, UDate date,
527        UnicodeString& name, UTimeZoneFormatTimeType* timeType = NULL) const;
528
529    /**
530     * Returns offset from GMT(UTC) in milliseconds for the given ISO 8601
531     * style time zone string. When the given string is not an ISO 8601 time zone
532     * string, this method sets the current position as the error index
533     * to <code>ParsePosition pos</code> and returns 0.
534     * @param text The text contains ISO8601 style time zone string (e.g. "-08:00", "Z")
535     *              at the position.
536     * @param pos The ParsePosition object.
537     * @return The offset from GMT(UTC) in milliseconds for the given ISO 8601 style
538     *              time zone string.
539     * @see #formatOffsetISO8601Basic
540     * @see #formatOffsetISO8601Extended
541     * @stable ICU 50
542     */
543    int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos) const;
544
545    /**
546     * Returns offset from GMT(UTC) in milliseconds for the given localized GMT
547     * offset format string. When the given string cannot be parsed, this method
548     * sets the current position as the error index to <code>ParsePosition pos</code>
549     * and returns 0.
550     * @param text The text contains a localized GMT offset string at the position.
551     * @param pos The ParsePosition object.
552     * @return The offset from GMT(UTC) in milliseconds for the given localized GMT
553     *          offset format string.
554     * @see #formatOffsetLocalizedGMT
555     * @stable ICU 50
556     */
557    int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
558
559    /**
560     * Returns offset from GMT(UTC) in milliseconds for the given short localized GMT
561     * offset format string. When the given string cannot be parsed, this method
562     * sets the current position as the error index to <code>ParsePosition pos</code>
563     * and returns 0.
564     * @param text The text contains a short localized GMT offset string at the position.
565     * @param pos The ParsePosition object.
566     * @return The offset from GMT(UTC) in milliseconds for the given short localized GMT
567     *          offset format string.
568     * @see #formatOffsetShortLocalizedGMT
569     * @stable ICU 51
570     */
571    int32_t parseOffsetShortLocalizedGMT(const UnicodeString& text, ParsePosition& pos) const;
572
573    /**
574     * Returns a <code>TimeZone</code> by parsing the time zone string according to
575     * the given parse position, the specified format style and parse options.
576     *
577     * @param text The text contains a time zone string at the position.
578     * @param style The format style
579     * @param pos The position.
580     * @param parseOptions The parse options repesented by bitwise flags of UTimeZoneFormatParseOption.
581     * @param timeType The output argument for receiving the time type (standard/daylight/unknown),
582     * or NULL if the information is not necessary.
583     * @return A <code>TimeZone</code>, or null if the input could not be parsed.
584     * @see UTimeZoneFormatStyle
585     * @see UTimeZoneFormatParseOption
586     * @see UTimeZoneFormatTimeType
587     * @stable ICU 50
588     */
589    virtual TimeZone* parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
590        int32_t parseOptions, UTimeZoneFormatTimeType* timeType = NULL) const;
591
592    /**
593     * Returns a <code>TimeZone</code> by parsing the time zone string according to
594     * the given parse position, the specified format style and the default parse options.
595     *
596     * @param text The text contains a time zone string at the position.
597     * @param style The format style
598     * @param pos The position.
599     * @param timeType The output argument for receiving the time type (standard/daylight/unknown),
600     * or NULL if the information is not necessary.
601     * @return A <code>TimeZone</code>, or null if the input could not be parsed.
602     * @see UTimeZoneFormatStyle
603     * @see UTimeZoneFormatParseOption
604     * @see UTimeZoneFormatTimeType
605     * @stable ICU 50
606     */
607    TimeZone* parse(UTimeZoneFormatStyle style, const UnicodeString& text, ParsePosition& pos,
608        UTimeZoneFormatTimeType* timeType = NULL) const;
609
610    /* ----------------------------------------------
611     * Format APIs
612     * ---------------------------------------------- */
613
614    /**
615     * Format an object to produce a time zone display string using localized GMT offset format.
616     * This method handles Formattable objects with a <code>TimeZone</code>. If a the Formattable
617     * object type is not a <code>TimeZone</code>, then it returns a failing UErrorCode.
618     * @param obj The object to format. Must be a <code>TimeZone</code>.
619     * @param appendTo Output parameter to receive result. Result is appended to existing contents.
620     * @param pos On input: an alignment field, if desired. On output: the offsets of the alignment field.
621     * @param status Output param filled with success/failure status.
622     * @return Reference to 'appendTo' parameter.
623     * @stable ICU 50
624     */
625    virtual UnicodeString& format(const Formattable& obj, UnicodeString& appendTo,
626        FieldPosition& pos, UErrorCode& status) const;
627
628    /**
629     * Parse a string to produce an object. This methods handles parsing of
630     * time zone display strings into Formattable objects with <code>TimeZone</code>.
631     * @param source The string to be parsed into an object.
632     * @param result Formattable to be set to the parse result. If parse fails, return contents are undefined.
633     * @param parse_pos The position to start parsing at. Upon return this param is set to the position after the
634     *                  last character successfully parsed. If the source is not parsed successfully, this param
635     *                  will remain unchanged.
636     * @return A newly created Formattable* object, or NULL on failure.  The caller owns this and should
637     *                 delete it when done.
638     * @stable ICU 50
639     */
640    virtual void parseObject(const UnicodeString& source, Formattable& result, ParsePosition& parse_pos) const;
641
642    /**
643     * ICU "poor man's RTTI", returns a UClassID for this class.
644     * @stable ICU 50
645     */
646    static UClassID U_EXPORT2 getStaticClassID(void);
647
648    /**
649     * ICU "poor man's RTTI", returns a UClassID for the actual class.
650     * @stable ICU 50
651     */
652    virtual UClassID getDynamicClassID() const;
653
654protected:
655    /**
656     * Constructs a TimeZoneFormat object for the specified locale.
657     * @param locale the locale
658     * @param status receives the status.
659     * @stable ICU 50
660     */
661    TimeZoneFormat(const Locale& locale, UErrorCode& status);
662
663private:
664    /* Locale of this object */
665    Locale fLocale;
666
667    /* Stores the region (could be implicit default) */
668    char fTargetRegion[ULOC_COUNTRY_CAPACITY];
669
670    /* TimeZoneNames object used by this formatter */
671    TimeZoneNames* fTimeZoneNames;
672
673    /* TimeZoneGenericNames object used by this formatter - lazily instantiated */
674    TimeZoneGenericNames* fTimeZoneGenericNames;
675
676    /* Localized GMT format pattern - e.g. "GMT{0}" */
677    UnicodeString fGMTPattern;
678
679    /* Array of offset patterns used by Localized GMT format - e.g. "+HH:mm" */
680    UnicodeString fGMTOffsetPatterns[UTZFMT_PAT_COUNT];
681
682    /* Localized decimal digits used by Localized GMT format */
683    UChar32 fGMTOffsetDigits[10];
684
685    /* Localized GMT zero format - e.g. "GMT" */
686    UnicodeString fGMTZeroFormat;
687
688    /* Bit flags representing parse options */
689    uint32_t fDefParseOptionFlags;
690
691    /* Constant parts of GMT format pattern, populated from localized GMT format pattern*/
692    UnicodeString fGMTPatternPrefix;    /* Substring before {0} */
693    UnicodeString fGMTPatternSuffix;    /* Substring after {0} */
694
695    /* Compiled offset patterns generated from fGMTOffsetPatterns[] */
696    UVector* fGMTOffsetPatternItems[UTZFMT_PAT_COUNT];
697
698    UBool fAbuttingOffsetHoursAndMinutes;
699
700    /**
701     * Returns the time zone's specific format string.
702     * @param tz the time zone
703     * @param stdType the name type used for standard time
704     * @param dstType the name type used for daylight time
705     * @param date the date
706     * @param name receives the time zone's specific format name string
707     * @param timeType when null, actual time type is set
708     * @return a reference to name.
709     */
710    UnicodeString& formatSpecific(const TimeZone& tz, UTimeZoneNameType stdType, UTimeZoneNameType dstType,
711        UDate date, UnicodeString& name, UTimeZoneFormatTimeType *timeType) const;
712
713    /**
714     * Returns the time zone's generic format string.
715     * @param tz the time zone
716     * @param genType the generic name type
717     * @param date the date
718     * @param name receives the time zone's generic format name string
719     * @return a reference to name.
720     */
721    UnicodeString& formatGeneric(const TimeZone& tz, int32_t genType, UDate date, UnicodeString& name) const;
722
723    /**
724     * Lazily create a TimeZoneGenericNames instance
725     * @param status receives the status
726     * @return the cached TimeZoneGenericNames.
727     */
728    const TimeZoneGenericNames* getTimeZoneGenericNames(UErrorCode& status) const;
729
730    /**
731     * Private method returning the time zone's exemplar location string.
732     * This method will never return empty.
733     * @param tz the time zone
734     * @param name receives the time zone's exemplar location name
735     * @return a reference to name.
736     */
737    UnicodeString& formatExemplarLocation(const TimeZone& tz, UnicodeString& name) const;
738
739    /**
740     * Private enum specifying a combination of offset fields
741     */
742    enum OffsetFields {
743        FIELDS_H,
744        FIELDS_HM,
745        FIELDS_HMS
746    };
747
748    /**
749     * Parses the localized GMT pattern string and initialize
750     * localized gmt pattern fields.
751     * @param gmtPattern the localized GMT pattern string such as "GMT {0}"
752     * @param status U_ILLEGAL_ARGUMENT_ERROR is set when the specified pattern does not
753     *               contain an argument "{0}".
754     */
755    void initGMTPattern(const UnicodeString& gmtPattern, UErrorCode& status);
756
757    /**
758     * Parse the GMT offset pattern into runtime optimized format.
759     * @param pattern the offset pattern string
760     * @param required the required set of fields, such as FIELDS_HM
761     * @param status U_ILLEGAL_ARGUMENT is set when the specified pattern does not contain
762     *               pattern letters for the required fields.
763     * @return A list of GMTOffsetField objects, or NULL on error.
764     */
765    static UVector* parseOffsetPattern(const UnicodeString& pattern, OffsetFields required, UErrorCode& status);
766
767    /**
768     * Appends seconds field to the offset pattern with hour/minute
769     * Note: This code will be obsoleted once we add hour-minute-second pattern data in CLDR.
770     * @param offsetHM the offset pattern including hours and minutes fields
771     * @param result the output offset pattern including hour, minute and seconds fields
772     * @param status receives the status
773     * @return a reference to result
774     */
775    static UnicodeString& expandOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
776
777    /**
778     * Truncates minutes field to the offset pattern with hour/minute
779     * Note: This code will be obsoleted once we add hour pattern data in CLDR.
780     * @param offsetHM the offset pattern including hours and minutes fields
781     * @param result the output offset pattern including only hours field
782     * @param status receives the status
783     * @return a reference to result
784     */
785    static UnicodeString& truncateOffsetPattern(const UnicodeString& offsetHM, UnicodeString& result, UErrorCode& status);
786
787    /**
788     * Break input string into UChar32[]. Each array element represents
789     * a code point. This method is used for parsing localized digit
790     * characters and support characters in Unicode supplemental planes.
791     * @param str the string
792     * @param codeArray receives the result
793     * @param capacity the capacity of codeArray
794     * @return TRUE when the specified code array is fully filled with code points
795     *         (no under/overflow).
796     */
797    static UBool toCodePoints(const UnicodeString& str, UChar32* codeArray, int32_t capacity);
798
799    /**
800     * Private method supprting all of ISO8601 formats
801     * @param offset the offset from GMT(UTC) in milliseconds.
802     * @param useUtcIndicator true if ISO 8601 UTC indicator "Z" is used when the offset is 0.
803     * @param isShort true if shortest form is used.
804     * @param ignoreSeconds true if non-zero offset seconds is appended.
805     * @param result Receives the result
806     * @param status Receives the status
807     * @return the ISO 8601 basic format.
808     */
809    UnicodeString& formatOffsetISO8601(int32_t offset, UBool isBasic, UBool useUtcIndicator,
810        UBool isShort, UBool ignoreSeconds, UnicodeString& result, UErrorCode& status) const;
811
812    /**
813     * Private method used for localized GMT formatting.
814     * @param offset the zone's UTC offset
815     * @param isShort true if the short localized GMT format is desired.
816     * @param result receives the localized GMT format string
817     * @param status receives the status
818     */
819    UnicodeString& formatOffsetLocalizedGMT(int32_t offset, UBool isShort, UnicodeString& result, UErrorCode& status) const;
820
821    /**
822     * Returns offset from GMT(UTC) in milliseconds for the given ISO 8601 style
823     * (extended format) time zone string. When the given string is not an ISO 8601 time
824     * zone string, this method sets the current position as the error index
825     * to <code>ParsePosition pos</code> and returns 0.
826     * @param text the text contains ISO 8601 style time zone string (e.g. "-08:00", "Z")
827     *      at the position.
828     * @param pos the position, non-negative error index will be set on failure.
829     * @param extendedOnly TRUE if parsing the text as ISO 8601 extended offset format (e.g. "-08:00"),
830     *      or FALSE to evaluate the text as basic format.
831     * @param hasDigitOffset receiving if the parsed zone string contains offset digits.
832     * @return the offset from GMT(UTC) in milliseconds for the given ISO 8601 style
833     *      time zone string.
834     */
835    int32_t parseOffsetISO8601(const UnicodeString& text, ParsePosition& pos, UBool extendedOnly,
836        UBool* hasDigitOffset = NULL) const;
837
838    /**
839     * Appends localized digits to the buffer.
840     * This code assumes that the input number is 0 - 59
841     * @param buf the target buffer
842     * @param n the integer number
843     * @param minDigits the minimum digits width
844     */
845    void appendOffsetDigits(UnicodeString& buf, int32_t n, uint8_t minDigits) const;
846
847    /**
848     * Returns offset from GMT(UTC) in milliseconds for the given localized GMT
849     * offset format string. When the given string cannot be parsed, this method
850     * sets the current position as the error index to <code>ParsePosition pos</code>
851     * and returns 0.
852     * @param text the text contains a localized GMT offset string at the position.
853     * @param pos the position, non-negative error index will be set on failure.
854     * @param isShort true if this parser to try the short format first
855     * @param hasDigitOffset receiving if the parsed zone string contains offset digits.
856     * @return the offset from GMT(UTC) in milliseconds for the given localized GMT
857     *      offset format string.
858     */
859    int32_t parseOffsetLocalizedGMT(const UnicodeString& text, ParsePosition& pos,
860        UBool isShort, UBool* hasDigitOffset) const;
861
862    /**
863     * Parse localized GMT format generated by the patter used by this formatter, except
864     * GMT Zero format.
865     * @param text the input text
866     * @param start the start index
867     * @param isShort true if the short localized format is parsed.
868     * @param parsedLen receives the parsed length
869     * @return the parsed offset in milliseconds
870     */
871    int32_t parseOffsetLocalizedGMTPattern(const UnicodeString& text, int32_t start,
872        UBool isShort, int32_t& parsedLen) const;
873
874    /**
875     * Parses localized GMT offset fields into offset.
876     * @param text the input text
877     * @param start the start index
878     * @param isShort true if this is a short format - currently not used
879     * @param parsedLen the parsed length, or 0 on failure.
880     * @return the parsed offset in milliseconds.
881     */
882    int32_t parseOffsetFields(const UnicodeString& text, int32_t start, UBool isShort, int32_t& parsedLen) const;
883
884    /**
885     * Parse localized GMT offset fields with the given pattern.
886     * @param text the input text
887     * @param start the start index
888     * @param pattenItems the pattern (already itemized)
889     * @param forceSingleHourDigit true if hours field is parsed as a single digit
890     * @param hour receives the hour offset field
891     * @param min receives the minute offset field
892     * @param sec receives the second offset field
893     * @return the parsed length
894     */
895    int32_t parseOffsetFieldsWithPattern(const UnicodeString& text, int32_t start,
896        UVector* patternItems, UBool forceSingleHourDigit, int32_t& hour, int32_t& min, int32_t& sec) const;
897
898    /**
899     * Parses abutting localized GMT offset fields (such as 0800) into offset.
900     * @param text the input text
901     * @param start the start index
902     * @param parsedLen the parsed length, or 0 on failure
903     * @return the parsed offset in milliseconds.
904     */
905    int32_t parseAbuttingOffsetFields(const UnicodeString& text, int32_t start, int32_t& parsedLen) const;
906
907    /**
908     * Parses the input text using the default format patterns (e.g. "UTC{0}").
909     * @param text the input text
910     * @param start the start index
911     * @param parsedLen the parsed length, or 0 on failure
912     * @return the parsed offset in milliseconds.
913     */
914    int32_t parseOffsetDefaultLocalizedGMT(const UnicodeString& text, int start, int32_t& parsedLen) const;
915
916    /**
917     * Parses the input GMT offset fields with the default offset pattern.
918     * @param text the input text
919     * @param start the start index
920     * @param separator the separator character, e.g. ':'
921     * @param parsedLen the parsed length, or 0 on failure.
922     * @return the parsed offset in milliseconds.
923     */
924    int32_t parseDefaultOffsetFields(const UnicodeString& text, int32_t start, UChar separator,
925        int32_t& parsedLen) const;
926
927    /**
928     * Reads an offset field value. This method will stop parsing when
929     * 1) number of digits reaches <code>maxDigits</code>
930     * 2) just before already parsed number exceeds <code>maxVal</code>
931     *
932     * @param text the text
933     * @param start the start offset
934     * @param minDigits the minimum number of required digits
935     * @param maxDigits the maximum number of digits
936     * @param minVal the minimum value
937     * @param maxVal the maximum value
938     * @param parsedLen the actual parsed length.
939     * @return the integer value parsed
940     */
941    int32_t parseOffsetFieldWithLocalizedDigits(const UnicodeString& text, int32_t start,
942        uint8_t minDigits, uint8_t maxDigits, uint16_t minVal, uint16_t maxVal, int32_t& parsedLen) const;
943
944    /**
945     * Reads a single decimal digit, either localized digits used by this object
946     * or any Unicode numeric character.
947     * @param text the text
948     * @param start the start index
949     * @param len the actual length read from the text
950     * the start index is not a decimal number.
951     * @return the integer value of the parsed digit, or -1 on failure.
952     */
953    int32_t parseSingleLocalizedDigit(const UnicodeString& text, int32_t start, int32_t& len) const;
954
955    /**
956     * Formats offset using ASCII digits. The input offset range must be
957     * within +/-24 hours (exclusive).
958     * @param offset The offset
959     * @param sep The field separator character or 0 if not required
960     * @param minFields The minimum fields
961     * @param maxFields The maximum fields
962     * @return The offset string
963     */
964    static UnicodeString& formatOffsetWithAsciiDigits(int32_t offset, UChar sep,
965        OffsetFields minFields, OffsetFields maxFields, UnicodeString& result);
966
967    /**
968     * Parses offset represented by contiguous ASCII digits.
969     * <p>
970     * Note: This method expects the input position is already at the start of
971     * ASCII digits and does not parse sign (+/-).
972     * @param text The text contains a sequence of ASCII digits
973     * @param pos The parse position
974     * @param minFields The minimum Fields to be parsed
975     * @param maxFields The maximum Fields to be parsed
976     * @param fixedHourWidth true if hours field must be width of 2
977     * @return Parsed offset, 0 or positive number.
978     */
979    static int32_t parseAbuttingAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos,
980        OffsetFields minFields, OffsetFields maxFields, UBool fixedHourWidth);
981
982    /**
983     * Parses offset represented by ASCII digits and separators.
984     * <p>
985     * Note: This method expects the input position is already at the start of
986     * ASCII digits and does not parse sign (+/-).
987     * @param text The text
988     * @param pos The parse position
989     * @param sep The separator character
990     * @param minFields The minimum Fields to be parsed
991     * @param maxFields The maximum Fields to be parsed
992     * @return Parsed offset, 0 or positive number.
993     */
994    static int32_t parseAsciiOffsetFields(const UnicodeString& text, ParsePosition& pos, UChar sep,
995        OffsetFields minFields, OffsetFields maxFields);
996
997    /**
998     * Unquotes the message format style pattern.
999     * @param pattern the pattern
1000     * @param result receive the unquoted pattern.
1001     * @return A reference to result.
1002     */
1003    static UnicodeString& unquote(const UnicodeString& pattern, UnicodeString& result);
1004
1005    /**
1006     * Initialize localized GMT format offset hour/min/sec patterns.
1007     * This method parses patterns into optimized run-time format.
1008     * @param status receives the status.
1009     */
1010    void initGMTOffsetPatterns(UErrorCode& status);
1011
1012    /**
1013     * Check if there are any GMT format offset patterns without
1014     * any separators between hours field and minutes field and update
1015     * fAbuttingOffsetHoursAndMinutes field. This method must be called
1016     * after all patterns are parsed into pattern items.
1017     */
1018    void checkAbuttingHoursAndMinutes();
1019
1020    /**
1021     * Creates an instance of TimeZone for the given offset
1022     * @param offset the offset
1023     * @return A TimeZone with the given offset
1024     */
1025    TimeZone* createTimeZoneForOffset(int32_t offset) const;
1026
1027    /**
1028     * Returns the time type for the given name type
1029     * @param nameType the name type
1030     * @return the time type (unknown/standard/daylight)
1031     */
1032    static UTimeZoneFormatTimeType getTimeType(UTimeZoneNameType nameType);
1033
1034    /**
1035     * Returns the time zone ID of a match at the specified index within
1036     * the MatchInfoCollection.
1037     * @param matches the collection of matches
1038     * @param idx the index withing matches
1039     * @param tzID receives the resolved time zone ID
1040     * @return a reference to tzID.
1041     */
1042    UnicodeString& getTimeZoneID(const TimeZoneNames::MatchInfoCollection* matches, int32_t idx, UnicodeString& tzID) const;
1043
1044
1045    /**
1046     * Parse a zone ID.
1047     * @param text the text contains a time zone ID string at the position.
1048     * @param pos the position
1049     * @param tzID receives the zone ID
1050     * @return a reference to tzID
1051     */
1052    UnicodeString& parseZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
1053
1054    /**
1055     * Parse a short zone ID.
1056     * @param text the text contains a short time zone ID string at the position.
1057     * @param pos the position
1058     * @param tzID receives the short zone ID
1059     * @return a reference to tzID
1060     */
1061    UnicodeString& parseShortZoneID(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
1062
1063    /**
1064     * Parse an exemplar location string.
1065     * @param text the text contains an exemplar location string at the position.
1066     * @param pos the position.
1067     * @param tzID receives the time zone ID
1068     * @return a reference to tzID
1069     */
1070    UnicodeString& parseExemplarLocation(const UnicodeString& text, ParsePosition& pos, UnicodeString& tzID) const;
1071};
1072
1073U_NAMESPACE_END
1074
1075#endif /* !UCONFIG_NO_FORMATTING */
1076#endif
1077