Applies the local time zone offset from UTC to the applicable time field values. Depending on the local time zone offset, adjustments (i.e. rollover) will be made to the Year, Day, Hour, Minute time field values. @param offset the local offset, in minutes from UTC.
(int offset)
| 264 | * @param offset the local offset, in minutes from UTC. |
| 265 | */ |
| 266 | private void apply_offset(int offset) |
| 267 | { |
| 268 | if (offset == 0) return; |
| 269 | if (offset < -24*60 || offset > 24*60) { |
| 270 | throw new IllegalArgumentException("bad offset " + offset); |
| 271 | } |
| 272 | // To convert _to_ UTC you must SUBTRACT the local offset |
| 273 | offset = -offset; |
| 274 | byte hour_offset = (byte) (offset / 60); |
| 275 | byte min_offset = (byte) (offset - (hour_offset * 60)); |
| 276 | |
| 277 | if (offset < 0) { |
| 278 | _minute += min_offset; // lower the minute value by adding a negative offset |
| 279 | _hour += hour_offset; |
| 280 | if (_minute < 0) { |
| 281 | _minute += (byte) 60; |
| 282 | _hour -= 1; |
| 283 | } |
| 284 | if (_hour >= 0) return; // hour is 0-23 |
| 285 | _hour += (byte) 24; |
| 286 | _day -= (byte) 1; |
| 287 | if (_day >= 1) return; // day is 1-31 |
| 288 | // we can't do this until we've figured out the month and year: _day += last_day_in_month(_year, _month); |
| 289 | _month -= 1; |
| 290 | if (_month >= 1) { |
| 291 | _day += last_day_in_month(_year, _month); // now we know (when the year doesn't change |
| 292 | assert(_day == last_day_in_month(_year, _month)); |
| 293 | return; // 1-12 |
| 294 | } |
| 295 | _month += (byte) 12; |
| 296 | _year -= (short) 1; |
| 297 | if (_year < 1) throw new IllegalArgumentException("year is less than 1"); |
| 298 | _day += last_day_in_month(_year, _month); // and now we know, even if the year did change |
| 299 | assert(_day == last_day_in_month(_year, _month)); |
| 300 | } |
| 301 | else { |
| 302 | _minute += min_offset; // lower the minute value by adding a negative offset |
| 303 | _hour += hour_offset; |
| 304 | if (_minute > 59) { |
| 305 | _minute -= (byte) 60; |
| 306 | _hour += (byte) 1; |
| 307 | } |
| 308 | if (_hour < 24) return; // hour is 0-23 |
| 309 | _hour -= (byte) 24; |
| 310 | _day += (byte) 1; |
| 311 | if (_day <= last_day_in_month(_year, _month)) return; // day is 1-31 |
| 312 | // we can't do this until we figure out the final month and year: _day -= last_day_in_month(_year, _month); |
| 313 | _day = 1; // this is always the case |
| 314 | _month += (byte) 1; |
| 315 | if (_month <= 12) { |
| 316 | return; // 1-12 |
| 317 | } |
| 318 | _month -= (byte) 12; |
| 319 | _year += (short) 1; |
| 320 | if (_year > 9999) throw new IllegalArgumentException("year exceeds 9999"); |
| 321 | } |
| 322 | } |
| 323 |
no test coverage detected