Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:07

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *******************************************************************************
0005 * Copyright (C) 2007-2013, International Business Machines Corporation and    *
0006 * others. All Rights Reserved.                                                *
0007 *******************************************************************************
0008 */
0009 #ifndef RBTZ_H
0010 #define RBTZ_H
0011 
0012 #include "unicode/utypes.h"
0013 
0014 #if U_SHOW_CPLUSPLUS_API
0015 
0016 /**
0017  * \file 
0018  * \brief C++ API: Rule based customizable time zone
0019  */
0020 
0021 #if !UCONFIG_NO_FORMATTING
0022 
0023 #include "unicode/basictz.h"
0024 #include "unicode/unistr.h"
0025 
0026 U_NAMESPACE_BEGIN
0027 
0028 // forward declaration
0029 class UVector;
0030 struct Transition;
0031 
0032 /**
0033  * a BasicTimeZone subclass implemented in terms of InitialTimeZoneRule and TimeZoneRule instances
0034  * @see BasicTimeZone
0035  * @see InitialTimeZoneRule
0036  * @see TimeZoneRule
0037  */
0038 class U_I18N_API RuleBasedTimeZone : public BasicTimeZone {
0039 public:
0040     /**
0041      * Constructs a <code>RuleBasedTimeZone</code> object with the ID and the
0042      * <code>InitialTimeZoneRule</code>.  The input <code>InitialTimeZoneRule</code>
0043      * is adopted by this <code>RuleBasedTimeZone</code>, thus the caller must not
0044      * delete it.
0045      * @param id                The time zone ID.
0046      * @param initialRule       The initial time zone rule.
0047      * @stable ICU 3.8
0048      */
0049     RuleBasedTimeZone(const UnicodeString& id, InitialTimeZoneRule* initialRule);
0050 
0051     /**
0052      * Copy constructor.
0053      * @param source    The RuleBasedTimeZone object to be copied.
0054      * @stable ICU 3.8
0055      */
0056     RuleBasedTimeZone(const RuleBasedTimeZone& source);
0057 
0058     /**
0059      * Destructor.
0060      * @stable ICU 3.8
0061      */
0062     virtual ~RuleBasedTimeZone();
0063 
0064     /**
0065      * Assignment operator.
0066      * @param right The object to be copied.
0067      * @stable ICU 3.8
0068      */
0069     RuleBasedTimeZone& operator=(const RuleBasedTimeZone& right);
0070 
0071     /**
0072      * Return true if the given <code>TimeZone</code> objects are
0073      * semantically equal. Objects of different subclasses are considered unequal.
0074      * @param that  The object to be compared with.
0075      * @return  true if the given <code>TimeZone</code> objects are
0076       *semantically equal.
0077      * @stable ICU 3.8
0078      */
0079     virtual bool operator==(const TimeZone& that) const override;
0080 
0081     /**
0082      * Return true if the given <code>TimeZone</code> objects are
0083      * semantically unequal. Objects of different subclasses are considered unequal.
0084      * @param that  The object to be compared with.
0085      * @return  true if the given <code>TimeZone</code> objects are
0086      * semantically unequal.
0087      * @stable ICU 3.8
0088      */
0089     virtual bool operator!=(const TimeZone& that) const;
0090 
0091     /**
0092      * Adds the `TimeZoneRule` which represents time transitions.
0093      * The `TimeZoneRule` must have start times, that is, the result
0094      * of `isTransitionRule()` must be true. Otherwise, U_ILLEGAL_ARGUMENT_ERROR
0095      * is set to the error code.
0096      * The input `TimeZoneRule` is adopted by this `RuleBasedTimeZone`;
0097      * the caller must not delete it. Should an error condition prevent
0098      * the successful adoption of the rule, this function will delete it.
0099      *
0100      * After all rules are added, the caller must call `complete()` method to
0101      * make this `RuleBasedTimeZone` ready to handle common time
0102      * zone functions.
0103      * @param rule The `TimeZoneRule`.
0104      * @param status Output param to filled in with a success or an error.
0105      * @stable ICU 3.8
0106      */
0107     void addTransitionRule(TimeZoneRule* rule, UErrorCode& status);
0108 
0109     /**
0110      * Makes the <code>TimeZoneRule</code> ready to handle actual timezone
0111      * calculation APIs.  This method collects time zone rules specified
0112      * by the caller via the constructor and addTransitionRule() and
0113      * builds internal structure for making the object ready to support
0114      * time zone APIs such as getOffset(), getNextTransition() and others.
0115      * @param status Output param to filled in with a success or an error.
0116      * @stable ICU 3.8
0117      */
0118     void complete(UErrorCode& status);
0119 
0120     /**
0121      * Clones TimeZone objects polymorphically. Clients are responsible for deleting
0122      * the TimeZone object cloned.
0123      *
0124      * @return   A new copy of this TimeZone object.
0125      * @stable ICU 3.8
0126      */
0127     virtual RuleBasedTimeZone* clone() const override;
0128 
0129     /**
0130      * Returns the TimeZone's adjusted GMT offset (i.e., the number of milliseconds to add
0131      * to GMT to get local time in this time zone, taking daylight savings time into
0132      * account) as of a particular reference date.  The reference date is used to determine
0133      * whether daylight savings time is in effect and needs to be figured into the offset
0134      * that is returned (in other words, what is the adjusted GMT offset in this time zone
0135      * at this particular date and time?).  For the time zones produced by createTimeZone(),
0136      * the reference data is specified according to the Gregorian calendar, and the date
0137      * and time fields are local standard time.
0138      *
0139      * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
0140      * which returns both the raw and the DST offset for a given time. This method
0141      * is retained only for backward compatibility.
0142      *
0143      * @param era        The reference date's era
0144      * @param year       The reference date's year
0145      * @param month      The reference date's month (0-based; 0 is January)
0146      * @param day        The reference date's day-in-month (1-based)
0147      * @param dayOfWeek  The reference date's day-of-week (1-based; 1 is Sunday)
0148      * @param millis     The reference date's milliseconds in day, local standard time
0149      * @param status     Output param to filled in with a success or an error.
0150      * @return           The offset in milliseconds to add to GMT to get local time.
0151      * @stable ICU 3.8
0152      */
0153     virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
0154                               uint8_t dayOfWeek, int32_t millis, UErrorCode& status) const override;
0155 
0156     /**
0157      * Gets the time zone offset, for current date, modified in case of
0158      * daylight savings. This is the offset to add *to* UTC to get local time.
0159      *
0160      * <p>Note: Don't call this method. Instead, call the getOffset(UDate...) overload,
0161      * which returns both the raw and the DST offset for a given time. This method
0162      * is retained only for backward compatibility.
0163      *
0164      * @param era        The reference date's era
0165      * @param year       The reference date's year
0166      * @param month      The reference date's month (0-based; 0 is January)
0167      * @param day        The reference date's day-in-month (1-based)
0168      * @param dayOfWeek  The reference date's day-of-week (1-based; 1 is Sunday)
0169      * @param millis     The reference date's milliseconds in day, local standard time
0170      * @param monthLength The length of the given month in days.
0171      * @param status     Output param to filled in with a success or an error.
0172      * @return           The offset in milliseconds to add to GMT to get local time.
0173      * @stable ICU 3.8
0174      */
0175     virtual int32_t getOffset(uint8_t era, int32_t year, int32_t month, int32_t day,
0176                            uint8_t dayOfWeek, int32_t millis,
0177                            int32_t monthLength, UErrorCode& status) const override;
0178 
0179     /**
0180      * Returns the time zone raw and GMT offset for the given moment
0181      * in time.  Upon return, local-millis = GMT-millis + rawOffset +
0182      * dstOffset.  All computations are performed in the proleptic
0183      * Gregorian calendar.  The default implementation in the TimeZone
0184      * class delegates to the 8-argument getOffset().
0185      *
0186      * @param date moment in time for which to return offsets, in
0187      * units of milliseconds from January 1, 1970 0:00 GMT, either GMT
0188      * time or local wall time, depending on `local'.
0189      * @param local if true, `date' is local wall time; otherwise it
0190      * is in GMT time.
0191      * @param rawOffset output parameter to receive the raw offset, that
0192      * is, the offset not including DST adjustments
0193      * @param dstOffset output parameter to receive the DST offset,
0194      * that is, the offset to be added to `rawOffset' to obtain the
0195      * total offset between local and GMT time. If DST is not in
0196      * effect, this value is zero; otherwise it is a positive value,
0197      * typically one hour.
0198      * @param ec input-output error code
0199      * @stable ICU 3.8
0200      */
0201     virtual void getOffset(UDate date, UBool local, int32_t& rawOffset,
0202                            int32_t& dstOffset, UErrorCode& ec) const override;
0203 
0204     /**
0205      * Sets the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
0206      * to GMT to get local time, before taking daylight savings time into account).
0207      *
0208      * @param offsetMillis  The new raw GMT offset for this time zone.
0209      * @stable ICU 3.8
0210      */
0211     virtual void setRawOffset(int32_t offsetMillis) override;
0212 
0213     /**
0214      * Returns the TimeZone's raw GMT offset (i.e., the number of milliseconds to add
0215      * to GMT to get local time, before taking daylight savings time into account).
0216      *
0217      * @return   The TimeZone's raw GMT offset.
0218      * @stable ICU 3.8
0219      */
0220     virtual int32_t getRawOffset(void) const override;
0221 
0222     /**
0223      * Queries if this time zone uses daylight savings time.
0224      * @return true if this time zone uses daylight savings time,
0225      * false, otherwise.
0226      * @stable ICU 3.8
0227      */
0228     virtual UBool useDaylightTime(void) const override;
0229 
0230 #ifndef U_FORCE_HIDE_DEPRECATED_API
0231     /**
0232      * Queries if the given date is in daylight savings time in
0233      * this time zone.
0234      * This method is wasteful since it creates a new GregorianCalendar and
0235      * deletes it each time it is called. This is a deprecated method
0236      * and provided only for Java compatibility.
0237      *
0238      * @param date the given UDate.
0239      * @param status Output param filled in with success/error code.
0240      * @return true if the given date is in daylight savings time,
0241      * false, otherwise.
0242      * @deprecated ICU 2.4. Use Calendar::inDaylightTime() instead.
0243      */
0244     virtual UBool inDaylightTime(UDate date, UErrorCode& status) const override;
0245 #endif  // U_FORCE_HIDE_DEPRECATED_API
0246 
0247     /**
0248      * Returns true if this zone has the same rule and offset as another zone.
0249      * That is, if this zone differs only in ID, if at all.
0250      * @param other the <code>TimeZone</code> object to be compared with
0251      * @return true if the given zone is the same as this one,
0252      * with the possible exception of the ID
0253      * @stable ICU 3.8
0254      */
0255     virtual UBool hasSameRules(const TimeZone& other) const override;
0256 
0257     /**
0258      * Gets the first time zone transition after the base time.
0259      * @param base      The base time.
0260      * @param inclusive Whether the base time is inclusive or not.
0261      * @param result    Receives the first transition after the base time.
0262      * @return  true if the transition is found.
0263      * @stable ICU 3.8
0264      */
0265     virtual UBool getNextTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const override;
0266 
0267     /**
0268      * Gets the most recent time zone transition before the base time.
0269      * @param base      The base time.
0270      * @param inclusive Whether the base time is inclusive or not.
0271      * @param result    Receives the most recent transition before the base time.
0272      * @return  true if the transition is found.
0273      * @stable ICU 3.8
0274      */
0275     virtual UBool getPreviousTransition(UDate base, UBool inclusive, TimeZoneTransition& result) const override;
0276 
0277     /**
0278      * Returns the number of <code>TimeZoneRule</code>s which represents time transitions,
0279      * for this time zone, that is, all <code>TimeZoneRule</code>s for this time zone except
0280      * <code>InitialTimeZoneRule</code>.  The return value range is 0 or any positive value.
0281      * @param status    Receives error status code.
0282      * @return The number of <code>TimeZoneRule</code>s representing time transitions.
0283      * @stable ICU 3.8
0284      */
0285     virtual int32_t countTransitionRules(UErrorCode& status) const override;
0286 
0287     /**
0288      * Gets the <code>InitialTimeZoneRule</code> and the set of <code>TimeZoneRule</code>
0289      * which represent time transitions for this time zone.  On successful return,
0290      * the argument initial points to non-nullptr <code>InitialTimeZoneRule</code> and
0291      * the array trsrules is filled with 0 or multiple <code>TimeZoneRule</code>
0292      * instances up to the size specified by trscount.  The results are referencing the
0293      * rule instance held by this time zone instance.  Therefore, after this time zone
0294      * is destructed, they are no longer available.
0295      * @param initial       Receives the initial timezone rule
0296      * @param trsrules      Receives the timezone transition rules
0297      * @param trscount      On input, specify the size of the array 'transitions' receiving
0298      *                      the timezone transition rules.  On output, actual number of
0299      *                      rules filled in the array will be set.
0300      * @param status        Receives error status code.
0301      * @stable ICU 3.8
0302      */
0303     virtual void getTimeZoneRules(const InitialTimeZoneRule*& initial,
0304         const TimeZoneRule* trsrules[], int32_t& trscount, UErrorCode& status) const override;
0305 
0306     /**
0307      * Get time zone offsets from local wall time.
0308      * @stable ICU 69
0309      */
0310     virtual void getOffsetFromLocal(
0311         UDate date, UTimeZoneLocalOption nonExistingTimeOpt,
0312         UTimeZoneLocalOption duplicatedTimeOpt,
0313         int32_t& rawOffset, int32_t& dstOffset, UErrorCode& status) const override;
0314 
0315 private:
0316     void deleteRules(void);
0317     void deleteTransitions(void);
0318     UVector* copyRules(UVector* source);
0319     TimeZoneRule* findRuleInFinal(UDate date, UBool local,
0320         int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
0321     UBool findNext(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const;
0322     UBool findPrev(UDate base, UBool inclusive, UDate& time, TimeZoneRule*& from, TimeZoneRule*& to) const;
0323     int32_t getLocalDelta(int32_t rawBefore, int32_t dstBefore, int32_t rawAfter, int32_t dstAfter,
0324         int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
0325     UDate getTransitionTime(Transition* transition, UBool local,
0326         int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt) const;
0327     void getOffsetInternal(UDate date, UBool local, int32_t NonExistingTimeOpt, int32_t DuplicatedTimeOpt,
0328         int32_t& rawOffset, int32_t& dstOffset, UErrorCode& ec) const;
0329     void completeConst(UErrorCode &status) const;
0330 
0331     InitialTimeZoneRule *fInitialRule;
0332     UVector             *fHistoricRules;
0333     UVector             *fFinalRules;
0334     UVector             *fHistoricTransitions;
0335     UBool               fUpToDate;
0336 
0337 public:
0338     /**
0339      * Return the class ID for this class. This is useful only for comparing to
0340      * a return value from getDynamicClassID(). For example:
0341      * <pre>
0342      * .   Base* polymorphic_pointer = createPolymorphicObject();
0343      * .   if (polymorphic_pointer->getDynamicClassID() ==
0344      * .       erived::getStaticClassID()) ...
0345      * </pre>
0346      * @return          The class ID for all objects of this class.
0347      * @stable ICU 3.8
0348      */
0349     static UClassID U_EXPORT2 getStaticClassID(void);
0350 
0351     /**
0352      * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
0353      * method is to implement a simple version of RTTI, since not all C++
0354      * compilers support genuine RTTI. Polymorphic operator==() and clone()
0355      * methods call this method.
0356      *
0357      * @return          The class ID for this object. All objects of a
0358      *                  given class have the same class ID.  Objects of
0359      *                  other classes have different class IDs.
0360      * @stable ICU 3.8
0361      */
0362     virtual UClassID getDynamicClassID(void) const override;
0363 };
0364 
0365 U_NAMESPACE_END
0366 
0367 #endif /* #if !UCONFIG_NO_FORMATTING */
0368 
0369 #endif /* U_SHOW_CPLUSPLUS_API */
0370 
0371 #endif // RBTZ_H
0372 
0373 //eof