2 *******************************************************************************
\r
3 * Copyright (C) 2007-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.util;
\r
8 import java.util.Arrays;
\r
9 import java.util.Date;
\r
12 * <code>TimeArrayTimeZoneRule</code> represents a time zone rule whose start times are
\r
13 * defined by an array of milliseconds since the standard base time.
\r
17 public class TimeArrayTimeZoneRule extends TimeZoneRule {
\r
19 private static final long serialVersionUID = -1117109130077415245L;
\r
21 private final long[] startTimes;
\r
22 private final int timeType;
\r
25 * Constructs a <code>TimeArrayTimeZoneRule</code> with the name, the GMT offset of its
\r
26 * standard time, the amount of daylight saving offset adjustment and
\r
27 * the array of times when this rule takes effect.
\r
29 * @param name The time zone name.
\r
30 * @param rawOffset The UTC offset of its standard time in milliseconds.
\r
31 * @param dstSavings The amount of daylight saving offset adjustment in
\r
32 * milliseconds. If this ia a rule for standard time,
\r
33 * the value of this argument is 0.
\r
34 * @param startTimes The start times in milliseconds since the base time
\r
35 * (January 1, 1970, 00:00:00).
\r
36 * @param timeType The time type of the start times, which is one of
\r
37 * <code>DataTimeRule.WALL_TIME</code>, <code>STANDARD_TIME</code>
\r
38 * and <code>UTC_TIME</code>.
\r
42 public TimeArrayTimeZoneRule(String name, int rawOffset, int dstSavings, long[] startTimes, int timeType) {
\r
43 super(name, rawOffset, dstSavings);
\r
44 if (startTimes == null || startTimes.length == 0) {
\r
45 throw new IllegalArgumentException("No start times are specified.");
\r
47 this.startTimes = (long[])startTimes.clone();
\r
48 Arrays.sort(this.startTimes);
\r
50 this.timeType = timeType;
\r
54 * Gets the array of start times used by this rule.
\r
56 * @return An array of the start times in milliseconds since the base time
\r
57 * (January 1, 1970, 00:00:00 GMT).
\r
60 public long[] getStartTimes() {
\r
61 return (long[])startTimes.clone();
\r
65 * Gets the time type of the start times used by this rule. The return value
\r
66 * is either <code>DateTimeRule.WALL_TIME</code> or <code>DateTimeRule.STANDARD_TIME</code>
\r
67 * or <code>DateTimeRule.UTC_TIME</code>.
\r
69 * @return The time type used of the start times used by this rule.
\r
72 public int getTimeType() {
\r
80 public Date getFirstStart(int prevRawOffset, int prevDSTSavings) {
\r
81 return new Date(getUTC(startTimes[0], prevRawOffset, prevDSTSavings));
\r
88 public Date getFinalStart(int prevRawOffset, int prevDSTSavings) {
\r
89 return new Date(getUTC(startTimes[startTimes.length - 1], prevRawOffset, prevDSTSavings));
\r
96 public Date getNextStart(long base, int prevOffset, int prevDSTSavings, boolean inclusive) {
\r
97 int i = startTimes.length - 1;
\r
98 for (; i >= 0; i--) {
\r
99 long time = getUTC(startTimes[i], prevOffset, prevDSTSavings);
\r
100 if (time < base || (!inclusive && time == base)) {
\r
104 if (i == startTimes.length - 1) {
\r
107 return new Date(getUTC(startTimes[i + 1], prevOffset, prevDSTSavings));
\r
114 public Date getPreviousStart(long base, int prevOffset, int prevDSTSavings, boolean inclusive) {
\r
115 int i = startTimes.length - 1;
\r
116 for (; i >= 0; i--) {
\r
117 long time = getUTC(startTimes[i], prevOffset, prevDSTSavings);
\r
118 if (time < base || (inclusive && time == base)) {
\r
119 return new Date(time);
\r
129 public boolean isEquivalentTo(TimeZoneRule other) {
\r
130 if (!(other instanceof TimeArrayTimeZoneRule)) {
\r
133 if (timeType == ((TimeArrayTimeZoneRule)other).timeType
\r
134 && Arrays.equals(startTimes, ((TimeArrayTimeZoneRule)other).startTimes)) {
\r
135 return super.isEquivalentTo(other);
\r
141 * {@inheritDoc}<br><br>
\r
142 * Note: This method in <code>TimeArrayTimeZoneRule</code> always returns true.
\r
145 public boolean isTransitionRule() {
\r
149 /* Get UTC of the time with the raw/dst offset */
\r
150 private long getUTC(long time, int raw, int dst) {
\r
151 if (timeType != DateTimeRule.UTC_TIME) {
\r
154 if (timeType == DateTimeRule.WALL_TIME) {
\r
161 * Returns a <code>String</code> representation of this <code>TimeArrayTimeZoneRule</code> object.
\r
162 * This method is used for debugging purpose only. The string representation can be changed
\r
163 * in future version of ICU without any notice.
\r
167 public String toString() {
\r
168 StringBuffer buf = new StringBuffer();
\r
169 buf.append(super.toString());
\r
170 buf.append(", timeType=");
\r
171 buf.append(timeType);
\r
172 buf.append(", startTimes=[");
\r
173 for (int i = 0; i < startTimes.length; i++) {
\r
177 buf.append(Long.toString(startTimes[i]));
\r
180 return buf.toString();
\r