2 *******************************************************************************
3 * Copyright (C) 1997-2003, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 12/05/96 clhuang Creation.
13 * 04/21/97 aliu Fixed miscellaneous bugs found by inspection and
15 * 07/29/97 aliu Ported source bodies back from Java version with
16 * numerous feature enhancements and bug fixes.
17 * 08/10/98 stephen JDK 1.2 sync.
18 * 09/17/98 stephen Fixed getOffset() for last hour of year and DST
19 * 12/02/99 aliu Added TimeMode and constructor and setStart/EndRule
20 * methods that take TimeMode. Whitespace cleanup.
21 ********************************************************************************
24 #include "unicode/utypes.h"
26 #if !UCONFIG_NO_FORMATTING
28 #include "unicode/simpletz.h"
29 #include "unicode/gregocal.h"
33 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(SimpleTimeZone
)
35 // WARNING: assumes that no rule is measured from the end of February,
36 // since we don't handle leap years. Could handle assuming always
37 // Gregorian, since we know they didn't have daylight time when
38 // Gregorian calendar started.
39 const int8_t SimpleTimeZone::STATICMONTHLENGTH
[] = {31,29,31,30,31,30,31,31,30,31,30,31};
41 // *****************************************************************************
42 // class SimpleTimeZone
43 // *****************************************************************************
46 SimpleTimeZone::SimpleTimeZone(int32_t rawOffsetGMT
, const UnicodeString
& ID
)
52 startTimeMode(WALL_TIME
),
53 endTimeMode(WALL_TIME
),
59 rawOffset(rawOffsetGMT
),
63 dstSavings(U_MILLIS_PER_HOUR
)
67 // -------------------------------------
69 SimpleTimeZone::SimpleTimeZone(int32_t rawOffsetGMT
, const UnicodeString
& ID
,
70 int8_t savingsStartMonth
, int8_t savingsStartDay
,
71 int8_t savingsStartDayOfWeek
, int32_t savingsStartTime
,
72 int8_t savingsEndMonth
, int8_t savingsEndDay
,
73 int8_t savingsEndDayOfWeek
, int32_t savingsEndTime
,
77 construct(rawOffsetGMT
,
78 savingsStartMonth
, savingsStartDay
, savingsStartDayOfWeek
,
79 savingsStartTime
, WALL_TIME
,
80 savingsEndMonth
, savingsEndDay
, savingsEndDayOfWeek
,
81 savingsEndTime
, WALL_TIME
,
82 U_MILLIS_PER_HOUR
, status
);
85 // -------------------------------------
87 SimpleTimeZone::SimpleTimeZone(int32_t rawOffsetGMT
, const UnicodeString
& ID
,
88 int8_t savingsStartMonth
, int8_t savingsStartDay
,
89 int8_t savingsStartDayOfWeek
, int32_t savingsStartTime
,
90 int8_t savingsEndMonth
, int8_t savingsEndDay
,
91 int8_t savingsEndDayOfWeek
, int32_t savingsEndTime
,
92 int32_t savingsDST
, UErrorCode
& status
)
95 construct(rawOffsetGMT
,
96 savingsStartMonth
, savingsStartDay
, savingsStartDayOfWeek
,
97 savingsStartTime
, WALL_TIME
,
98 savingsEndMonth
, savingsEndDay
, savingsEndDayOfWeek
,
99 savingsEndTime
, WALL_TIME
,
103 // -------------------------------------
105 SimpleTimeZone::SimpleTimeZone(int32_t rawOffsetGMT
, const UnicodeString
& ID
,
106 int8_t savingsStartMonth
, int8_t savingsStartDay
,
107 int8_t savingsStartDayOfWeek
, int32_t savingsStartTime
,
108 TimeMode savingsStartTimeMode
,
109 int8_t savingsEndMonth
, int8_t savingsEndDay
,
110 int8_t savingsEndDayOfWeek
, int32_t savingsEndTime
,
111 TimeMode savingsEndTimeMode
,
112 int32_t savingsDST
, UErrorCode
& status
)
115 construct(rawOffsetGMT
,
116 savingsStartMonth
, savingsStartDay
, savingsStartDayOfWeek
,
117 savingsStartTime
, savingsStartTimeMode
,
118 savingsEndMonth
, savingsEndDay
, savingsEndDayOfWeek
,
119 savingsEndTime
, savingsEndTimeMode
,
124 * Internal construction method.
126 void SimpleTimeZone::construct(int32_t rawOffsetGMT
,
127 int8_t savingsStartMonth
,
128 int8_t savingsStartDay
,
129 int8_t savingsStartDayOfWeek
,
130 int32_t savingsStartTime
,
131 TimeMode savingsStartTimeMode
,
132 int8_t savingsEndMonth
,
133 int8_t savingsEndDay
,
134 int8_t savingsEndDayOfWeek
,
135 int32_t savingsEndTime
,
136 TimeMode savingsEndTimeMode
,
140 this->rawOffset
= rawOffsetGMT
;
141 this->startMonth
= savingsStartMonth
;
142 this->startDay
= savingsStartDay
;
143 this->startDayOfWeek
= savingsStartDayOfWeek
;
144 this->startTime
= savingsStartTime
;
145 this->startTimeMode
= savingsStartTimeMode
;
146 this->endMonth
= savingsEndMonth
;
147 this->endDay
= savingsEndDay
;
148 this->endDayOfWeek
= savingsEndDayOfWeek
;
149 this->endTime
= savingsEndTime
;
150 this->endTimeMode
= savingsEndTimeMode
;
151 this->dstSavings
= savingsDST
;
153 this->startMode
= DOM_MODE
;
154 this->endMode
= DOM_MODE
;
158 if (savingsDST
<= 0) {
159 status
= U_ILLEGAL_ARGUMENT_ERROR
;
163 // -------------------------------------
165 SimpleTimeZone::~SimpleTimeZone()
169 // -------------------------------------
171 // Called by TimeZone::createDefault(), then clone() inside a Mutex - be careful.
172 SimpleTimeZone::SimpleTimeZone(const SimpleTimeZone
&source
)
178 // -------------------------------------
180 // Called by TimeZone::createDefault(), then clone() inside a Mutex - be careful.
182 SimpleTimeZone::operator=(const SimpleTimeZone
&right
)
186 TimeZone::operator=(right
);
187 rawOffset
= right
.rawOffset
;
188 startMonth
= right
.startMonth
;
189 startDay
= right
.startDay
;
190 startDayOfWeek
= right
.startDayOfWeek
;
191 startTime
= right
.startTime
;
192 startTimeMode
= right
.startTimeMode
;
193 startMode
= right
.startMode
;
194 endMonth
= right
.endMonth
;
195 endDay
= right
.endDay
;
196 endDayOfWeek
= right
.endDayOfWeek
;
197 endTime
= right
.endTime
;
198 endTimeMode
= right
.endTimeMode
;
199 endMode
= right
.endMode
;
200 startYear
= right
.startYear
;
201 dstSavings
= right
.dstSavings
;
202 useDaylight
= right
.useDaylight
;
207 // -------------------------------------
210 SimpleTimeZone::operator==(const TimeZone
& that
) const
212 return ((this == &that
) ||
213 (getDynamicClassID() == that
.getDynamicClassID() &&
214 TimeZone::operator==(that
) &&
215 hasSameRules(that
)));
218 // -------------------------------------
220 // Called by TimeZone::createDefault() inside a Mutex - be careful.
222 SimpleTimeZone::clone() const
224 return new SimpleTimeZone(*this);
227 // -------------------------------------
230 * Sets the daylight savings starting year, that is, the year this time zone began
231 * observing its specified daylight savings time rules. The time zone is considered
232 * not to observe daylight savings time prior to that year; SimpleTimeZone doesn't
233 * support historical daylight-savings-time rules.
234 * @param year the daylight savings starting year.
237 SimpleTimeZone::setStartYear(int32_t year
)
242 // -------------------------------------
245 * Sets the daylight savings starting rule. For example, in the U.S., Daylight Savings
246 * Time starts at the first Sunday in April, at 2 AM in standard time.
247 * Therefore, you can set the start rule by calling:
248 * setStartRule(TimeFields.APRIL, 1, TimeFields.SUNDAY, 2*60*60*1000);
249 * The dayOfWeekInMonth and dayOfWeek parameters together specify how to calculate
250 * the exact starting date. Their exact meaning depend on their respective signs,
251 * allowing various types of rules to be constructed, as follows:<ul>
252 * <li>If both dayOfWeekInMonth and dayOfWeek are positive, they specify the
253 * day of week in the month (e.g., (2, WEDNESDAY) is the second Wednesday
255 * <li>If dayOfWeek is positive and dayOfWeekInMonth is negative, they specify
256 * the day of week in the month counting backward from the end of the month.
257 * (e.g., (-1, MONDAY) is the last Monday in the month)
258 * <li>If dayOfWeek is zero and dayOfWeekInMonth is positive, dayOfWeekInMonth
259 * specifies the day of the month, regardless of what day of the week it is.
260 * (e.g., (10, 0) is the tenth day of the month)
261 * <li>If dayOfWeek is zero and dayOfWeekInMonth is negative, dayOfWeekInMonth
262 * specifies the day of the month counting backward from the end of the
263 * month, regardless of what day of the week it is (e.g., (-2, 0) is the
264 * next-to-last day of the month).
265 * <li>If dayOfWeek is negative and dayOfWeekInMonth is positive, they specify the
266 * first specified day of the week on or after the specfied day of the month.
267 * (e.g., (15, -SUNDAY) is the first Sunday after the 15th of the month
268 * [or the 15th itself if the 15th is a Sunday].)
269 * <li>If dayOfWeek and DayOfWeekInMonth are both negative, they specify the
270 * last specified day of the week on or before the specified day of the month.
271 * (e.g., (-20, -TUESDAY) is the last Tuesday before the 20th of the month
272 * [or the 20th itself if the 20th is a Tuesday].)</ul>
273 * @param month the daylight savings starting month. Month is 0-based.
275 * @param dayOfWeekInMonth the daylight savings starting
276 * day-of-week-in-month. Please see the member description for an example.
277 * @param dayOfWeek the daylight savings starting day-of-week. Please see
278 * the member description for an example.
279 * @param time the daylight savings starting time. Please see the member
280 * description for an example.
284 SimpleTimeZone::setStartRule(int32_t month
, int32_t dayOfWeekInMonth
, int32_t dayOfWeek
,
285 int32_t time
, TimeMode mode
, UErrorCode
& status
)
287 startMonth
= (int8_t)month
;
288 startDay
= (int8_t)dayOfWeekInMonth
;
289 startDayOfWeek
= (int8_t)dayOfWeek
;
291 startTimeMode
= mode
;
292 decodeStartRule(status
);
295 // -------------------------------------
298 SimpleTimeZone::setStartRule(int32_t month
, int32_t dayOfMonth
,
299 int32_t time
, TimeMode mode
, UErrorCode
& status
)
301 setStartRule(month
, dayOfMonth
, 0, time
, mode
, status
);
304 // -------------------------------------
307 SimpleTimeZone::setStartRule(int32_t month
, int32_t dayOfMonth
, int32_t dayOfWeek
,
308 int32_t time
, TimeMode mode
, UBool after
, UErrorCode
& status
)
310 setStartRule(month
, after
? dayOfMonth
: -dayOfMonth
,
311 -dayOfWeek
, time
, mode
, status
);
314 // -------------------------------------
317 * Sets the daylight savings ending rule. For example, in the U.S., Daylight
318 * Savings Time ends at the last (-1) Sunday in October, at 2 AM in standard time.
319 * Therefore, you can set the end rule by calling:
320 * setEndRule(TimeFields.OCTOBER, -1, TimeFields.SUNDAY, 2*60*60*1000);
321 * Various other types of rules can be specified by manipulating the dayOfWeek
322 * and dayOfWeekInMonth parameters. For complete details, see the documentation
323 * for setStartRule().
324 * @param month the daylight savings ending month. Month is 0-based.
326 * @param dayOfWeekInMonth the daylight savings ending
327 * day-of-week-in-month. See setStartRule() for a complete explanation.
328 * @param dayOfWeek the daylight savings ending day-of-week. See setStartRule()
329 * for a complete explanation.
330 * @param time the daylight savings ending time. Please see the member
331 * description for an example.
335 SimpleTimeZone::setEndRule(int32_t month
, int32_t dayOfWeekInMonth
, int32_t dayOfWeek
,
336 int32_t time
, TimeMode mode
, UErrorCode
& status
)
338 endMonth
= (int8_t)month
;
339 endDay
= (int8_t)dayOfWeekInMonth
;
340 endDayOfWeek
= (int8_t)dayOfWeek
;
343 decodeEndRule(status
);
346 // -------------------------------------
349 SimpleTimeZone::setEndRule(int32_t month
, int32_t dayOfMonth
,
350 int32_t time
, TimeMode mode
, UErrorCode
& status
)
352 setEndRule(month
, dayOfMonth
, 0, time
, mode
, status
);
355 // -------------------------------------
358 SimpleTimeZone::setEndRule(int32_t month
, int32_t dayOfMonth
, int32_t dayOfWeek
,
359 int32_t time
, TimeMode mode
, UBool after
, UErrorCode
& status
)
361 setEndRule(month
, after
? dayOfMonth
: -dayOfMonth
,
362 -dayOfWeek
, time
, mode
, status
);
365 // -------------------------------------
368 SimpleTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
, int32_t day
,
369 uint8_t dayOfWeek
, int32_t millis
, UErrorCode
& status
) const
371 // Check the month before indexing into STATICMONTHLENGTH. This
372 // duplicates the test that occurs in the 7-argument getOffset(),
373 // however, this is unavoidable. We don't mind because this method, in
374 // fact, should not be called; internal code should always call the
375 // 7-argument getOffset(), and outside code should use Calendar.get(int
376 // field) with fields ZONE_OFFSET and DST_OFFSET. We can't get rid of
377 // this method because it's public API. - liu 8/10/98
378 if(month
< UCAL_JANUARY
|| month
> UCAL_DECEMBER
) {
379 status
= U_ILLEGAL_ARGUMENT_ERROR
;
383 return getOffset(era
, year
, month
, day
, dayOfWeek
, millis
, STATICMONTHLENGTH
[month
], status
);
387 SimpleTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
, int32_t day
,
388 uint8_t dayOfWeek
, int32_t millis
,
389 int32_t monthLength
, UErrorCode
& status
) const {
390 // Check the month before indexing into STATICMONTHLENGTH. This
391 // duplicates a test that occurs in the 9-argument getOffset(),
392 // however, this is unavoidable. We don't mind because this method, in
393 // fact, should not be called; internal code should always call the
394 // 9-argument getOffset(), and outside code should use Calendar.get(int
395 // field) with fields ZONE_OFFSET and DST_OFFSET. We can't get rid of
396 // this method because it's public API. - liu 8/10/98
397 if (month
< UCAL_JANUARY
398 || month
> UCAL_DECEMBER
) {
399 status
= U_ILLEGAL_ARGUMENT_ERROR
;
403 // TODO FIX We don't handle leap years yet!
404 int32_t prevMonthLength
= (month
>= 1) ? STATICMONTHLENGTH
[month
- 1] : 31;
406 return getOffset(era
, year
, month
, day
, dayOfWeek
, millis
,
407 monthLength
, prevMonthLength
, status
);
411 SimpleTimeZone::getOffset(uint8_t era
, int32_t year
, int32_t month
, int32_t day
,
412 uint8_t dayOfWeek
, int32_t millis
,
413 int32_t monthLength
, int32_t prevMonthLength
,
414 UErrorCode
& status
) const
416 if(U_FAILURE(status
)) return 0;
418 if ((era
!= GregorianCalendar::AD
&& era
!= GregorianCalendar::BC
)
419 || month
< UCAL_JANUARY
420 || month
> UCAL_DECEMBER
423 || dayOfWeek
< UCAL_SUNDAY
424 || dayOfWeek
> UCAL_SATURDAY
426 || millis
>= U_MILLIS_PER_DAY
429 || prevMonthLength
< 28
430 || prevMonthLength
> 31) {
431 status
= U_ILLEGAL_ARGUMENT_ERROR
;
435 int32_t result
= rawOffset
;
437 // Bail out if we are before the onset of daylight savings time
438 if(!useDaylight
|| year
< startYear
|| era
!= GregorianCalendar::AD
)
441 // Check for southern hemisphere. We assume that the start and end
442 // month are different.
443 UBool southern
= (startMonth
> endMonth
);
445 // Compare the date to the starting and ending rules.+1 = date>rule, -1
446 // = date<rule, 0 = date==rule.
447 int32_t startCompare
= compareToRule((int8_t)month
, (int8_t)monthLength
, (int8_t)prevMonthLength
,
448 (int8_t)day
, (int8_t)dayOfWeek
, millis
,
449 startTimeMode
== UTC_TIME
? -rawOffset
: 0,
450 startMode
, (int8_t)startMonth
, (int8_t)startDayOfWeek
,
451 (int8_t)startDay
, startTime
);
452 int32_t endCompare
= 0;
454 /* We don't always have to compute endCompare. For many instances,
455 * startCompare is enough to determine if we are in DST or not. In the
456 * northern hemisphere, if we are before the start rule, we can't have
457 * DST. In the southern hemisphere, if we are after the start rule, we
458 * must have DST. This is reflected in the way the next if statement
459 * (not the one immediately following) short circuits. */
460 if(southern
!= (startCompare
>= 0)) {
461 endCompare
= compareToRule((int8_t)month
, (int8_t)monthLength
, (int8_t)prevMonthLength
,
462 (int8_t)day
, (int8_t)dayOfWeek
, millis
,
463 endTimeMode
== WALL_TIME
? dstSavings
:
464 (endTimeMode
== UTC_TIME
? -rawOffset
: 0),
465 endMode
, (int8_t)endMonth
, (int8_t)endDayOfWeek
,
466 (int8_t)endDay
, endTime
);
469 // Check for both the northern and southern hemisphere cases. We
470 // assume that in the northern hemisphere, the start rule is before the
471 // end rule within the calendar year, and vice versa for the southern
473 if ((!southern
&& (startCompare
>= 0 && endCompare
< 0)) ||
474 (southern
&& (startCompare
>= 0 || endCompare
< 0)))
475 result
+= dstSavings
;
480 // -------------------------------------
483 * Compare a given date in the year to a rule. Return 1, 0, or -1, depending
484 * on whether the date is after, equal to, or before the rule date. The
485 * millis are compared directly against the ruleMillis, so any
486 * standard-daylight adjustments must be handled by the caller.
488 * @return 1 if the date is after the rule date, -1 if the date is before
489 * the rule date, or 0 if the date is equal to the rule date.
492 SimpleTimeZone::compareToRule(int8_t month
, int8_t monthLen
, int8_t prevMonthLen
,
494 int8_t dayOfWeek
, int32_t millis
, int32_t millisDelta
,
495 EMode ruleMode
, int8_t ruleMonth
, int8_t ruleDayOfWeek
,
496 int8_t ruleDay
, int32_t ruleMillis
)
498 // Make adjustments for startTimeMode and endTimeMode
499 millis
+= millisDelta
;
500 while (millis
>= U_MILLIS_PER_DAY
) {
501 millis
-= U_MILLIS_PER_DAY
;
503 dayOfWeek
= (int8_t)(1 + (dayOfWeek
% 7)); // dayOfWeek is one-based
504 if (dayOfMonth
> monthLen
) {
506 /* When incrementing the month, it is desirible to overflow
507 * from DECEMBER to DECEMBER+1, since we use the result to
508 * compare against a real month. Wraparound of the value
509 * leads to bug 4173604. */
514 millis
+= U_MILLIS_PER_DAY
;
516 dayOfWeek
= (int8_t)(1 + ((dayOfWeek
+5) % 7)); // dayOfWeek is one-based
517 if (dayOfMonth
< 1) {
518 dayOfMonth
= prevMonthLen
;
523 // first compare months. If they're different, we don't have to worry about days
525 if (month
< ruleMonth
) return -1;
526 else if (month
> ruleMonth
) return 1;
528 // calculate the actual day of month for the rule
529 int32_t ruleDayOfMonth
= 0;
532 // if the mode is day-of-month, the day of month is given
534 ruleDayOfMonth
= ruleDay
;
537 // if the mode is day-of-week-in-month, calculate the day-of-month from it
538 case DOW_IN_MONTH_MODE
:
539 // In this case ruleDay is the day-of-week-in-month (this code is using
540 // the dayOfWeek and dayOfMonth parameters to figure out the day-of-week
541 // of the first day of the month, so it's trusting that they're really
542 // consistent with each other)
544 ruleDayOfMonth
= 1 + (ruleDay
- 1) * 7 +
545 (7 + ruleDayOfWeek
- (dayOfWeek
- dayOfMonth
+ 1)) % 7;
547 // if ruleDay is negative (we assume it's not zero here), we have to do
548 // the same calculation figuring backward from the last day of the month.
549 // (STATICMONTHLENGTH gives us that last day. We don't take leap years
550 // into account, so this may not work right for February.)
553 // (again, this code is trusting that dayOfMonth and dayOfMonth are
554 // consistent with each other here, since we're using them to figure
555 // the day of week of the first of the month)
556 ruleDayOfMonth
= monthLen
+ (ruleDay
+ 1) * 7 -
557 (7 + (dayOfWeek
+ monthLen
- dayOfMonth
) - ruleDayOfWeek
) % 7;
561 case DOW_GE_DOM_MODE
:
562 ruleDayOfMonth
= ruleDay
+
563 (49 + ruleDayOfWeek
- ruleDay
- dayOfWeek
+ dayOfMonth
) % 7;
566 case DOW_LE_DOM_MODE
:
567 ruleDayOfMonth
= ruleDay
-
568 (49 - ruleDayOfWeek
+ ruleDay
+ dayOfWeek
- dayOfMonth
) % 7;
569 // Note at this point ruleDayOfMonth may be <1, although it will
570 // be >=1 for well-formed rules.
574 // now that we have a real day-in-month for the rule, we can compare days...
575 if (dayOfMonth
< ruleDayOfMonth
) return -1;
576 else if (dayOfMonth
> ruleDayOfMonth
) return 1;
578 // ...and if they're equal, we compare times
579 if (millis
< ruleMillis
) return -1;
580 else if (millis
> ruleMillis
) return 1;
584 // -------------------------------------
587 SimpleTimeZone::getRawOffset() const
592 // -------------------------------------
595 SimpleTimeZone::setRawOffset(int32_t offsetMillis
)
597 rawOffset
= offsetMillis
;
600 // -------------------------------------
603 SimpleTimeZone::setDSTSavings(int32_t millisSavedDuringDST
, UErrorCode
& status
)
605 if (millisSavedDuringDST
<= 0) {
606 status
= U_ILLEGAL_ARGUMENT_ERROR
;
609 dstSavings
= millisSavedDuringDST
;
613 // -------------------------------------
616 SimpleTimeZone::getDSTSavings() const
621 // -------------------------------------
624 SimpleTimeZone::useDaylightTime() const
629 // -------------------------------------
633 * Queries if the given date is in Daylight Savings Time.
635 UBool
SimpleTimeZone::inDaylightTime(UDate date
, UErrorCode
& status
) const
637 // This method is wasteful since it creates a new GregorianCalendar and
638 // deletes it each time it is called. However, this is a deprecated method
639 // and provided only for Java compatibility as of 8/6/97 [LIU].
640 if (U_FAILURE(status
)) return FALSE
;
641 GregorianCalendar
*gc
= new GregorianCalendar(*this, status
);
644 status
= U_MEMORY_ALLOCATION_ERROR
;
647 gc
->setTime(date
, status
);
648 UBool result
= gc
->inDaylightTime(status
);
653 // -------------------------------------
656 * Return true if this zone has the same rules and offset as another zone.
657 * @param other the TimeZone object to be compared with
658 * @return true if the given zone has the same rules and offset as this one
661 SimpleTimeZone::hasSameRules(const TimeZone
& other
) const
663 if (this == &other
) return TRUE
;
664 if (other
.getDynamicClassID() != SimpleTimeZone::getStaticClassID()) return FALSE
;
665 SimpleTimeZone
*that
= (SimpleTimeZone
*)&other
;
666 return rawOffset
== that
->rawOffset
&&
667 useDaylight
== that
->useDaylight
&&
669 // Only check rules if using DST
670 || (dstSavings
== that
->dstSavings
&&
671 startMode
== that
->startMode
&&
672 startMonth
== that
->startMonth
&&
673 startDay
== that
->startDay
&&
674 startDayOfWeek
== that
->startDayOfWeek
&&
675 startTime
== that
->startTime
&&
676 startTimeMode
== that
->startTimeMode
&&
677 endMode
== that
->endMode
&&
678 endMonth
== that
->endMonth
&&
679 endDay
== that
->endDay
&&
680 endDayOfWeek
== that
->endDayOfWeek
&&
681 endTime
== that
->endTime
&&
682 endTimeMode
== that
->endTimeMode
&&
683 startYear
== that
->startYear
));
686 // -------------------------------------
688 //----------------------------------------------------------------------
689 // Rule representation
691 // We represent the following flavors of rules:
692 // 5 the fifth of the month
693 // lastSun the last Sunday in the month
694 // lastMon the last Monday in the month
695 // Sun>=8 first Sunday on or after the eighth
696 // Sun<=25 last Sunday on or before the 25th
697 // This is further complicated by the fact that we need to remain
698 // backward compatible with the 1.1 FCS. Finally, we need to minimize
699 // API changes. In order to satisfy these requirements, we support
700 // three representation systems, and we translate between them.
702 // INTERNAL REPRESENTATION
703 // This is the format SimpleTimeZone objects take after construction or
704 // streaming in is complete. Rules are represented directly, using an
705 // unencoded format. We will discuss the start rule only below; the end
706 // rule is analogous.
707 // startMode Takes on enumerated values DAY_OF_MONTH,
708 // DOW_IN_MONTH, DOW_AFTER_DOM, or DOW_BEFORE_DOM.
709 // startDay The day of the month, or for DOW_IN_MONTH mode, a
710 // value indicating which DOW, such as +1 for first,
711 // +2 for second, -1 for last, etc.
712 // startDayOfWeek The day of the week. Ignored for DAY_OF_MONTH.
714 // ENCODED REPRESENTATION
715 // This is the format accepted by the constructor and by setStartRule()
716 // and setEndRule(). It uses various combinations of positive, negative,
717 // and zero values to encode the different rules. This representation
718 // allows us to specify all the different rule flavors without altering
720 // MODE startMonth startDay startDayOfWeek
721 // DOW_IN_MONTH_MODE >=0 !=0 >0
722 // DOM_MODE >=0 >0 ==0
723 // DOW_GE_DOM_MODE >=0 >0 <0
724 // DOW_LE_DOM_MODE >=0 <0 <0
725 // (no DST) don't care ==0 don't care
727 // STREAMED REPRESENTATION
728 // We must retain binary compatibility with the 1.1 FCS. The 1.1 code only
729 // handles DOW_IN_MONTH_MODE and non-DST mode, the latter indicated by the
730 // flag useDaylight. When we stream an object out, we translate into an
731 // approximate DOW_IN_MONTH_MODE representation so the object can be parsed
732 // and used by 1.1 code. Following that, we write out the full
733 // representation separately so that contemporary code can recognize and
734 // parse it. The full representation is written in a "packed" format,
735 // consisting of a version number, a length, and an array of bytes. Future
736 // versions of this class may specify different versions. If they wish to
737 // include additional data, they should do so by storing them after the
738 // packed representation below.
739 //----------------------------------------------------------------------
742 * Given a set of encoded rules in startDay and startDayOfMonth, decode
743 * them and set the startMode appropriately. Do the same for endDay and
744 * endDayOfMonth. Upon entry, the day of week variables may be zero or
745 * negative, in order to indicate special modes. The day of month
746 * variables may also be negative. Upon exit, the mode variables will be
747 * set, and the day of week and day of month variables will be positive.
748 * This method also recognizes a startDay or endDay of zero as indicating
752 SimpleTimeZone::decodeRules(UErrorCode
& status
)
754 decodeStartRule(status
);
755 decodeEndRule(status
);
759 * Decode the start rule and validate the parameters. The parameters are
760 * expected to be in encoded form, which represents the various rule modes
761 * by negating or zeroing certain values. Representation formats are:
764 * DOW_IN_MONTH DOM DOW>=DOM DOW<=DOM no DST
765 * ------------ ----- -------- -------- ----------
766 * month 0..11 same same same don't care
767 * day -5..5 1..31 1..31 -1..-31 0
768 * dayOfWeek 1..7 0 -1..-7 -1..-7 don't care
769 * time 0..ONEDAY same same same don't care
771 * The range for month does not include UNDECIMBER since this class is
772 * really specific to GregorianCalendar, which does not use that month.
773 * The range for time includes ONEDAY (vs. ending at ONEDAY-1) because the
774 * end rule is an exclusive limit point. That is, the range of times that
775 * are in DST include those >= the start and < the end. For this reason,
776 * it should be possible to specify an end of ONEDAY in order to include the
777 * entire day. Although this is equivalent to time 0 of the following day,
778 * it's not always possible to specify that, for example, on December 31.
779 * While arguably the start range should still be 0..ONEDAY-1, we keep
780 * the start and end ranges the same for consistency.
783 SimpleTimeZone::decodeStartRule(UErrorCode
& status
)
785 if(U_FAILURE(status
)) return;
787 useDaylight
= (UBool
)((startDay
!= 0) && (endDay
!= 0) ? TRUE
: FALSE
);
788 if (useDaylight
&& dstSavings
== 0) {
789 dstSavings
= U_MILLIS_PER_HOUR
;
792 if (startMonth
< UCAL_JANUARY
|| startMonth
> UCAL_DECEMBER
) {
793 status
= U_ILLEGAL_ARGUMENT_ERROR
;
796 if (startTime
< 0 || startTime
> U_MILLIS_PER_DAY
||
797 startTimeMode
< WALL_TIME
|| startTimeMode
> UTC_TIME
) {
798 status
= U_ILLEGAL_ARGUMENT_ERROR
;
801 if (startDayOfWeek
== 0) {
802 startMode
= DOM_MODE
;
804 if (startDayOfWeek
> 0) {
805 startMode
= DOW_IN_MONTH_MODE
;
807 startDayOfWeek
= (int8_t)-startDayOfWeek
;
809 startMode
= DOW_GE_DOM_MODE
;
811 startDay
= (int8_t)-startDay
;
812 startMode
= DOW_LE_DOM_MODE
;
815 if (startDayOfWeek
> UCAL_SATURDAY
) {
816 status
= U_ILLEGAL_ARGUMENT_ERROR
;
820 if (startMode
== DOW_IN_MONTH_MODE
) {
821 if (startDay
< -5 || startDay
> 5) {
822 status
= U_ILLEGAL_ARGUMENT_ERROR
;
825 } else if (startDay
> STATICMONTHLENGTH
[startMonth
]) {
826 status
= U_ILLEGAL_ARGUMENT_ERROR
;
833 * Decode the end rule and validate the parameters. This method is exactly
834 * analogous to decodeStartRule().
835 * @see decodeStartRule
838 SimpleTimeZone::decodeEndRule(UErrorCode
& status
)
840 if(U_FAILURE(status
)) return;
842 useDaylight
= (UBool
)((startDay
!= 0) && (endDay
!= 0) ? TRUE
: FALSE
);
843 if (useDaylight
&& dstSavings
== 0) {
844 dstSavings
= U_MILLIS_PER_HOUR
;
847 if (endMonth
< UCAL_JANUARY
|| endMonth
> UCAL_DECEMBER
) {
848 status
= U_ILLEGAL_ARGUMENT_ERROR
;
851 if (endTime
< 0 || endTime
> U_MILLIS_PER_DAY
||
852 endTimeMode
< WALL_TIME
|| endTimeMode
> UTC_TIME
) {
853 status
= U_ILLEGAL_ARGUMENT_ERROR
;
856 if (endDayOfWeek
== 0) {
859 if (endDayOfWeek
> 0) {
860 endMode
= DOW_IN_MONTH_MODE
;
862 endDayOfWeek
= (int8_t)-endDayOfWeek
;
864 endMode
= DOW_GE_DOM_MODE
;
866 endDay
= (int8_t)-endDay
;
867 endMode
= DOW_LE_DOM_MODE
;
870 if (endDayOfWeek
> UCAL_SATURDAY
) {
871 status
= U_ILLEGAL_ARGUMENT_ERROR
;
875 if (endMode
== DOW_IN_MONTH_MODE
) {
876 if (endDay
< -5 || endDay
> 5) {
877 status
= U_ILLEGAL_ARGUMENT_ERROR
;
880 } else if (endDay
> STATICMONTHLENGTH
[endMonth
]) {
881 status
= U_ILLEGAL_ARGUMENT_ERROR
;
889 #endif /* #if !UCONFIG_NO_FORMATTING */