-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathPSTTimeZone.java
More file actions
263 lines (228 loc) · 10.1 KB
/
PSTTimeZone.java
File metadata and controls
263 lines (228 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Copyright 2010 Richard Johnson & Orin Eman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ---
*
* This file is part of java-libpst.
*
* java-libpst is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-libpst is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with java-libpst. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.pff;
import java.util.Calendar;
import java.util.SimpleTimeZone;
/**
* Class containing time zone information
*
* @author Orin Eman
*
*
*/
public class PSTTimeZone {
PSTTimeZone(final byte[] timeZoneData) {
this.rule = null;
this.name = "";
try {
final int headerLen = (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, 2, 4);
final int nameLen = 2 * (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, 6, 8);
this.name = new String(timeZoneData, 8, nameLen, "UTF-16LE");
int ruleOffset = 8 + nameLen;
final int nRules = (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, ruleOffset, ruleOffset + 2);
ruleOffset = 4 + headerLen;
for (int rule = 0; rule < nRules; ++rule) {
// Is this rule the effective rule?
final int flags = (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, ruleOffset + 4,
ruleOffset + 6);
if ((flags & 0x0002) != 0) {
this.rule = new TZRule(timeZoneData, ruleOffset + 6);
break;
}
ruleOffset += 66;
}
} catch (final Exception e) {
System.err.printf("Exception reading timezone: %s\n", e.toString());
e.printStackTrace();
this.rule = null;
this.name = "";
}
}
PSTTimeZone(String name, final byte[] timeZoneData) {
this.name = name;
this.rule = null;
try {
this.rule = new TZRule(new SYSTEMTIME(), timeZoneData, 0);
} catch (final Exception e) {
System.err.printf("Exception reading timezone: %s\n", e.toString());
e.printStackTrace();
this.rule = null;
name = "";
}
}
public String getName() {
return this.name;
}
public SimpleTimeZone getSimpleTimeZone() {
if (this.simpleTimeZone != null) {
return this.simpleTimeZone;
}
if (this.rule.startStandard.wMonth == 0) {
// A time zone with no daylight savings time
this.simpleTimeZone = new SimpleTimeZone((this.rule.lBias + this.rule.lStandardBias) * 60 * 1000,
this.name);
return this.simpleTimeZone;
}
final int startMonth = (this.rule.startDaylight.wMonth - 1) + Calendar.JANUARY;
final int startDayOfMonth = (this.rule.startDaylight.wDay == 5) ? -1
: ((this.rule.startDaylight.wDay - 1) * 7) + 1;
final int startDayOfWeek = this.rule.startDaylight.wDayOfWeek + Calendar.SUNDAY;
final int endMonth = (this.rule.startStandard.wMonth - 1) + Calendar.JANUARY;
final int endDayOfMonth = (this.rule.startStandard.wDay == 5) ? -1
: ((this.rule.startStandard.wDay - 1) * 7) + 1;
final int endDayOfWeek = this.rule.startStandard.wDayOfWeek + Calendar.SUNDAY;
final int savings = (this.rule.lStandardBias - this.rule.lDaylightBias) * 60 * 1000;
this.simpleTimeZone = new SimpleTimeZone(-((this.rule.lBias + this.rule.lStandardBias) * 60 * 1000), this.name,
startMonth, startDayOfMonth, -startDayOfWeek,
(((((this.rule.startDaylight.wHour * 60) + this.rule.startDaylight.wMinute) * 60)
+ this.rule.startDaylight.wSecond) * 1000) + this.rule.startDaylight.wMilliseconds,
endMonth, endDayOfMonth, -endDayOfWeek,
(((((this.rule.startStandard.wHour * 60) + this.rule.startStandard.wMinute) * 60)
+ this.rule.startStandard.wSecond) * 1000) + this.rule.startStandard.wMilliseconds,
savings);
return this.simpleTimeZone;
}
public boolean isEqual(final PSTTimeZone rhs) {
if (this.name.equalsIgnoreCase(rhs.name)) {
if (this.rule.isEqual(rhs.rule)) {
return true;
}
System.err.printf("Warning: different timezones with the same name: %s\n", this.name);
}
return false;
}
public SYSTEMTIME getStart() {
return this.rule.dtStart;
}
public int getBias() {
return this.rule.lBias;
}
public int getStandardBias() {
return this.rule.lStandardBias;
}
public int getDaylightBias() {
return this.rule.lDaylightBias;
}
public SYSTEMTIME getDaylightStart() {
return this.rule.startDaylight;
}
public SYSTEMTIME getStandardStart() {
return this.rule.startStandard;
}
public class SYSTEMTIME {
SYSTEMTIME() {
this.wYear = 0;
this.wMonth = 0;
this.wDayOfWeek = 0;
this.wDay = 0;
this.wHour = 0;
this.wMinute = 0;
this.wSecond = 0;
this.wMilliseconds = 0;
}
SYSTEMTIME(final byte[] timeZoneData, final int offset) {
this.wYear = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset, offset + 2) & 0x7FFF);
this.wMonth = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 2, offset + 4)
& 0x7FFF);
this.wDayOfWeek = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 4, offset + 6)
& 0x7FFF);
this.wDay = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 6, offset + 8)
& 0x7FFF);
this.wHour = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 8, offset + 10)
& 0x7FFF);
this.wMinute = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 10, offset + 12)
& 0x7FFF);
this.wSecond = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 12, offset + 14)
& 0x7FFF);
this.wMilliseconds = (short) (PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 14,
offset + 16) & 0x7FFF);
}
boolean isEqual(final SYSTEMTIME rhs) {
return this.wYear == rhs.wYear && this.wMonth == rhs.wMonth && this.wDayOfWeek == rhs.wDayOfWeek
&& this.wDay == rhs.wDay && this.wHour == rhs.wHour && this.wMinute == rhs.wMinute
&& this.wSecond == rhs.wSecond && this.wMilliseconds == rhs.wMilliseconds;
}
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
/**
* A static copy of the UTC time zone, available for others to use
*/
public static SimpleTimeZone utcTimeZone = new SimpleTimeZone(0, "UTC");
private class TZRule {
TZRule(final SYSTEMTIME dtStart, final byte[] timeZoneData, final int offset) {
this.dtStart = dtStart;
this.InitBiases(timeZoneData, offset);
@SuppressWarnings("unused")
final short wStandardYear = (short) PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 12,
offset + 14);
this.startStandard = new SYSTEMTIME(timeZoneData, offset + 14);
@SuppressWarnings("unused")
final short wDaylightYear = (short) PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 30,
offset + 32);
this.startDaylight = new SYSTEMTIME(timeZoneData, offset + 32);
}
TZRule(final byte[] timeZoneData, final int offset) {
this.dtStart = new SYSTEMTIME(timeZoneData, offset);
this.InitBiases(timeZoneData, offset + 16);
this.startStandard = new SYSTEMTIME(timeZoneData, offset + 28);
this.startDaylight = new SYSTEMTIME(timeZoneData, offset + 44);
}
private void InitBiases(final byte[] timeZoneData, final int offset) {
this.lBias = (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset, offset + 4);
this.lStandardBias = (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 4, offset + 8);
this.lDaylightBias = (int) PSTObject.convertLittleEndianBytesToLong(timeZoneData, offset + 8, offset + 12);
}
boolean isEqual(final TZRule rhs) {
return this.dtStart.isEqual(rhs.dtStart) && this.lBias == rhs.lBias
&& this.lStandardBias == rhs.lStandardBias && this.lDaylightBias == rhs.lDaylightBias
&& this.startStandard.isEqual(rhs.startStandard) && this.startDaylight.isEqual(rhs.startDaylight);
}
SYSTEMTIME dtStart;
int lBias;
int lStandardBias;
int lDaylightBias;
SYSTEMTIME startStandard;
SYSTEMTIME startDaylight;
}
private String name;
private TZRule rule;
private SimpleTimeZone simpleTimeZone = null;
}