| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text.format; |
| 18 |
|
| 19 |
import android.util.TimeFormatException; |
| 20 |
|
| 21 |
import com.android.i18n.timezone.WallTime; |
| 22 |
import com.android.i18n.timezone.ZoneInfoData; |
| 23 |
import com.android.i18n.timezone.ZoneInfoDb; |
| 24 |
|
| 25 |
import java.util.Locale; |
| 26 |
import java.util.TimeZone; |
| 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 |
@Deprecated |
| 56 |
public class Time { |
| 57 |
private static final String Y_M_D_T_H_M_S_000 = "%Y-%m-%dT%H:%M:%S.000"; |
| 58 |
private static final String Y_M_D_T_H_M_S_000_Z = "%Y-%m-%dT%H:%M:%S.000Z"; |
| 59 |
private static final String Y_M_D = "%Y-%m-%d"; |
| 60 |
|
| 61 |
public static final String TIMEZONE_UTC = "UTC"; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static final int EPOCH_JULIAN_DAY = 2440588; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static final int MONDAY_BEFORE_JULIAN_EPOCH = EPOCH_JULIAN_DAY - 3; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public boolean allDay; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public int second; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public int minute; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public int hour; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public int monthDay; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public int month; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public int year; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public int weekDay; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public int yearDay; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public int isDst; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public long gmtoff; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public String timezone; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static final int SECOND = 1; |
| 146 |
public static final int MINUTE = 2; |
| 147 |
public static final int HOUR = 3; |
| 148 |
public static final int MONTH_DAY = 4; |
| 149 |
public static final int MONTH = 5; |
| 150 |
public static final int YEAR = 6; |
| 151 |
public static final int WEEK_DAY = 7; |
| 152 |
public static final int YEAR_DAY = 8; |
| 153 |
public static final int WEEK_NUM = 9; |
| 154 |
|
| 155 |
public static final int SUNDAY = 0; |
| 156 |
public static final int MONDAY = 1; |
| 157 |
public static final int TUESDAY = 2; |
| 158 |
public static final int WEDNESDAY = 3; |
| 159 |
public static final int THURSDAY = 4; |
| 160 |
public static final int FRIDAY = 5; |
| 161 |
public static final int SATURDAY = 6; |
| 162 |
|
| 163 |
|
| 164 |
private TimeCalculator calculator; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public Time(String timezoneId) { |
| 173 |
if (timezoneId == null) { |
| 174 |
throw new NullPointerException("timezoneId is null!"); |
| 175 |
} |
| 176 |
initialize(timezoneId); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public Time() { |
| 184 |
initialize(TimeZone.getDefault().getID()); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public Time(Time other) { |
| 194 |
initialize(other.timezone); |
| 195 |
set(other); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
private void initialize(String timezoneId) { |
| 200 |
this.timezone = timezoneId; |
| 201 |
this.year = 1970; |
| 202 |
this.monthDay = 1; |
| 203 |
|
| 204 |
|
| 205 |
this.isDst = -1; |
| 206 |
|
| 207 |
|
| 208 |
calculator = new TimeCalculator(timezoneId); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public long normalize(boolean ignoreDst) { |
| 230 |
calculator.copyFieldsFromTime(this); |
| 231 |
long timeInMillis = calculator.toMillis(ignoreDst); |
| 232 |
calculator.copyFieldsToTime(this); |
| 233 |
return timeInMillis; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void switchTimezone(String timezone) { |
| 244 |
calculator.copyFieldsFromTime(this); |
| 245 |
calculator.switchTimeZone(timezone); |
| 246 |
calculator.copyFieldsToTime(this); |
| 247 |
this.timezone = timezone; |
| 248 |
} |
| 249 |
|
| 250 |
private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31, |
| 251 |
31, 30, 31, 30, 31 }; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public int getActualMaximum(int field) { |
| 261 |
switch (field) { |
| 262 |
case SECOND: |
| 263 |
return 59; |
| 264 |
case MINUTE: |
| 265 |
return 59; |
| 266 |
case HOUR: |
| 267 |
return 23; |
| 268 |
case MONTH_DAY: { |
| 269 |
int n = DAYS_PER_MONTH[this.month]; |
| 270 |
if (n != 28) { |
| 271 |
return n; |
| 272 |
} else { |
| 273 |
int y = this.year; |
| 274 |
return ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0)) ? 29 : 28; |
| 275 |
} |
| 276 |
} |
| 277 |
case MONTH: |
| 278 |
return 11; |
| 279 |
case YEAR: |
| 280 |
return 2037; |
| 281 |
case WEEK_DAY: |
| 282 |
return 6; |
| 283 |
case YEAR_DAY: { |
| 284 |
int y = this.year; |
| 285 |
|
| 286 |
return ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0)) ? 365 : 364; |
| 287 |
} |
| 288 |
case WEEK_NUM: |
| 289 |
throw new RuntimeException("WEEK_NUM not implemented"); |
| 290 |
default: |
| 291 |
throw new RuntimeException("bad field=" + field); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public void clear(String timezoneId) { |
| 301 |
if (timezoneId == null) { |
| 302 |
throw new NullPointerException("timezone is null!"); |
| 303 |
} |
| 304 |
this.timezone = timezoneId; |
| 305 |
this.allDay = false; |
| 306 |
this.second = 0; |
| 307 |
this.minute = 0; |
| 308 |
this.hour = 0; |
| 309 |
this.monthDay = 0; |
| 310 |
this.month = 0; |
| 311 |
this.year = 0; |
| 312 |
this.weekDay = 0; |
| 313 |
this.yearDay = 0; |
| 314 |
this.gmtoff = 0; |
| 315 |
this.isDst = -1; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public static int compare(Time a, Time b) { |
| 337 |
if (a == null) { |
| 338 |
throw new NullPointerException("a == null"); |
| 339 |
} else if (b == null) { |
| 340 |
throw new NullPointerException("b == null"); |
| 341 |
} |
| 342 |
a.calculator.copyFieldsFromTime(a); |
| 343 |
b.calculator.copyFieldsFromTime(b); |
| 344 |
|
| 345 |
return TimeCalculator.compare(a.calculator, b.calculator); |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public String format(String format) { |
| 356 |
calculator.copyFieldsFromTime(this); |
| 357 |
return calculator.format(format); |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
@Override |
| 364 |
public String toString() { |
| 365 |
|
| 366 |
|
| 367 |
TimeCalculator calculator = new TimeCalculator(this.timezone); |
| 368 |
calculator.copyFieldsFromTime(this); |
| 369 |
return calculator.toStringInternal(); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public boolean parse(String s) { |
| 409 |
if (s == null) { |
| 410 |
throw new NullPointerException("time string is null"); |
| 411 |
} |
| 412 |
if (parseInternal(s)) { |
| 413 |
timezone = TIMEZONE_UTC; |
| 414 |
return true; |
| 415 |
} |
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private boolean parseInternal(String s) { |
| 423 |
int len = s.length(); |
| 424 |
if (len < 8) { |
| 425 |
throw new TimeFormatException("String is too short: \"" + s + |
| 426 |
"\" Expected at least 8 characters."); |
| 427 |
} |
| 428 |
|
| 429 |
boolean inUtc = false; |
| 430 |
|
| 431 |
|
| 432 |
int n = getChar(s, 0, 1000); |
| 433 |
n += getChar(s, 1, 100); |
| 434 |
n += getChar(s, 2, 10); |
| 435 |
n += getChar(s, 3, 1); |
| 436 |
year = n; |
| 437 |
|
| 438 |
|
| 439 |
n = getChar(s, 4, 10); |
| 440 |
n += getChar(s, 5, 1); |
| 441 |
n--; |
| 442 |
month = n; |
| 443 |
|
| 444 |
|
| 445 |
n = getChar(s, 6, 10); |
| 446 |
n += getChar(s, 7, 1); |
| 447 |
monthDay = n; |
| 448 |
|
| 449 |
if (len > 8) { |
| 450 |
if (len < 15) { |
| 451 |
throw new TimeFormatException( |
| 452 |
"String is too short: \"" + s |
| 453 |
+ "\" If there are more than 8 characters there must be at least" |
| 454 |
+ " 15."); |
| 455 |
} |
| 456 |
checkChar(s, 8, 'T'); |
| 457 |
allDay = false; |
| 458 |
|
| 459 |
|
| 460 |
n = getChar(s, 9, 10); |
| 461 |
n += getChar(s, 10, 1); |
| 462 |
hour = n; |
| 463 |
|
| 464 |
|
| 465 |
n = getChar(s, 11, 10); |
| 466 |
n += getChar(s, 12, 1); |
| 467 |
minute = n; |
| 468 |
|
| 469 |
|
| 470 |
n = getChar(s, 13, 10); |
| 471 |
n += getChar(s, 14, 1); |
| 472 |
second = n; |
| 473 |
|
| 474 |
if (len > 15) { |
| 475 |
|
| 476 |
checkChar(s, 15, 'Z'); |
| 477 |
inUtc = true; |
| 478 |
} |
| 479 |
} else { |
| 480 |
allDay = true; |
| 481 |
hour = 0; |
| 482 |
minute = 0; |
| 483 |
second = 0; |
| 484 |
} |
| 485 |
|
| 486 |
weekDay = 0; |
| 487 |
yearDay = 0; |
| 488 |
isDst = -1; |
| 489 |
gmtoff = 0; |
| 490 |
return inUtc; |
| 491 |
} |
| 492 |
|
| 493 |
private void checkChar(String s, int spos, char expected) { |
| 494 |
char c = s.charAt(spos); |
| 495 |
if (c != expected) { |
| 496 |
throw new TimeFormatException(String.format( |
| 497 |
"Unexpected character 0x%02d at pos=%d. Expected 0x%02d (\'%c\').", |
| 498 |
(int) c, spos, (int) expected, expected)); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
private static int getChar(String s, int spos, int mul) { |
| 503 |
char c = s.charAt(spos); |
| 504 |
if (Character.isDigit(c)) { |
| 505 |
return Character.getNumericValue(c) * mul; |
| 506 |
} else { |
| 507 |
throw new TimeFormatException("Parse error at pos=" + spos); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public boolean parse3339(String s) { |
| 543 |
if (s == null) { |
| 544 |
throw new NullPointerException("time string is null"); |
| 545 |
} |
| 546 |
if (parse3339Internal(s)) { |
| 547 |
timezone = TIMEZONE_UTC; |
| 548 |
return true; |
| 549 |
} |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
private boolean parse3339Internal(String s) { |
| 554 |
int len = s.length(); |
| 555 |
if (len < 10) { |
| 556 |
throw new TimeFormatException("String too short --- expected at least 10 characters."); |
| 557 |
} |
| 558 |
boolean inUtc = false; |
| 559 |
|
| 560 |
|
| 561 |
int n = getChar(s, 0, 1000); |
| 562 |
n += getChar(s, 1, 100); |
| 563 |
n += getChar(s, 2, 10); |
| 564 |
n += getChar(s, 3, 1); |
| 565 |
year = n; |
| 566 |
|
| 567 |
checkChar(s, 4, '-'); |
| 568 |
|
| 569 |
|
| 570 |
n = getChar(s, 5, 10); |
| 571 |
n += getChar(s, 6, 1); |
| 572 |
--n; |
| 573 |
month = n; |
| 574 |
|
| 575 |
checkChar(s, 7, '-'); |
| 576 |
|
| 577 |
|
| 578 |
n = getChar(s, 8, 10); |
| 579 |
n += getChar(s, 9, 1); |
| 580 |
monthDay = n; |
| 581 |
|
| 582 |
if (len >= 19) { |
| 583 |
|
| 584 |
checkChar(s, 10, 'T'); |
| 585 |
allDay = false; |
| 586 |
|
| 587 |
|
| 588 |
n = getChar(s, 11, 10); |
| 589 |
n += getChar(s, 12, 1); |
| 590 |
|
| 591 |
|
| 592 |
int hour = n; |
| 593 |
|
| 594 |
checkChar(s, 13, ':'); |
| 595 |
|
| 596 |
|
| 597 |
n = getChar(s, 14, 10); |
| 598 |
n += getChar(s, 15, 1); |
| 599 |
|
| 600 |
int minute = n; |
| 601 |
|
| 602 |
checkChar(s, 16, ':'); |
| 603 |
|
| 604 |
|
| 605 |
n = getChar(s, 17, 10); |
| 606 |
n += getChar(s, 18, 1); |
| 607 |
second = n; |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
int tzIndex = 19; |
| 612 |
if (tzIndex < len && s.charAt(tzIndex) == '.') { |
| 613 |
do { |
| 614 |
tzIndex++; |
| 615 |
} while (tzIndex < len && Character.isDigit(s.charAt(tzIndex))); |
| 616 |
} |
| 617 |
|
| 618 |
int offset = 0; |
| 619 |
if (len > tzIndex) { |
| 620 |
char c = s.charAt(tzIndex); |
| 621 |
|
| 622 |
|
| 623 |
switch (c) { |
| 624 |
case 'Z': |
| 625 |
|
| 626 |
offset = 0; |
| 627 |
break; |
| 628 |
case '-': |
| 629 |
offset = 1; |
| 630 |
break; |
| 631 |
case '+': |
| 632 |
offset = -1; |
| 633 |
break; |
| 634 |
default: |
| 635 |
throw new TimeFormatException(String.format( |
| 636 |
"Unexpected character 0x%02d at position %d. Expected + or -", |
| 637 |
(int) c, tzIndex)); |
| 638 |
} |
| 639 |
inUtc = true; |
| 640 |
|
| 641 |
if (offset != 0) { |
| 642 |
if (len < tzIndex + 6) { |
| 643 |
throw new TimeFormatException( |
| 644 |
String.format("Unexpected length; should be %d characters", |
| 645 |
tzIndex + 6)); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
n = getChar(s, tzIndex + 1, 10); |
| 650 |
n += getChar(s, tzIndex + 2, 1); |
| 651 |
n *= offset; |
| 652 |
hour += n; |
| 653 |
|
| 654 |
|
| 655 |
n = getChar(s, tzIndex + 4, 10); |
| 656 |
n += getChar(s, tzIndex + 5, 1); |
| 657 |
n *= offset; |
| 658 |
minute += n; |
| 659 |
} |
| 660 |
} |
| 661 |
this.hour = hour; |
| 662 |
this.minute = minute; |
| 663 |
|
| 664 |
if (offset != 0) { |
| 665 |
normalize(false); |
| 666 |
} |
| 667 |
} else { |
| 668 |
allDay = true; |
| 669 |
this.hour = 0; |
| 670 |
this.minute = 0; |
| 671 |
this.second = 0; |
| 672 |
} |
| 673 |
|
| 674 |
this.weekDay = 0; |
| 675 |
this.yearDay = 0; |
| 676 |
this.isDst = -1; |
| 677 |
this.gmtoff = 0; |
| 678 |
return inUtc; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public static String getCurrentTimezone() { |
| 685 |
return TimeZone.getDefault().getID(); |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public void setToNow() { |
| 692 |
set(System.currentTimeMillis()); |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
public long toMillis(boolean ignoreDst) { |
| 751 |
calculator.copyFieldsFromTime(this); |
| 752 |
return calculator.toMillis(ignoreDst); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
public void set(long millis) { |
| 763 |
allDay = false; |
| 764 |
calculator.timezone = timezone; |
| 765 |
calculator.setTimeInMillis(millis); |
| 766 |
calculator.copyFieldsToTime(this); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
public String format2445() { |
| 776 |
calculator.copyFieldsFromTime(this); |
| 777 |
return calculator.format2445(!allDay); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public void set(Time that) { |
| 784 |
this.timezone = that.timezone; |
| 785 |
this.allDay = that.allDay; |
| 786 |
this.second = that.second; |
| 787 |
this.minute = that.minute; |
| 788 |
this.hour = that.hour; |
| 789 |
this.monthDay = that.monthDay; |
| 790 |
this.month = that.month; |
| 791 |
this.year = that.year; |
| 792 |
this.weekDay = that.weekDay; |
| 793 |
this.yearDay = that.yearDay; |
| 794 |
this.isDst = that.isDst; |
| 795 |
this.gmtoff = that.gmtoff; |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
public void set(int second, int minute, int hour, int monthDay, int month, int year) { |
| 803 |
this.allDay = false; |
| 804 |
this.second = second; |
| 805 |
this.minute = minute; |
| 806 |
this.hour = hour; |
| 807 |
this.monthDay = monthDay; |
| 808 |
this.month = month; |
| 809 |
this.year = year; |
| 810 |
this.weekDay = 0; |
| 811 |
this.yearDay = 0; |
| 812 |
this.isDst = -1; |
| 813 |
this.gmtoff = 0; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
public void set(int monthDay, int month, int year) { |
| 826 |
this.allDay = true; |
| 827 |
this.second = 0; |
| 828 |
this.minute = 0; |
| 829 |
this.hour = 0; |
| 830 |
this.monthDay = monthDay; |
| 831 |
this.month = month; |
| 832 |
this.year = year; |
| 833 |
this.weekDay = 0; |
| 834 |
this.yearDay = 0; |
| 835 |
this.isDst = -1; |
| 836 |
this.gmtoff = 0; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
public boolean before(Time that) { |
| 851 |
return Time.compare(this, that) < 0; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
public boolean after(Time that) { |
| 867 |
return Time.compare(this, that) > 0; |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
private static final int[] sThursdayOffset = { -3, 3, 2, 1, 0, -1, -2 }; |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
public int getWeekNumber() { |
| 898 |
|
| 899 |
int closestThursday = yearDay + sThursdayOffset[weekDay]; |
| 900 |
|
| 901 |
|
| 902 |
if (closestThursday >= 0 && closestThursday <= 364) { |
| 903 |
return closestThursday / 7 + 1; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
Time temp = new Time(this); |
| 908 |
temp.monthDay += sThursdayOffset[weekDay]; |
| 909 |
temp.normalize(true ); |
| 910 |
return temp.yearDay / 7 + 1; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
public String format3339(boolean allDay) { |
| 924 |
if (allDay) { |
| 925 |
return format(Y_M_D); |
| 926 |
} else if (TIMEZONE_UTC.equals(timezone)) { |
| 927 |
return format(Y_M_D_T_H_M_S_000_Z); |
| 928 |
} else { |
| 929 |
String base = format(Y_M_D_T_H_M_S_000); |
| 930 |
String sign = (gmtoff < 0) ? "-" : "+"; |
| 931 |
int offset = (int) Math.abs(gmtoff); |
| 932 |
int minutes = (offset % 3600) / 60; |
| 933 |
int hours = offset / 3600; |
| 934 |
|
| 935 |
return String.format(Locale.US, "%s%s%02d:%02d", base, sign, hours, minutes); |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
public static boolean isEpoch(Time time) { |
| 954 |
long millis = time.toMillis(true); |
| 955 |
return getJulianDay(millis, 0 ) == EPOCH_JULIAN_DAY; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
@Deprecated |
| 979 |
public static int getJulianDay(long millis, long gmtoffSeconds) { |
| 980 |
long offsetMillis = gmtoffSeconds * 1000; |
| 981 |
long adjustedMillis = millis + offsetMillis; |
| 982 |
long julianDay = adjustedMillis / DateUtils.DAY_IN_MILLIS; |
| 983 |
|
| 984 |
if (adjustedMillis < 0 && adjustedMillis % DateUtils.DAY_IN_MILLIS != 0) { |
| 985 |
julianDay--; |
| 986 |
} |
| 987 |
return (int) (julianDay + EPOCH_JULIAN_DAY); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
public long setJulianDay(int julianDay) { |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
long millis = (julianDay - EPOCH_JULIAN_DAY) * DateUtils.DAY_IN_MILLIS; |
| 1015 |
set(millis); |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
int approximateDay = getJulianDay(millis, gmtoff); |
| 1020 |
int diff = julianDay - approximateDay; |
| 1021 |
monthDay += diff; |
| 1022 |
|
| 1023 |
|
| 1024 |
hour = 0; |
| 1025 |
minute = 0; |
| 1026 |
second = 0; |
| 1027 |
millis = normalize(true); |
| 1028 |
return millis; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
public static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek) { |
| 1044 |
int diff = THURSDAY - firstDayOfWeek; |
| 1045 |
if (diff < 0) { |
| 1046 |
diff += 7; |
| 1047 |
} |
| 1048 |
int refDay = EPOCH_JULIAN_DAY - diff; |
| 1049 |
return (julianDay - refDay) / 7; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
public static int getJulianMondayFromWeeksSinceEpoch(int week) { |
| 1063 |
return MONDAY_BEFORE_JULIAN_EPOCH + week * 7; |
| 1064 |
} |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
private static class TimeCalculator { |
| 1074 |
public final WallTime wallTime; |
| 1075 |
public String timezone; |
| 1076 |
|
| 1077 |
|
| 1078 |
private ZoneInfoData mZoneInfoData; |
| 1079 |
|
| 1080 |
public TimeCalculator(String timezoneId) { |
| 1081 |
this.mZoneInfoData = lookupZoneInfoData(timezoneId); |
| 1082 |
this.wallTime = new WallTime(); |
| 1083 |
} |
| 1084 |
|
| 1085 |
public long toMillis(boolean ignoreDst) { |
| 1086 |
if (ignoreDst) { |
| 1087 |
wallTime.setIsDst(-1); |
| 1088 |
} |
| 1089 |
|
| 1090 |
int r = wallTime.mktime(mZoneInfoData); |
| 1091 |
if (r == -1) { |
| 1092 |
return -1; |
| 1093 |
} |
| 1094 |
return r * 1000L; |
| 1095 |
} |
| 1096 |
|
| 1097 |
public void setTimeInMillis(long millis) { |
| 1098 |
|
| 1099 |
int intSeconds = (int) (millis / 1000); |
| 1100 |
|
| 1101 |
updateZoneInfoFromTimeZone(); |
| 1102 |
wallTime.localtime(intSeconds, mZoneInfoData); |
| 1103 |
} |
| 1104 |
|
| 1105 |
public String format(String format) { |
| 1106 |
if (format == null) { |
| 1107 |
format = "%c"; |
| 1108 |
} |
| 1109 |
TimeFormatter formatter = new TimeFormatter(); |
| 1110 |
return formatter.format(format, wallTime, mZoneInfoData); |
| 1111 |
} |
| 1112 |
|
| 1113 |
private void updateZoneInfoFromTimeZone() { |
| 1114 |
if (!mZoneInfoData.getID().equals(timezone)) { |
| 1115 |
this.mZoneInfoData = lookupZoneInfoData(timezone); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
private static ZoneInfoData lookupZoneInfoData(String timezoneId) { |
| 1120 |
ZoneInfoData zoneInfoData = ZoneInfoDb.getInstance().makeZoneInfoData(timezoneId); |
| 1121 |
if (zoneInfoData == null) { |
| 1122 |
zoneInfoData = ZoneInfoDb.getInstance().makeZoneInfoData("GMT"); |
| 1123 |
} |
| 1124 |
if (zoneInfoData == null) { |
| 1125 |
throw new AssertionError("GMT not found: \"" + timezoneId + "\""); |
| 1126 |
} |
| 1127 |
return zoneInfoData; |
| 1128 |
} |
| 1129 |
|
| 1130 |
public void switchTimeZone(String timezone) { |
| 1131 |
int seconds = wallTime.mktime(mZoneInfoData); |
| 1132 |
this.timezone = timezone; |
| 1133 |
updateZoneInfoFromTimeZone(); |
| 1134 |
wallTime.localtime(seconds, mZoneInfoData); |
| 1135 |
} |
| 1136 |
|
| 1137 |
public String format2445(boolean hasTime) { |
| 1138 |
char[] buf = new char[hasTime ? 16 : 8]; |
| 1139 |
int n = wallTime.getYear(); |
| 1140 |
|
| 1141 |
buf[0] = toChar(n / 1000); |
| 1142 |
n %= 1000; |
| 1143 |
buf[1] = toChar(n / 100); |
| 1144 |
n %= 100; |
| 1145 |
buf[2] = toChar(n / 10); |
| 1146 |
n %= 10; |
| 1147 |
buf[3] = toChar(n); |
| 1148 |
|
| 1149 |
n = wallTime.getMonth() + 1; |
| 1150 |
buf[4] = toChar(n / 10); |
| 1151 |
buf[5] = toChar(n % 10); |
| 1152 |
|
| 1153 |
n = wallTime.getMonthDay(); |
| 1154 |
buf[6] = toChar(n / 10); |
| 1155 |
buf[7] = toChar(n % 10); |
| 1156 |
|
| 1157 |
if (!hasTime) { |
| 1158 |
return new String(buf, 0, 8); |
| 1159 |
} |
| 1160 |
|
| 1161 |
buf[8] = 'T'; |
| 1162 |
|
| 1163 |
n = wallTime.getHour(); |
| 1164 |
buf[9] = toChar(n / 10); |
| 1165 |
buf[10] = toChar(n % 10); |
| 1166 |
|
| 1167 |
n = wallTime.getMinute(); |
| 1168 |
buf[11] = toChar(n / 10); |
| 1169 |
buf[12] = toChar(n % 10); |
| 1170 |
|
| 1171 |
n = wallTime.getSecond(); |
| 1172 |
buf[13] = toChar(n / 10); |
| 1173 |
buf[14] = toChar(n % 10); |
| 1174 |
|
| 1175 |
if (TIMEZONE_UTC.equals(timezone)) { |
| 1176 |
|
| 1177 |
buf[15] = 'Z'; |
| 1178 |
return new String(buf, 0, 16); |
| 1179 |
} else { |
| 1180 |
return new String(buf, 0, 15); |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
private char toChar(int n) { |
| 1185 |
return (n >= 0 && n <= 9) ? (char) (n + '0') : ' '; |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
public String toStringInternal() { |
| 1193 |
|
| 1194 |
|
| 1195 |
return String.format("%04d%02d%02dT%02d%02d%02d%s(%d,%d,%d,%d,%d)", |
| 1196 |
wallTime.getYear(), |
| 1197 |
wallTime.getMonth() + 1, |
| 1198 |
wallTime.getMonthDay(), |
| 1199 |
wallTime.getHour(), |
| 1200 |
wallTime.getMinute(), |
| 1201 |
wallTime.getSecond(), |
| 1202 |
timezone, |
| 1203 |
wallTime.getWeekDay(), |
| 1204 |
wallTime.getYearDay(), |
| 1205 |
wallTime.getGmtOffset(), |
| 1206 |
wallTime.getIsDst(), |
| 1207 |
toMillis(false ) / 1000 |
| 1208 |
); |
| 1209 |
|
| 1210 |
} |
| 1211 |
|
| 1212 |
public static int compare(TimeCalculator aObject, TimeCalculator bObject) { |
| 1213 |
if (aObject.timezone.equals(bObject.timezone)) { |
| 1214 |
|
| 1215 |
int diff = aObject.wallTime.getYear() - bObject.wallTime.getYear(); |
| 1216 |
if (diff != 0) { |
| 1217 |
return diff; |
| 1218 |
} |
| 1219 |
|
| 1220 |
diff = aObject.wallTime.getMonth() - bObject.wallTime.getMonth(); |
| 1221 |
if (diff != 0) { |
| 1222 |
return diff; |
| 1223 |
} |
| 1224 |
|
| 1225 |
diff = aObject.wallTime.getMonthDay() - bObject.wallTime.getMonthDay(); |
| 1226 |
if (diff != 0) { |
| 1227 |
return diff; |
| 1228 |
} |
| 1229 |
|
| 1230 |
diff = aObject.wallTime.getHour() - bObject.wallTime.getHour(); |
| 1231 |
if (diff != 0) { |
| 1232 |
return diff; |
| 1233 |
} |
| 1234 |
|
| 1235 |
diff = aObject.wallTime.getMinute() - bObject.wallTime.getMinute(); |
| 1236 |
if (diff != 0) { |
| 1237 |
return diff; |
| 1238 |
} |
| 1239 |
|
| 1240 |
diff = aObject.wallTime.getSecond() - bObject.wallTime.getSecond(); |
| 1241 |
if (diff != 0) { |
| 1242 |
return diff; |
| 1243 |
} |
| 1244 |
|
| 1245 |
return 0; |
| 1246 |
} else { |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
long am = aObject.toMillis(false ); |
| 1251 |
long bm = bObject.toMillis(false ); |
| 1252 |
long diff = am - bm; |
| 1253 |
return (diff < 0) ? -1 : ((diff > 0) ? 1 : 0); |
| 1254 |
} |
| 1255 |
|
| 1256 |
} |
| 1257 |
|
| 1258 |
public void copyFieldsToTime(Time time) { |
| 1259 |
time.second = wallTime.getSecond(); |
| 1260 |
time.minute = wallTime.getMinute(); |
| 1261 |
time.hour = wallTime.getHour(); |
| 1262 |
time.monthDay = wallTime.getMonthDay(); |
| 1263 |
time.month = wallTime.getMonth(); |
| 1264 |
time.year = wallTime.getYear(); |
| 1265 |
|
| 1266 |
|
| 1267 |
time.weekDay = wallTime.getWeekDay(); |
| 1268 |
time.yearDay = wallTime.getYearDay(); |
| 1269 |
|
| 1270 |
|
| 1271 |
time.isDst = wallTime.getIsDst(); |
| 1272 |
|
| 1273 |
time.gmtoff = wallTime.getGmtOffset(); |
| 1274 |
} |
| 1275 |
|
| 1276 |
public void copyFieldsFromTime(Time time) { |
| 1277 |
wallTime.setSecond(time.second); |
| 1278 |
wallTime.setMinute(time.minute); |
| 1279 |
wallTime.setHour(time.hour); |
| 1280 |
wallTime.setMonthDay(time.monthDay); |
| 1281 |
wallTime.setMonth(time.month); |
| 1282 |
wallTime.setYear(time.year); |
| 1283 |
wallTime.setWeekDay(time.weekDay); |
| 1284 |
wallTime.setYearDay(time.yearDay); |
| 1285 |
wallTime.setIsDst(time.isDst); |
| 1286 |
wallTime.setGmtOffset((int) time.gmtoff); |
| 1287 |
|
| 1288 |
if (time.allDay && (time.second != 0 || time.minute != 0 || time.hour != 0)) { |
| 1289 |
throw new IllegalArgumentException("allDay is true but sec, min, hour are not 0."); |
| 1290 |
} |
| 1291 |
|
| 1292 |
timezone = time.timezone; |
| 1293 |
updateZoneInfoFromTimeZone(); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
} |