@@ -33,27 +33,32 @@ public actual open class TimeZone internal constructor(internal val value: TimeZ
33
33
if (zoneId.length == 1 ) {
34
34
throw IllegalTimeZoneException (" Invalid zone ID: $zoneId " )
35
35
}
36
- if (zoneId.startsWith(" +" ) || zoneId.startsWith(" -" )) {
37
- return UtcOffset .parse(zoneId).asTimeZone()
38
- }
39
- if (zoneId == " UTC" || zoneId == " GMT" || zoneId == " UT" ) {
40
- return FixedOffsetTimeZone (UtcOffset (0 ), zoneId)
41
- }
42
- if (zoneId.startsWith(" UTC+" ) || zoneId.startsWith(" GMT+" ) ||
43
- zoneId.startsWith(" UTC-" ) || zoneId.startsWith(" GMT-" )) {
44
- val prefix = zoneId.take(3 )
45
- val offset = UtcOffset .parse(zoneId.substring(3 ))
46
- return when (offset.totalSeconds) {
47
- 0 -> FixedOffsetTimeZone (offset, prefix)
48
- else -> FixedOffsetTimeZone (offset, " $prefix$offset " )
36
+ try {
37
+ if (zoneId.startsWith(" +" ) || zoneId.startsWith(" -" )) {
38
+ return UtcOffset .parse(zoneId).asTimeZone()
49
39
}
50
- }
51
- if (zoneId.startsWith(" UT+" ) || zoneId.startsWith(" UT-" )) {
52
- val offset = UtcOffset .parse(zoneId.substring(2 ))
53
- return when (offset.totalSeconds) {
54
- 0 -> FixedOffsetTimeZone (offset, " UT" )
55
- else -> FixedOffsetTimeZone (offset, " UT$offset " )
40
+ if (zoneId == " UTC" || zoneId == " GMT" || zoneId == " UT" ) {
41
+ return FixedOffsetTimeZone (UtcOffset (0 ), zoneId)
42
+ }
43
+ if (zoneId.startsWith(" UTC+" ) || zoneId.startsWith(" GMT+" ) ||
44
+ zoneId.startsWith(" UTC-" ) || zoneId.startsWith(" GMT-" )
45
+ ) {
46
+ val prefix = zoneId.take(3 )
47
+ val offset = UtcOffset .parse(zoneId.substring(3 ))
48
+ return when (offset.totalSeconds) {
49
+ 0 -> FixedOffsetTimeZone (offset, prefix)
50
+ else -> FixedOffsetTimeZone (offset, " $prefix$offset " )
51
+ }
52
+ }
53
+ if (zoneId.startsWith(" UT+" ) || zoneId.startsWith(" UT-" )) {
54
+ val offset = UtcOffset .parse(zoneId.substring(2 ))
55
+ return when (offset.totalSeconds) {
56
+ 0 -> FixedOffsetTimeZone (offset, " UT" )
57
+ else -> FixedOffsetTimeZone (offset, " UT$offset " )
58
+ }
56
59
}
60
+ } catch (e: DateTimeFormatException ) {
61
+ throw IllegalTimeZoneException (e)
57
62
}
58
63
return TimeZone (PlatformTimeZoneImpl .of(zoneId))
59
64
}
@@ -154,11 +159,11 @@ public actual class UtcOffset internal constructor(public actual val totalSecond
154
159
minutes = parseNumber(offsetString, 4 , true )
155
160
seconds = parseNumber(offsetString, 7 , true )
156
161
}
157
- else -> throw IllegalTimeZoneException (" Invalid ID for UtcOffset, invalid format: $offsetString " )
162
+ else -> throw DateTimeFormatException (" Invalid ID for UtcOffset, invalid format: $offsetString " )
158
163
}
159
164
val first: Char = offsetString[0 ]
160
165
if (first != ' +' && first != ' -' ) {
161
- throw IllegalTimeZoneException (
166
+ throw DateTimeFormatException (
162
167
" Invalid ID for UtcOffset, plus/minus not found when expected: $offsetString " )
163
168
}
164
169
return if (first == ' -' ) {
@@ -171,30 +176,30 @@ public actual class UtcOffset internal constructor(public actual val totalSecond
171
176
// org.threeten.bp.ZoneOffset#validate
172
177
private fun validate (hours : Int , minutes : Int , seconds : Int ) {
173
178
if (hours < - 18 || hours > 18 ) {
174
- throw IllegalTimeZoneException (" Zone offset hours not in valid range: value " + hours +
179
+ throw DateTimeFormatException (" Zone offset hours not in valid range: value " + hours +
175
180
" is not in the range -18 to 18" )
176
181
}
177
182
if (hours > 0 ) {
178
183
if (minutes < 0 || seconds < 0 ) {
179
- throw IllegalTimeZoneException (" Zone offset minutes and seconds must be positive because hours is positive" )
184
+ throw DateTimeFormatException (" Zone offset minutes and seconds must be positive because hours is positive" )
180
185
}
181
186
} else if (hours < 0 ) {
182
187
if (minutes > 0 || seconds > 0 ) {
183
- throw IllegalTimeZoneException (" Zone offset minutes and seconds must be negative because hours is negative" )
188
+ throw DateTimeFormatException (" Zone offset minutes and seconds must be negative because hours is negative" )
184
189
}
185
190
} else if (minutes > 0 && seconds < 0 || minutes < 0 && seconds > 0 ) {
186
- throw IllegalTimeZoneException (" Zone offset minutes and seconds must have the same sign" )
191
+ throw DateTimeFormatException (" Zone offset minutes and seconds must have the same sign" )
187
192
}
188
193
if (abs(minutes) > 59 ) {
189
- throw IllegalTimeZoneException (" Zone offset minutes not in valid range: abs(value) " +
194
+ throw DateTimeFormatException (" Zone offset minutes not in valid range: abs(value) " +
190
195
abs(minutes) + " is not in the range 0 to 59" )
191
196
}
192
197
if (abs(seconds) > 59 ) {
193
- throw IllegalTimeZoneException (" Zone offset seconds not in valid range: abs(value) " +
198
+ throw DateTimeFormatException (" Zone offset seconds not in valid range: abs(value) " +
194
199
abs(seconds) + " is not in the range 0 to 59" )
195
200
}
196
201
if (abs(hours) == 18 && (abs(minutes) > 0 || abs(seconds) > 0 )) {
197
- throw IllegalTimeZoneException ( " Zone offset not in valid range: -18:00 to +18:00" )
202
+ throw DateTimeFormatException ( " Utc offset not in valid range: -18:00 to +18:00" )
198
203
}
199
204
}
200
205
@@ -216,12 +221,12 @@ public actual class UtcOffset internal constructor(public actual val totalSecond
216
221
// org.threeten.bp.ZoneOffset#parseNumber
217
222
private fun parseNumber (offsetId : CharSequence , pos : Int , precededByColon : Boolean ): Int {
218
223
if (precededByColon && offsetId[pos - 1 ] != ' :' ) {
219
- throw IllegalTimeZoneException (" Invalid ID for ZoneOffset , colon not found when expected: $offsetId " )
224
+ throw DateTimeFormatException (" Invalid ID for UtcOffset , colon not found when expected: $offsetId " )
220
225
}
221
226
val ch1 = offsetId[pos]
222
227
val ch2 = offsetId[pos + 1 ]
223
228
if (ch1 < ' 0' || ch1 > ' 9' || ch2 < ' 0' || ch2 > ' 9' ) {
224
- throw IllegalTimeZoneException (" Invalid ID for ZoneOffset , non numeric characters found: $offsetId " )
229
+ throw DateTimeFormatException (" Invalid ID for UtcOffset , non numeric characters found: $offsetId " )
225
230
}
226
231
return (ch1 - ' 0' ) * 10 + (ch2 - ' 0' )
227
232
}
0 commit comments