Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1112 lines (1017 sloc) 44.66 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 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 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
<?php
/**
* Represents a date and time as a value object
*
* @copyright Copyright (c) 2008-2011 Will Bond
* @author Will Bond [wb] <will@flourishlib.com>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fTimestamp
*
* @version 1.0.0b13
* @changes 1.0.0b13 Fixed a method signature [wb, 2011-08-24]
* @changes 1.0.0b12 Fixed a bug with the constructor not properly handling unix timestamps that are negative integers [wb, 2011-06-02]
* @changes 1.0.0b11 Changed the `$timestamp` and `$timezone` attributes to be protected [wb, 2011-03-20]
* @changes 1.0.0b10 Fixed a bug in ::__construct() with specifying a timezone other than the default for a relative time string such as "now" or "+2 hours" [wb, 2010-07-05]
* @changes 1.0.0b9 Added the `$simple` parameter to ::getFuzzyDifference() [wb, 2010-03-15]
* @changes 1.0.0b8 Fixed a bug with ::fixISOWeek() not properly parsing some ISO week dates [wb, 2009-10-06]
* @changes 1.0.0b7 Fixed a translation bug with ::getFuzzyDifference() [wb, 2009-07-11]
* @changes 1.0.0b6 Added ::registerUnformatCallback() and ::callUnformatCallback() to allow for localization of date/time parsing [wb, 2009-06-01]
* @changes 1.0.0b5 Backwards compatibility break - Removed ::getSecondsDifference() and ::getSeconds(), added ::eq(), ::gt(), ::gte(), ::lt(), ::lte() [wb, 2009-03-05]
* @changes 1.0.0b4 Updated for new fCore API [wb, 2009-02-16]
* @changes 1.0.0b3 Removed a useless double check of the strtotime() return value in ::__construct() [wb, 2009-01-21]
* @changes 1.0.0b2 Added support for CURRENT_TIMESTAMP, CURRENT_DATE and CURRENT_TIME SQL keywords [wb, 2009-01-11]
* @changes 1.0.0b The initial implementation [wb, 2008-02-12]
*/
class fTimestamp
{
// The following constants allow for nice looking callbacks to static methods
const callFormatCallback = 'fTimestamp::callFormatCallback';
const callUnformatCallback = 'fTimestamp::callUnformatCallback';
const combine = 'fTimestamp::combine';
const defineFormat = 'fTimestamp::defineFormat';
const fixISOWeek = 'fTimestamp::fixISOWeek';
const getDefaultTimezone = 'fTimestamp::getDefaultTimezone';
const isValidTimezone = 'fTimestamp::isValidTimezone';
const registerFormatCallback = 'fTimestamp::registerFormatCallback';
const registerUnformatCallback = 'fTimestamp::registerUnformatCallback';
const reset = 'fTimestamp::reset';
const setDefaultTimezone = 'fTimestamp::setDefaultTimezone';
const translateFormat = 'fTimestamp::translateFormat';


/**
* Pre-defined formatting styles
*
* @var array
*/
static private $formats = array();

/**
* A callback to process all formatting strings through
*
* @var callback
*/
static private $format_callback = NULL;

/**
* A callback to parse all date string to allow for locale-specific parsing
*
* @var callback
*/
static private $unformat_callback = NULL;


/**
* If a format callback is defined, call it
*
* @internal
*
* @param string $formatted_string The formatted date/time/timestamp string to be (possibly) modified
* @return string The (possibly) modified formatted string
*/
static public function callFormatCallback($formatted_string)
{
if (self::$format_callback) {
return call_user_func(self::$format_callback, $formatted_string);
}
return $formatted_string;
}


/**
* If an unformat callback is defined, call it
*
* @internal
*
* @param string $date_time_string A raw date/time/timestamp string to be (possibly) parsed/modified
* @return string The (possibly) parsed or modified date/time/timestamp
*/
static public function callUnformatCallback($date_time_string)
{
if (self::$unformat_callback) {
return call_user_func(self::$unformat_callback, $date_time_string);
}
return $date_time_string;
}


/**
* Checks to make sure the current version of PHP is high enough to support timezone features
*
* @return void
*/
static private function checkPHPVersion()
{
if (!fCore::checkVersion('5.1')) {
throw new fEnvironmentException(
'The %s class takes advantage of the timezone features in PHP 5.1.0 and newer. Unfortunately it appears you are running an older version of PHP.',
__CLASS__
);
}
}


/**
* Composes text using fText if loaded
*
* @param string $message The message to compose
* @param mixed $component A string or number to insert into the message
* @param mixed ...
* @return string The composed and possible translated message
*/
static protected function compose($message)
{
$args = array_slice(func_get_args(), 1);

if (class_exists('fText', FALSE)) {
return call_user_func_array(
array('fText', 'compose'),
array($message, $args)
);
} else {
return vsprintf($message, $args);
}
}


/**
* Creates a reusable format for formatting fDate, fTime, and fTimestamp objects
*
* @param string $name The name of the format
* @param string $formatting_string The format string compatible with the [http://php.net/date date()] function
* @return void
*/
static public function defineFormat($name, $formatting_string)
{
self::$formats[$name] = $formatting_string;
}


/**
* Fixes an ISO week format into `'Y-m-d'` so [http://php.net/strtotime strtotime()] will accept it
*
* @internal
*
* @param string $date The date to fix
* @return string The fixed date
*/
static public function fixISOWeek($date)
{
if (preg_match('#^(.*)(\d{4})-W(5[0-3]|[1-4][0-9]|0?[1-9])-([1-7])(.*)$#D', $date, $matches)) {
$before = $matches[1];
$year = $matches[2];
$week = $matches[3];
$day = $matches[4];
$after = $matches[5];

$first_of_year = strtotime($year . '-01-01');
$first_thursday = strtotime('thursday', $first_of_year);
$iso_year_start = strtotime('last monday', $first_thursday);

$ymd = date('Y-m-d', strtotime('+' . ($week-1) . ' weeks +' . ($day-1) . ' days', $iso_year_start));

$date = $before . $ymd . $after;
}
return $date;
}


/**
* Provides a consistent interface to getting the default timezone. Wraps the [http://php.net/date_default_timezone_get date_default_timezone_get()] function.
*
* @return string The default timezone used for all date/time calculations
*/
static public function getDefaultTimezone()
{
self::checkPHPVersion();

return date_default_timezone_get();
}


/**
* Checks to see if a timezone is valid
*
* @internal
*
* @param string $timezone The timezone to check
* @return boolean If the timezone is valid
*/
static public function isValidTimezone($timezone)
{
static $valid_timezones = array(
'UTC' => TRUE,
'Africa/Abidjan' => TRUE,
'Africa/Accra' => TRUE,
'Africa/Addis_Ababa' => TRUE,
'Africa/Algiers' => TRUE,
'Africa/Asmara' => TRUE,
'Africa/Asmera' => TRUE,
'Africa/Bamako' => TRUE,
'Africa/Bangui' => TRUE,
'Africa/Banjul' => TRUE,
'Africa/Bissau' => TRUE,
'Africa/Blantyre' => TRUE,
'Africa/Brazzaville' => TRUE,
'Africa/Bujumbura' => TRUE,
'Africa/Cairo' => TRUE,
'Africa/Casablanca' => TRUE,
'Africa/Ceuta' => TRUE,
'Africa/Conakry' => TRUE,
'Africa/Dakar' => TRUE,
'Africa/Dar_es_Salaam' => TRUE,
'Africa/Djibouti' => TRUE,
'Africa/Douala' => TRUE,
'Africa/El_Aaiun' => TRUE,
'Africa/Freetown' => TRUE,
'Africa/Gaborone' => TRUE,
'Africa/Harare' => TRUE,
'Africa/Johannesburg' => TRUE,
'Africa/Kampala' => TRUE,
'Africa/Khartoum' => TRUE,
'Africa/Kigali' => TRUE,
'Africa/Kinshasa' => TRUE,
'Africa/Lagos' => TRUE,
'Africa/Libreville' => TRUE,
'Africa/Lome' => TRUE,
'Africa/Luanda' => TRUE,
'Africa/Lubumbashi' => TRUE,
'Africa/Lusaka' => TRUE,
'Africa/Malabo' => TRUE,
'Africa/Maputo' => TRUE,
'Africa/Maseru' => TRUE,
'Africa/Mbabane' => TRUE,
'Africa/Mogadishu' => TRUE,
'Africa/Monrovia' => TRUE,
'Africa/Nairobi' => TRUE,
'Africa/Ndjamena' => TRUE,
'Africa/Niamey' => TRUE,
'Africa/Nouakchott' => TRUE,
'Africa/Ouagadougou' => TRUE,
'Africa/Porto-Novo' => TRUE,
'Africa/Sao_Tome' => TRUE,
'Africa/Timbuktu' => TRUE,
'Africa/Tripoli' => TRUE,
'Africa/Tunis' => TRUE,
'Africa/Windhoek' => TRUE,
'America/Adak' => TRUE,
'America/Anchorage' => TRUE,
'America/Anguilla' => TRUE,
'America/Antigua' => TRUE,
'America/Araguaina' => TRUE,
'America/Argentina/Buenos_Aires' => TRUE,
'America/Argentina/Catamarca' => TRUE,
'America/Argentina/ComodRivadavia' => TRUE,
'America/Argentina/Cordoba' => TRUE,
'America/Argentina/Jujuy' => TRUE,
'America/Argentina/La_Rioja' => TRUE,
'America/Argentina/Mendoza' => TRUE,
'America/Argentina/Rio_Gallegos' => TRUE,
'America/Argentina/San_Juan' => TRUE,
'America/Argentina/San_Luis' => TRUE,
'America/Argentina/Tucuman' => TRUE,
'America/Argentina/Ushuaia' => TRUE,
'America/Aruba' => TRUE,
'America/Asuncion' => TRUE,
'America/Atikokan' => TRUE,
'America/Atka' => TRUE,
'America/Bahia' => TRUE,
'America/Barbados' => TRUE,
'America/Belem' => TRUE,
'America/Belize' => TRUE,
'America/Blanc-Sablon' => TRUE,
'America/Boa_Vista' => TRUE,
'America/Bogota' => TRUE,
'America/Boise' => TRUE,
'America/Buenos_Aires' => TRUE,
'America/Cambridge_Bay' => TRUE,
'America/Campo_Grande' => TRUE,
'America/Cancun' => TRUE,
'America/Caracas' => TRUE,
'America/Catamarca' => TRUE,
'America/Cayenne' => TRUE,
'America/Cayman' => TRUE,
'America/Chicago' => TRUE,
'America/Chihuahua' => TRUE,
'America/Coral_Harbour' => TRUE,
'America/Cordoba' => TRUE,
'America/Costa_Rica' => TRUE,
'America/Cuiaba' => TRUE,
'America/Curacao' => TRUE,
'America/Danmarkshavn' => TRUE,
'America/Dawson' => TRUE,
'America/Dawson_Creek' => TRUE,
'America/Denver' => TRUE,
'America/Detroit' => TRUE,
'America/Dominica' => TRUE,
'America/Edmonton' => TRUE,
'America/Eirunepe' => TRUE,
'America/El_Salvador' => TRUE,
'America/Ensenada' => TRUE,
'America/Fort_Wayne' => TRUE,
'America/Fortaleza' => TRUE,
'America/Glace_Bay' => TRUE,
'America/Godthab' => TRUE,
'America/Goose_Bay' => TRUE,
'America/Grand_Turk' => TRUE,
'America/Grenada' => TRUE,
'America/Guadeloupe' => TRUE,
'America/Guatemala' => TRUE,
'America/Guayaquil' => TRUE,
'America/Guyana' => TRUE,
'America/Halifax' => TRUE,
'America/Havana' => TRUE,
'America/Hermosillo' => TRUE,
'America/Indiana/Indianapolis' => TRUE,
'America/Indiana/Knox' => TRUE,
'America/Indiana/Marengo' => TRUE,
'America/Indiana/Petersburg' => TRUE,
'America/Indiana/Tell_City' => TRUE,
'America/Indiana/Vevay' => TRUE,
'America/Indiana/Vincennes' => TRUE,
'America/Indiana/Winamac' => TRUE,
'America/Indianapolis' => TRUE,
'America/Inuvik' => TRUE,
'America/Iqaluit' => TRUE,
'America/Jamaica' => TRUE,
'America/Jujuy' => TRUE,
'America/Juneau' => TRUE,
'America/Kentucky/Louisville' => TRUE,
'America/Kentucky/Monticello' => TRUE,
'America/Knox_IN' => TRUE,
'America/La_Paz' => TRUE,
'America/Lima' => TRUE,
'America/Los_Angeles' => TRUE,
'America/Louisville' => TRUE,
'America/Maceio' => TRUE,
'America/Managua' => TRUE,
'America/Manaus' => TRUE,
'America/Marigot' => TRUE,
'America/Martinique' => TRUE,
'America/Mazatlan' => TRUE,
'America/Mendoza' => TRUE,
'America/Menominee' => TRUE,
'America/Merida' => TRUE,
'America/Mexico_City' => TRUE,
'America/Miquelon' => TRUE,
'America/Moncton' => TRUE,
'America/Monterrey' => TRUE,
'America/Montevideo' => TRUE,
'America/Montreal' => TRUE,
'America/Montserrat' => TRUE,
'America/Nassau' => TRUE,
'America/New_York' => TRUE,
'America/Nipigon' => TRUE,
'America/Nome' => TRUE,
'America/Noronha' => TRUE,
'America/North_Dakota/Center' => TRUE,
'America/North_Dakota/New_Salem' => TRUE,
'America/Panama' => TRUE,
'America/Pangnirtung' => TRUE,
'America/Paramaribo' => TRUE,
'America/Phoenix' => TRUE,
'America/Port-au-Prince' => TRUE,
'America/Port_of_Spain' => TRUE,
'America/Porto_Acre' => TRUE,
'America/Porto_Velho' => TRUE,
'America/Puerto_Rico' => TRUE,
'America/Rainy_River' => TRUE,
'America/Rankin_Inlet' => TRUE,
'America/Recife' => TRUE,
'America/Regina' => TRUE,
'America/Resolute' => TRUE,
'America/Rio_Branco' => TRUE,
'America/Rosario' => TRUE,
'America/Santiago' => TRUE,
'America/Santo_Domingo' => TRUE,
'America/Sao_Paulo' => TRUE,
'America/Scoresbysund' => TRUE,
'America/Shiprock' => TRUE,
'America/St_Barthelemy' => TRUE,
'America/St_Johns' => TRUE,
'America/St_Kitts' => TRUE,
'America/St_Lucia' => TRUE,
'America/St_Thomas' => TRUE,
'America/St_Vincent' => TRUE,
'America/Swift_Current' => TRUE,
'America/Tegucigalpa' => TRUE,
'America/Thule' => TRUE,
'America/Thunder_Bay' => TRUE,
'America/Tijuana' => TRUE,
'America/Toronto' => TRUE,
'America/Tortola' => TRUE,
'America/Vancouver' => TRUE,
'America/Virgin' => TRUE,
'America/Whitehorse' => TRUE,
'America/Winnipeg' => TRUE,
'America/Yakutat' => TRUE,
'America/Yellowknife' => TRUE,
'Antarctica/Casey' => TRUE,
'Antarctica/Davis' => TRUE,
'Antarctica/DumontDUrville' => TRUE,
'Antarctica/Mawson' => TRUE,
'Antarctica/McMurdo' => TRUE,
'Antarctica/Palmer' => TRUE,
'Antarctica/Rothera' => TRUE,
'Antarctica/South_Pole' => TRUE,
'Antarctica/Syowa' => TRUE,
'Antarctica/Vostok' => TRUE,
'Arctic/Longyearbyen' => TRUE,
'Asia/Aden' => TRUE,
'Asia/Almaty' => TRUE,
'Asia/Amman' => TRUE,
'Asia/Anadyr' => TRUE,
'Asia/Aqtau' => TRUE,
'Asia/Aqtobe' => TRUE,
'Asia/Ashgabat' => TRUE,
'Asia/Ashkhabad' => TRUE,
'Asia/Baghdad' => TRUE,
'Asia/Bahrain' => TRUE,
'Asia/Baku' => TRUE,
'Asia/Bangkok' => TRUE,
'Asia/Beirut' => TRUE,
'Asia/Bishkek' => TRUE,
'Asia/Brunei' => TRUE,
'Asia/Calcutta' => TRUE,
'Asia/Choibalsan' => TRUE,
'Asia/Chongqing' => TRUE,
'Asia/Chungking' => TRUE,
'Asia/Colombo' => TRUE,
'Asia/Dacca' => TRUE,
'Asia/Damascus' => TRUE,
'Asia/Dhaka' => TRUE,
'Asia/Dili' => TRUE,
'Asia/Dubai' => TRUE,
'Asia/Dushanbe' => TRUE,
'Asia/Gaza' => TRUE,
'Asia/Harbin' => TRUE,
'Asia/Ho_Chi_Minh' => TRUE,
'Asia/Hong_Kong' => TRUE,
'Asia/Hovd' => TRUE,
'Asia/Irkutsk' => TRUE,
'Asia/Istanbul' => TRUE,
'Asia/Jakarta' => TRUE,
'Asia/Jayapura' => TRUE,
'Asia/Jerusalem' => TRUE,
'Asia/Kabul' => TRUE,
'Asia/Kamchatka' => TRUE,
'Asia/Karachi' => TRUE,
'Asia/Kashgar' => TRUE,
'Asia/Katmandu' => TRUE,
'Asia/Kolkata' => TRUE,
'Asia/Krasnoyarsk' => TRUE,
'Asia/Kuala_Lumpur' => TRUE,
'Asia/Kuching' => TRUE,
'Asia/Kuwait' => TRUE,
'Asia/Macao' => TRUE,
'Asia/Macau' => TRUE,
'Asia/Magadan' => TRUE,
'Asia/Makassar' => TRUE,
'Asia/Manila' => TRUE,
'Asia/Muscat' => TRUE,
'Asia/Nicosia' => TRUE,
'Asia/Novosibirsk' => TRUE,
'Asia/Omsk' => TRUE,
'Asia/Oral' => TRUE,
'Asia/Phnom_Penh' => TRUE,
'Asia/Pontianak' => TRUE,
'Asia/Pyongyang' => TRUE,
'Asia/Qatar' => TRUE,
'Asia/Qyzylorda' => TRUE,
'Asia/Rangoon' => TRUE,
'Asia/Riyadh' => TRUE,
'Asia/Saigon' => TRUE,
'Asia/Sakhalin' => TRUE,
'Asia/Samarkand' => TRUE,
'Asia/Seoul' => TRUE,
'Asia/Shanghai' => TRUE,
'Asia/Singapore' => TRUE,
'Asia/Taipei' => TRUE,
'Asia/Tashkent' => TRUE,
'Asia/Tbilisi' => TRUE,
'Asia/Tehran' => TRUE,
'Asia/Tel_Aviv' => TRUE,
'Asia/Thimbu' => TRUE,
'Asia/Thimphu' => TRUE,
'Asia/Tokyo' => TRUE,
'Asia/Ujung_Pandang' => TRUE,
'Asia/Ulaanbaatar' => TRUE,
'Asia/Ulan_Bator' => TRUE,
'Asia/Urumqi' => TRUE,
'Asia/Vientiane' => TRUE,
'Asia/Vladivostok' => TRUE,
'Asia/Yakutsk' => TRUE,
'Asia/Yekaterinburg' => TRUE,
'Asia/Yerevan' => TRUE,
'Atlantic/Azores' => TRUE,
'Atlantic/Bermuda' => TRUE,
'Atlantic/Canary' => TRUE,
'Atlantic/Cape_Verde' => TRUE,
'Atlantic/Faeroe' => TRUE,
'Atlantic/Faroe' => TRUE,
'Atlantic/Jan_Mayen' => TRUE,
'Atlantic/Madeira' => TRUE,
'Atlantic/Reykjavik' => TRUE,
'Atlantic/South_Georgia' => TRUE,
'Atlantic/St_Helena' => TRUE,
'Atlantic/Stanley' => TRUE,
'Australia/ACT' => TRUE,
'Australia/Adelaide' => TRUE,
'Australia/Brisbane' => TRUE,
'Australia/Broken_Hill' => TRUE,
'Australia/Canberra' => TRUE,
'Australia/Currie' => TRUE,
'Australia/Darwin' => TRUE,
'Australia/Eucla' => TRUE,
'Australia/Hobart' => TRUE,
'Australia/LHI' => TRUE,
'Australia/Lindeman' => TRUE,
'Australia/Lord_Howe' => TRUE,
'Australia/Melbourne' => TRUE,
'Australia/North' => TRUE,
'Australia/NSW' => TRUE,
'Australia/Perth' => TRUE,
'Australia/Queensland' => TRUE,
'Australia/South' => TRUE,
'Australia/Sydney' => TRUE,
'Australia/Tasmania' => TRUE,
'Australia/Victoria' => TRUE,
'Australia/West' => TRUE,
'Australia/Yancowinna' => TRUE,
'Europe/Amsterdam' => TRUE,
'Europe/Andorra' => TRUE,
'Europe/Athens' => TRUE,
'Europe/Belfast' => TRUE,
'Europe/Belgrade' => TRUE,
'Europe/Berlin' => TRUE,
'Europe/Bratislava' => TRUE,
'Europe/Brussels' => TRUE,
'Europe/Bucharest' => TRUE,
'Europe/Budapest' => TRUE,
'Europe/Chisinau' => TRUE,
'Europe/Copenhagen' => TRUE,
'Europe/Dublin' => TRUE,
'Europe/Gibraltar' => TRUE,
'Europe/Guernsey' => TRUE,
'Europe/Helsinki' => TRUE,
'Europe/Isle_of_Man' => TRUE,
'Europe/Istanbul' => TRUE,
'Europe/Jersey' => TRUE,
'Europe/Kaliningrad' => TRUE,
'Europe/Kiev' => TRUE,
'Europe/Lisbon' => TRUE,
'Europe/Ljubljana' => TRUE,
'Europe/London' => TRUE,
'Europe/Luxembourg' => TRUE,
'Europe/Madrid' => TRUE,
'Europe/Malta' => TRUE,
'Europe/Mariehamn' => TRUE,
'Europe/Minsk' => TRUE,
'Europe/Monaco' => TRUE,
'Europe/Moscow' => TRUE,
'Europe/Nicosia' => TRUE,
'Europe/Oslo' => TRUE,
'Europe/Paris' => TRUE,
'Europe/Podgorica' => TRUE,
'Europe/Prague' => TRUE,
'Europe/Riga' => TRUE,
'Europe/Rome' => TRUE,
'Europe/Samara' => TRUE,
'Europe/San_Marino' => TRUE,
'Europe/Sarajevo' => TRUE,
'Europe/Simferopol' => TRUE,
'Europe/Skopje' => TRUE,
'Europe/Sofia' => TRUE,
'Europe/Stockholm' => TRUE,
'Europe/Tallinn' => TRUE,
'Europe/Tirane' => TRUE,
'Europe/Tiraspol' => TRUE,
'Europe/Uzhgorod' => TRUE,
'Europe/Vaduz' => TRUE,
'Europe/Vatican' => TRUE,
'Europe/Vienna' => TRUE,
'Europe/Vilnius' => TRUE,
'Europe/Volgograd' => TRUE,
'Europe/Warsaw' => TRUE,
'Europe/Zagreb' => TRUE,
'Europe/Zaporozhye' => TRUE,
'Europe/Zurich' => TRUE,
'Indian/Antananarivo' => TRUE,
'Indian/Chagos' => TRUE,
'Indian/Christmas' => TRUE,
'Indian/Cocos' => TRUE,
'Indian/Comoro' => TRUE,
'Indian/Kerguelen' => TRUE,
'Indian/Mahe' => TRUE,
'Indian/Maldives' => TRUE,
'Indian/Mauritius' => TRUE,
'Indian/Mayotte' => TRUE,
'Indian/Reunion' => TRUE,
'Pacific/Apia' => TRUE,
'Pacific/Auckland' => TRUE,
'Pacific/Chatham' => TRUE,
'Pacific/Easter' => TRUE,
'Pacific/Efate' => TRUE,
'Pacific/Enderbury' => TRUE,
'Pacific/Fakaofo' => TRUE,
'Pacific/Fiji' => TRUE,
'Pacific/Funafuti' => TRUE,
'Pacific/Galapagos' => TRUE,
'Pacific/Gambier' => TRUE,
'Pacific/Guadalcanal' => TRUE,
'Pacific/Guam' => TRUE,
'Pacific/Honolulu' => TRUE,
'Pacific/Johnston' => TRUE,
'Pacific/Kiritimati' => TRUE,
'Pacific/Kosrae' => TRUE,
'Pacific/Kwajalein' => TRUE,
'Pacific/Majuro' => TRUE,
'Pacific/Marquesas' => TRUE,
'Pacific/Midway' => TRUE,
'Pacific/Nauru' => TRUE,
'Pacific/Niue' => TRUE,
'Pacific/Norfolk' => TRUE,
'Pacific/Noumea' => TRUE,
'Pacific/Pago_Pago' => TRUE,
'Pacific/Palau' => TRUE,
'Pacific/Pitcairn' => TRUE,
'Pacific/Ponape' => TRUE,
'Pacific/Port_Moresby' => TRUE,
'Pacific/Rarotonga' => TRUE,
'Pacific/Saipan' => TRUE,
'Pacific/Samoa' => TRUE,
'Pacific/Tahiti' => TRUE,
'Pacific/Tarawa' => TRUE,
'Pacific/Tongatapu' => TRUE,
'Pacific/Truk' => TRUE,
'Pacific/Wake' => TRUE,
'Pacific/Wallis' => TRUE
);

return isset($valid_timezones[$timezone]);
}


/**
* Allows setting a callback to translate or modify any return values from ::format(), fDate::format() and fTime::format()
*
* @param callback $callback The callback to pass all formatted dates/times/timestamps through. Should accept a single string and return a single string.
* @return void
*/
static public function registerFormatCallback($callback)
{
if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}
self::$format_callback = $callback;
}


/**
* Allows setting a callback to parse any date strings passed into ::__construct(), fDate::__construct() and fTime::__construct()
*
* @param callback $callback The callback to pass all date/time/timestamp strings through. Should accept a single string and return a single string that is parsable by [http://php.net/strtotime `strtotime()`].
* @return void
*/
static public function registerUnformatCallback($callback)
{
if (is_string($callback) && strpos($callback, '::') !== FALSE) {
$callback = explode('::', $callback);
}
self::$unformat_callback = $callback;
}


/**
* Resets the configuration of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
self::$formats = array();
self::$format_callback = NULL;
}


/**
* Provides a consistent interface to setting the default timezone. Wraps the [http://php.net/date_default_timezone_set date_default_timezone_set()] function.
*
* @param string $timezone The default timezone to use for all date/time calculations
* @return void
*/
static public function setDefaultTimezone($timezone)
{
self::checkPHPVersion();

$result = date_default_timezone_set($timezone);
if (!$result) {
throw new fProgrammerException(
'The timezone specified, %s, is not a valid timezone',
$timezone
);
}
}


/**
* Takes a format name set via ::defineFormat() and returns the [http://php.net/date date()] function formatting string
*
* @internal
*
* @param string $format The format to translate
* @return string The formatting string. If no matching format was found, this will be the same as the `$format` parameter.
*/
static public function translateFormat($format)
{
if (isset(self::$formats[$format])) {
$format = self::$formats[$format];
}
return $format;
}


/**
* The date/time
*
* @var integer
*/
protected $timestamp;

/**
* The timezone for this date/time
*
* @var string
*/
protected $timezone;


/**
* Creates the date/time to represent
*
* @throws fValidationException When `$datetime` is not a valid date/time, date or time value
*
* @param fTimestamp|object|string|integer $datetime The date/time to represent, `NULL` is interpreted as now
* @param string $timezone The timezone for the date/time. This causes the date/time to be interpretted as being in the specified timezone. If not specified, will default to timezone set by ::setDefaultTimezone().
* @return fTimestamp
*/
public function __construct($datetime=NULL, $timezone=NULL)
{
self::checkPHPVersion();

$default_tz = date_default_timezone_get();

if ($timezone) {
if (!self::isValidTimezone($timezone)) {
throw new fValidationException(
'The timezone specified, %s, is not a valid timezone',
$timezone
);
}

} elseif ($datetime instanceof fTimestamp) {
$timezone = $datetime->timezone;

} else {
$timezone = $default_tz;
}

$this->timezone = $timezone;

if ($datetime === NULL) {
$timestamp = time();
} elseif (is_numeric($datetime) && preg_match('#^-?\d+$#D', $datetime)) {
$timestamp = (int) $datetime;
} elseif (is_string($datetime) && in_array(strtoupper($datetime), array('CURRENT_TIMESTAMP', 'CURRENT_TIME'))) {
$timestamp = time();
} elseif (is_string($datetime) && strtoupper($datetime) == 'CURRENT_DATE') {
$timestamp = strtotime(date('Y-m-d'));
} else {
if (is_object($datetime) && is_callable(array($datetime, '__toString'))) {
$datetime = $datetime->__toString();
} elseif (is_numeric($datetime) || is_object($datetime)) {
$datetime = (string) $datetime;
}

$datetime = self::callUnformatCallback($datetime);

if ($timezone != $default_tz) {
date_default_timezone_set($timezone);
}
$timestamp = strtotime(self::fixISOWeek($datetime));
if ($timezone != $default_tz) {
date_default_timezone_set($default_tz);
}
}

if ($timestamp === FALSE) {
throw new fValidationException(
'The date/time specified, %s, does not appear to be a valid date/time',
$datetime
);
}

$this->timestamp = $timestamp;
}


/**
* All requests that hit this method should be requests for callbacks
*
* @internal
*
* @param string $method The method to create a callback for
* @return callback The callback for the method requested
*/
public function __get($method)
{
return array($this, $method);
}


/**
* Returns this date/time
*
* @return string The `'Y-m-d H:i:s'` format of this date/time
*/
public function __toString()
{
return $this->format('Y-m-d H:i:s');
}


/**
* Changes the date/time by the adjustment specified
*
* @throws fValidationException When `$adjustment` is not a valid relative date/time measurement or timezone
*
* @param string $adjustment The adjustment to make - may be a relative adjustment or a different timezone
* @return fTimestamp The adjusted date/time
*/
public function adjust($adjustment)
{
if (self::isValidTimezone($adjustment)) {
$timezone = $adjustment;
$timestamp = $this->timestamp;

} else {
$timezone = $this->timezone;
$timestamp = strtotime($adjustment, $this->timestamp);

if ($timestamp === FALSE || $timestamp === -1) {
throw new fValidationException(
'The adjustment specified, %s, does not appear to be a valid relative date/time measurement',
$adjustment
);
}
}

return new fTimestamp($timestamp, $timezone);
}


/**
* If this timestamp is equal to the timestamp passed
*
* @param fTimestamp|object|string|integer $other_timestamp The timestamp to compare with, `NULL` is interpreted as today
* @return boolean If this timestamp is equal to the one passed
*/
public function eq($other_timestamp=NULL)
{
$other_timestamp = new fTimestamp($other_timestamp);
return $this->timestamp == $other_timestamp->timestamp;
}


/**
* Formats the date/time
*
* @param string $format The [http://php.net/date date()] function compatible formatting string, or a format name from ::defineFormat()
* @return string The formatted date/time
*/
public function format($format)
{
$format = self::translateFormat($format);

$default_tz = date_default_timezone_get();
date_default_timezone_set($this->timezone);

$formatted = date($format, $this->timestamp);

date_default_timezone_set($default_tz);

return self::callFormatCallback($formatted);
}


/**
* Returns the approximate difference in time, discarding any unit of measure but the least specific.
*
* The output will read like:
*
* - "This timestamp is `{return value}` the provided one" when a timestamp it passed
* - "This timestamp is `{return value}`" when no timestamp is passed and comparing with the current timestamp
*
* Examples of output for a timestamp passed might be:
*
* - `'5 minutes after'`
* - `'2 hours before'`
* - `'2 days after'`
* - `'at the same time'`
*
* Examples of output for no timestamp passed might be:
*
* - `'5 minutes ago'`
* - `'2 hours ago'`
* - `'2 days from now'`
* - `'1 year ago'`
* - `'right now'`
*
* You would never get the following output since it includes more than one unit of time measurement:
*
* - `'5 minutes and 28 seconds'`
* - `'3 weeks, 1 day and 4 hours'`
*
* Values that are close to the next largest unit of measure will be rounded up:
*
* - `'55 minutes'` would be represented as `'1 hour'`, however `'45 minutes'` would not
* - `'29 days'` would be represented as `'1 month'`, but `'21 days'` would be shown as `'3 weeks'`
*
* @param fTimestamp|object|string|integer $other_timestamp The timestamp to create the difference with, `NULL` is interpreted as now
* @param boolean $simple When `TRUE`, the returned value will only include the difference in the two timestamps, but not `from now`, `ago`, `after` or `before`
* @param boolean |$simple
* @return string The fuzzy difference in time between the this timestamp and the one provided
*/
public function getFuzzyDifference($other_timestamp=NULL, $simple=FALSE)
{
if (is_bool($other_timestamp)) {
$simple = $other_timestamp;
$other_timestamp = NULL;
}

$relative_to_now = FALSE;
if ($other_timestamp === NULL) {
$relative_to_now = TRUE;
}
$other_timestamp = new fTimestamp($other_timestamp);

$diff = $this->timestamp - $other_timestamp->timestamp;

if (abs($diff) < 10) {
if ($relative_to_now) {
return self::compose('right now');
}
return self::compose('at the same time');
}

$break_points = array(
/* 45 seconds */
45 => array(1, self::compose('second'), self::compose('seconds')),
/* 45 minutes */
2700 => array(60, self::compose('minute'), self::compose('minutes')),
/* 18 hours */
64800 => array(3600, self::compose('hour'), self::compose('hours')),
/* 5 days */
432000 => array(86400, self::compose('day'), self::compose('days')),
/* 3 weeks */
1814400 => array(604800, self::compose('week'), self::compose('weeks')),
/* 9 months */
23328000 => array(2592000, self::compose('month'), self::compose('months')),
/* largest int */
2147483647 => array(31536000, self::compose('year'), self::compose('years'))
);

foreach ($break_points as $break_point => $unit_info) {
if (abs($diff) > $break_point) { continue; }

$unit_diff = round(abs($diff)/$unit_info[0]);
$units = fGrammar::inflectOnQuantity($unit_diff, $unit_info[1], $unit_info[2]);
break;
}

if ($simple) {
return self::compose('%1$s %2$s', $unit_diff, $units);
}

if ($relative_to_now) {
if ($diff > 0) {
return self::compose('%1$s %2$s from now', $unit_diff, $units);
}

return self::compose('%1$s %2$s ago', $unit_diff, $units);
}

if ($diff > 0) {
return self::compose('%1$s %2$s after', $unit_diff, $units);
}

return self::compose('%1$s %2$s before', $unit_diff, $units);
}


/**
* If this timestamp is greater than the timestamp passed
*
* @param fTimestamp|object|string|integer $other_timestamp The timestamp to compare with, `NULL` is interpreted as now
* @return boolean If this timestamp is greater than the one passed
*/
public function gt($other_timestamp=NULL)
{
$other_timestamp = new fTimestamp($other_timestamp);
return $this->timestamp > $other_timestamp->timestamp;
}


/**
* If this timestamp is greater than or equal to the timestamp passed
*
* @param fTimestamp|object|string|integer $other_timestamp The timestamp to compare with, `NULL` is interpreted as now
* @return boolean If this timestamp is greater than or equal to the one passed
*/
public function gte($other_timestamp=NULL)
{
$other_timestamp = new fTimestamp($other_timestamp);
return $this->timestamp >= $other_timestamp->timestamp;
}


/**
* If this timestamp is less than the timestamp passed
*
* @param fTimestamp|object|string|integer $other_timestamp The timestamp to compare with, `NULL` is interpreted as today
* @return boolean If this timestamp is less than the one passed
*/
public function lt($other_timestamp=NULL)
{
$other_timestamp = new fTimestamp($other_timestamp);
return $this->timestamp < $other_timestamp->timestamp;
}


/**
* If this timestamp is less than or equal to the timestamp passed
*
* @param fTimestamp|object|string|integer $other_timestamp The timestamp to compare with, `NULL` is interpreted as today
* @return boolean If this timestamp is less than or equal to the one passed
*/
public function lte($other_timestamp=NULL)
{
$other_timestamp = new fTimestamp($other_timestamp);
return $this->timestamp <= $other_timestamp->timestamp;
}


/**
* Modifies the current timestamp, creating a new fTimestamp object
*
* The purpose of this method is to allow for easy creation of a timestamp
* based on this timestamp. Below are some examples of formats to
* modify the current timestamp:
*
* - `'Y-m-01 H:i:s'` to change the date of the timestamp to the first of the month:
* - `'Y-m-t H:i:s'` to change the date of the timestamp to the last of the month:
* - `'Y-m-d 17:i:s'` to set the hour of the timestamp to 5 PM:
*
* @param string $format The current timestamp will be formatted with this string, and the output used to create a new object. The format should **not** include the timezone (character `e`).
* @param string $timezone The timezone for the new object if different from the current timezone
* @return fTimestamp The new timestamp
*/
public function modify($format, $timezone=NULL)
{
$timezone = ($timezone !== NULL) ? $timezone : $this->timezone;
return new fTimestamp($this->format($format), $timezone);
}
}



/**
* Copyright (c) 2008-2011 Will Bond <will@flourishlib.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
Something went wrong with that request. Please try again.