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