Skip to content

Commit 2fa9b3e

Browse files
committed
conversions for java.util.Calendar + java.util.TimeZone
see issue #705
1 parent a9a346e commit 2fa9b3e

File tree

8 files changed

+466
-12
lines changed

8 files changed

+466
-12
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
* -----------------------------------------------------------------------
3+
* Copyright © 2013-2017 Meno Hochschild, <http://www.menodata.de/>
4+
* -----------------------------------------------------------------------
5+
* This file (OldApiTimezone.java) is part of project Time4J.
6+
*
7+
* Time4J is free software: You can redistribute it and/or modify it
8+
* under the terms of the GNU Lesser General Public License as published
9+
* by the Free Software Foundation, either version 2.1 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Time4J is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with Time4J. If not, see <http://www.gnu.org/licenses/>.
19+
* -----------------------------------------------------------------------
20+
*/
21+
22+
package net.time4j;
23+
24+
import net.time4j.tz.Timezone;
25+
import net.time4j.tz.TransitionHistory;
26+
import net.time4j.tz.ZonalTransition;
27+
28+
import java.util.Date;
29+
import java.util.List;
30+
31+
32+
/**
33+
* <p>Spezialimplementierung, die die Daten und Regeln einer Time4J-Zeitzone im Gewand des alten API bewahrt.. </p>
34+
*
35+
* @author Meno Hochschild
36+
* @since 3.37/4.32
37+
* @serial include
38+
* @concurrency {mutable}
39+
*/
40+
final class OldApiTimezone
41+
extends java.util.TimeZone {
42+
43+
//~ Statische Felder/Initialisierungen --------------------------------
44+
45+
private static final long serialVersionUID = -6919910650419401271L;
46+
47+
//~ Instanzvariablen --------------------------------------------------
48+
49+
/**
50+
* @serial the underlying Time4J-zone
51+
*/
52+
private final Timezone tz;
53+
54+
//~ Konstruktoren -----------------------------------------------------
55+
56+
OldApiTimezone(Timezone tz) {
57+
super();
58+
59+
this.tz = tz;
60+
61+
this.setID(tz.getID().canonical());
62+
assert (tz.getHistory() != null);
63+
64+
}
65+
66+
//~ Methoden ----------------------------------------------------------
67+
68+
@Override
69+
public int getOffset(long date) {
70+
71+
return this.tz.getOffset(
72+
TemporalType.MILLIS_SINCE_UNIX.translate(Long.valueOf(date))
73+
).getIntegralAmount() * 1000;
74+
75+
}
76+
77+
@Override
78+
public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) {
79+
80+
if (milliseconds < 0 || milliseconds >= 86_400_000) {
81+
throw new IllegalArgumentException("Milliseconds out of range: " + milliseconds);
82+
}
83+
84+
if (era == java.util.GregorianCalendar.BC) {
85+
year = 1 - year;
86+
} else if (era != java.util.GregorianCalendar.AD) {
87+
throw new IllegalArgumentException("Unknown era: " + era);
88+
} else if (dayOfWeek < 1 || dayOfWeek >= 7) {
89+
throw new IllegalArgumentException("Day-of-week out of range: " + dayOfWeek);
90+
}
91+
92+
return this.tz.getOffset(
93+
PlainDate.of(year, month + 1, day), // input month is zero-based
94+
PlainTime.midnightAtStartOfDay().plus(milliseconds, ClockUnit.MILLIS)
95+
).getIntegralAmount() * 1000;
96+
97+
}
98+
99+
@Override
100+
public void setRawOffset(int offsetMillis) {
101+
102+
// manipulation of raw offsets not supported => no-op
103+
104+
}
105+
106+
@Override
107+
public int getRawOffset() {
108+
109+
return this.tz.getStandardOffset(SystemClock.currentMoment()).getIntegralAmount() * 1000;
110+
111+
}
112+
113+
@Override
114+
public int getDSTSavings() {
115+
116+
TransitionHistory history = this.tz.getHistory();
117+
118+
if (history != null) {
119+
List<ZonalTransition> transitions = history.getStdTransitions();
120+
int dst = 0;
121+
for (int i = transitions.size() - 1; i >= 0; i--) {
122+
ZonalTransition t = transitions.get(i);
123+
if (t.isDaylightSaving()) {
124+
dst = t.getDaylightSavingOffset() * 1000;
125+
}
126+
}
127+
return dst;
128+
}
129+
130+
return 0;
131+
132+
}
133+
134+
@Override
135+
public boolean useDaylightTime() {
136+
137+
return !this.tz.isFixed() && (this.getDSTSavings() != 0);
138+
139+
}
140+
141+
@Override
142+
public boolean inDaylightTime(Date date) {
143+
144+
return this.tz.isDaylightSaving(TemporalType.JAVA_UTIL_DATE.translate(date));
145+
146+
}
147+
148+
@Override
149+
public boolean hasSameRules(java.util.TimeZone other) {
150+
151+
if (this == other) {
152+
return true;
153+
} else if (other instanceof OldApiTimezone) {
154+
OldApiTimezone that = (OldApiTimezone) other;
155+
return this.tz.getHistory().equals(that.tz.getHistory());
156+
}
157+
158+
return false;
159+
160+
}
161+
162+
@Override
163+
public boolean equals(Object obj) {
164+
165+
if (obj instanceof OldApiTimezone) {
166+
OldApiTimezone that = (OldApiTimezone) obj;
167+
return this.tz.equals(that.tz);
168+
}
169+
170+
return false;
171+
172+
}
173+
174+
@Override
175+
public int hashCode() {
176+
177+
return this.tz.hashCode();
178+
179+
}
180+
181+
@Override
182+
public String toString() {
183+
184+
return this.getClass().getName() + "@" + this.tz.toString();
185+
186+
}
187+
188+
Timezone getDelegate() {
189+
190+
return this.tz;
191+
192+
}
193+
194+
}

0 commit comments

Comments
 (0)