/usr/share/php/Date/Span.php is in php-date 1.4.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// {{{ Header
/**
* Generic time span handling class for PEAR
*
* PHP versions 4 and 5
*
* LICENSE:
*
* Copyright (c) 1997-2005 Leandro Lucarella, Pierre-Alain Joye
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the BSD License.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Date and Time
* @package Date
* @author Leandro Lucarella <llucax@php.net>
* @author Pierre-Alain Joye <pajoye@php.net>
* @copyright 1997-2006 Leandro Lucarella, Pierre-Alain Joye
* @license http://www.opensource.org/licenses/bsd-license.php
* BSD License
* @version CVS: $Id: Span.php,v 1.9 2006/11/21 17:38:15 firman Exp $
* @link http://pear.php.net/package/Date
* @since File available since Release 1.4
*/
// }}}
// {{{ Includes
/**
* Get the Date class
*/
require_once 'Date.php';
/**
* Get the Date_Calc class
*/
require_once 'Date/Calc.php';
// }}}
// {{{ Constants
/**
* Non Numeric Separated Values (NNSV) Input Format.
*
* Input format guessed from something like this:
* days<sep>hours<sep>minutes<sep>seconds
* Where <sep> is any quantity of non numeric chars. If no values are
* given, time span is set to zero, if one value is given, it's used for
* hours, if two values are given it's used for hours and minutes and if
* three values are given, it's used for hours, minutes and seconds.<br>
* Examples:<br>
* '' -> 0, 0, 0, 0 (days, hours, minutes, seconds)<br>
* '12' -> 0, 12, 0, 0
* '12.30' -> 0, 12, 30, 0<br>
* '12:30:18' -> 0, 12, 30, 18<br>
* '3-12-30-18' -> 3, 12, 30, 18<br>
* '3 days, 12-30-18' -> 3, 12, 30, 18<br>
* '12:30 with 18 secs' -> 0, 12, 30, 18<br>
*
* @const int
*/
define('DATE_SPAN_INPUT_FORMAT_NNSV', 1);
// }}}
// {{{ Global Variables
/**
* Default time format when converting to a string.
*
* @global string
*/
$GLOBALS['_DATE_SPAN_FORMAT'] = '%C';
/**
* Default time format when converting from a string.
*
* @global mixed
*/
$GLOBALS['_DATE_SPAN_INPUT_FORMAT'] = DATE_SPAN_INPUT_FORMAT_NNSV;
// }}}
// {{{ Class: Date_Span
/**
* Generic time span handling class for PEAR
*
* @author Leandro Lucarella <llucax@php.net>
* @author Pierre-Alain Joye <pajoye@php.net>
* @copyright 1997-2006 Leandro Lucarella, Pierre-Alain Joye
* @license http://www.opensource.org/licenses/bsd-license.php
* BSD License
* @version Release: 1.4.7
* @link http://pear.php.net/package/Date
* @since Class available since Release 1.4
*/
class Date_Span
{
// {{{ Properties
/**
* @var int
*/
var $day;
/**
* @var int
*/
var $hour;
/**
* @var int
*/
var $minute;
/**
* @var int
*/
var $second;
// }}}
// {{{ Constructor
/**
* Constructor.
*
* Creates the time span object calling the set() method.
*
* @param mixed $time Time span expression.
* @param mixed $format Format string to set it from a string or the
* second date set it from a date diff.
*
* @see set()
* @access public
*/
function Date_Span($time = 0, $format = null)
{
$this->set($time, $format);
}
// }}}
// {{{ set()
/**
* Set the time span to a new value in a 'smart' way.
*
* Sets the time span depending on the argument types, calling
* to the appropriate setFromXxx() method.
*
* @param mixed $time Time span expression.
* @param mixed $format Format string to set it from a string or the
* second date set it from a date diff.
*
* @return bool true on success.
*
* @see setFromObject()
* @see setFromArray()
* @see setFromString()
* @see setFromSeconds()
* @see setFromDateDiff()
* @access public
*/
function set($time = 0, $format = null)
{
if (is_a($time, 'date_span')) {
return $this->copy($time);
} elseif (is_a($time, 'date') and is_a($format, 'date')) {
return $this->setFromDateDiff($time, $format);
} elseif (is_array($time)) {
return $this->setFromArray($time);
} elseif (is_string($time)) {
return $this->setFromString($time, $format);
} elseif (is_int($time)) {
return $this->setFromSeconds($time);
} else {
return $this->setFromSeconds(0);
}
}
// }}}
// {{{ setFromArray()
/**
* Set the time span from an array.
*
* Set the time span from an array. Any value can be a float (but it
* has no sense in seconds), for example array(23.5, 20, 0) is
* interpreted as 23 hours, .5*60 + 20 = 50 minutes and 0 seconds.
*
* @param array $time Items are counted from right to left. First
* item is for seconds, second for minutes, third
* for hours and fourth for days. If there are
* less items than 4, zero (0) is assumed for the
* absent values.
*
* @return bool True on success.
*
* @access public
*/
function setFromArray($time)
{
if (!is_array($time)) {
return false;
}
$tmp1 = new Date_Span;
if (!$tmp1->setFromSeconds(@array_pop($time))) {
return false;
}
$tmp2 = new Date_Span;
if (!$tmp2->setFromMinutes(@array_pop($time))) {
return false;
}
$tmp1->add($tmp2);
if (!$tmp2->setFromHours(@array_pop($time))) {
return false;
}
$tmp1->add($tmp2);
if (!$tmp2->setFromDays(@array_pop($time))) {
return false;
}
$tmp1->add($tmp2);
return $this->copy($tmp1);
}
// }}}
// {{{ setFromString()
/**
* Set the time span from a string based on an input format.
*
* Set the time span from a string based on an input format. This is
* some like a mix of format() method and sscanf() PHP function. The
* error checking and validation of this function is very primitive,
* so you should be carefull when using it with unknown $time strings.
* With this method you are assigning day, hour, minute and second
* values, and the last values are used. This means that if you use
* something like setFromString('10, 20', '%H, %h') your time span
* would be 20 hours long. Allways remember that this method set
* <b>all</b> the values, so if you had a $time span 30 minutes long
* and you make $time->setFromString('20 hours', '%H hours'), $time
* span would be 20 hours long (and not 20 hours and 30 minutes).
* Input format options:<br>
* <code>%C</code> Days with time, same as "%D, %H:%M:%S".<br>
* <code>%d</code> Total days as a float number
* (2 days, 12 hours = 2.5 days).<br>
* <code>%D</code> Days as a decimal number.<br>
* <code>%e</code> Total hours as a float number
* (1 day, 2 hours, 30 minutes = 26.5 hours).<br>
* <code>%f</code> Total minutes as a float number
* (2 minutes, 30 seconds = 2.5 minutes).<br>
* <code>%g</code> Total seconds as a decimal number
* (2 minutes, 30 seconds = 90 seconds).<br>
* <code>%h</code> Hours as decimal number.<br>
* <code>%H</code> Hours as decimal number limited to 2 digits.<br>
* <code>%m</code> Minutes as a decimal number.<br>
* <code>%M</code> Minutes as a decimal number limited to 2 digits.<br>
* <code>%n</code> Newline character (\n).<br>
* <code>%p</code> Either 'am' or 'pm' depending on the time. If 'pm'
* is detected it adds 12 hours to the resulting time
* span (without any checks). This is case
* insensitive.<br>
* <code>%r</code> Time in am/pm notation, same as "%H:%M:%S %p".<br>
* <code>%R</code> Time in 24-hour notation, same as "%H:%M".<br>
* <code>%s</code> Seconds as a decimal number.<br>
* <code>%S</code> Seconds as a decimal number limited to 2 digits.<br>
* <code>%t</code> Tab character (\t).<br>
* <code>%T</code> Current time equivalent, same as "%H:%M:%S".<br>
* <code>%%</code> Literal '%'.<br>
*
* @param string $time String from where to get the time span
* information.
* @param string $format Format string.
*
* @return bool True on success.
*
* @access public
*/
function setFromString($time, $format = null)
{
if (is_null($format)) {
$format = $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
}
// If format is a string, it parses the string format.
if (is_string($format)) {
$str = '';
$vars = array();
$pm = 'am';
$day = $hour = $minute = $second = 0;
for ($i = 0; $i < strlen($format); $i++) {
$char = $format{$i};
if ($char == '%') {
$nextchar = $format{++$i};
switch ($nextchar) {
case 'c':
$str .= '%d, %d:%d:%d';
array_push(
$vars, 'day', 'hour', 'minute', 'second');
break;
case 'C':
$str .= '%d, %2d:%2d:%2d';
array_push(
$vars, 'day', 'hour', 'minute', 'second');
break;
case 'd':
$str .= '%f';
array_push($vars, 'day');
break;
case 'D':
$str .= '%d';
array_push($vars, 'day');
break;
case 'e':
$str .= '%f';
array_push($vars, 'hour');
break;
case 'f':
$str .= '%f';
array_push($vars, 'minute');
break;
case 'g':
$str .= '%f';
array_push($vars, 'second');
break;
case 'h':
$str .= '%d';
array_push($vars, 'hour');
break;
case 'H':
$str .= '%2d';
array_push($vars, 'hour');
break;
case 'm':
$str .= '%d';
array_push($vars, 'minute');
break;
case 'M':
$str .= '%2d';
array_push($vars, 'minute');
break;
case 'n':
$str .= "\n";
break;
case 'p':
$str .= '%2s';
array_push($vars, 'pm');
break;
case 'r':
$str .= '%2d:%2d:%2d %2s';
array_push(
$vars, 'hour', 'minute', 'second', 'pm');
break;
case 'R':
$str .= '%2d:%2d';
array_push($vars, 'hour', 'minute');
break;
case 's':
$str .= '%d';
array_push($vars, 'second');
break;
case 'S':
$str .= '%2d';
array_push($vars, 'second');
break;
case 't':
$str .= "\t";
break;
case 'T':
$str .= '%2d:%2d:%2d';
array_push($vars, 'hour', 'minute', 'second');
break;
case '%':
$str .= "%";
break;
default:
$str .= $char . $nextchar;
}
} else {
$str .= $char;
}
}
$vals = sscanf($time, $str);
foreach ($vals as $i => $val) {
if (is_null($val)) {
return false;
}
$$vars[$i] = $val;
}
if (strcasecmp($pm, 'pm') == 0) {
$hour += 12;
} elseif (strcasecmp($pm, 'am') != 0) {
return false;
}
$this->setFromArray(array($day, $hour, $minute, $second));
// If format is a integer, it uses a predefined format
// detection method.
} elseif (is_integer($format)) {
switch ($format) {
case DATE_SPAN_INPUT_FORMAT_NNSV:
$time = preg_split('/\D+/', $time);
switch (count($time)) {
case 0:
return $this->setFromArray(
array(0, 0, 0, 0));
case 1:
return $this->setFromArray(
array(0, $time[0], 0, 0));
case 2:
return $this->setFromArray(
array(0, $time[0], $time[1], 0));
case 3:
return $this->setFromArray(
array(0, $time[0], $time[1], $time[2]));
default:
return $this->setFromArray($time);
}
break;
}
}
return false;
}
// }}}
// {{{ setFromSeconds()
/**
* Set the time span from a total number of seconds.
*
* @param int $seconds Total number of seconds.
*
* @return bool True on success.
*
* @access public
*/
function setFromSeconds($seconds)
{
if ($seconds < 0) {
return false;
}
$sec = intval($seconds);
$min = floor($sec / 60);
$hour = floor($min / 60);
$day = intval(floor($hour / 24));
$this->second = $sec % 60;
$this->minute = $min % 60;
$this->hour = $hour % 24;
$this->day = $day;
return true;
}
// }}}
// {{{ setFromMinutes()
/**
* Set the time span from a total number of minutes.
*
* @param float $minutes Total number of minutes.
*
* @return bool True on success.
*
* @access public
*/
function setFromMinutes($minutes)
{
return $this->setFromSeconds(round($minutes * 60));
}
// }}}
// {{{ setFromHours()
/**
* Set the time span from a total number of hours.
*
* @param float $hours Total number of hours.
*
* @return bool True on success.
*
* @access public
*/
function setFromHours($hours)
{
return $this->setFromSeconds(round($hours * 3600));
}
// }}}
// {{{ setFromDays()
/**
* Set the time span from a total number of days.
*
* @param float $days Total number of days.
*
* @return bool True on success.
*
* @access public
*/
function setFromDays($days)
{
return $this->setFromSeconds(round($days * 86400));
}
// }}}
// {{{ setFromDateDiff()
/**
* Set the span from the elapsed time between two dates.
*
* Set the span from the elapsed time between two dates. The time span
* is allways positive, so the date's order is not important.
*
* @param object Date $date1 First Date.
* @param object Date $date2 Second Date.
*
* @return bool True on success.
*
* @access public
*/
function setFromDateDiff($date1, $date2)
{
if (!is_a($date1, 'date') or !is_a($date2, 'date')) {
return false;
}
$date1->toUTC();
$date2->toUTC();
if ($date1->after($date2)) {
list($date1, $date2) = array($date2, $date1);
}
$days = Date_Calc::dateDiff(
$date1->getDay(), $date1->getMonth(), $date1->getYear(),
$date2->getDay(), $date2->getMonth(), $date2->getYear()
);
$hours = $date2->getHour() - $date1->getHour();
$mins = $date2->getMinute() - $date1->getMinute();
$secs = $date2->getSecond() - $date1->getSecond();
$this->setFromSeconds(
$days * 86400 + $hours * 3600 + $mins * 60 + $secs
);
return true;
}
// }}}
// {{{ copy()
/**
* Set the time span from another time object.
*
* @param object Date_Span $time Source time span object.
*
* @return bool True on success.
*
* @access public
*/
function copy($time)
{
if (is_a($time, 'date_span')) {
$this->second = $time->second;
$this->minute = $time->minute;
$this->hour = $time->hour;
$this->day = $time->day;
return true;
} else {
return false;
}
}
// }}}
// {{{ format()
/**
* Time span pretty printing (similar to Date::format()).
*
* Formats the time span in the given format, similar to
* strftime() and Date::format().<br>
* <br>
* Formatting options:<br>
* <code>%C</code> Days with time, same as "%D, %H:%M:%S".<br>
* <code>%d</code> Total days as a float number
* (2 days, 12 hours = 2.5 days).<br>
* <code>%D</code> Days as a decimal number.<br>
* <code>%e</code> Total hours as a float number
* (1 day, 2 hours, 30 minutes = 26.5 hours).<br>
* <code>%E</code> Total hours as a decimal number
* (1 day, 2 hours, 40 minutes = 26 hours).<br>
* <code>%f</code> Total minutes as a float number
* (2 minutes, 30 seconds = 2.5 minutes).<br>
* <code>%F</code> Total minutes as a decimal number
* (1 hour, 2 minutes, 40 seconds = 62 minutes).<br>
* <code>%g</code> Total seconds as a decimal number
* (2 minutes, 30 seconds = 90 seconds).<br>
* <code>%h</code> Hours as decimal number (0 to 23).<br>
* <code>%H</code> Hours as decimal number (00 to 23).<br>
* <code>%i</code> Hours as decimal number on 12-hour clock
* (1 to 12).<br>
* <code>%I</code> Hours as decimal number on 12-hour clock
* (01 to 12).<br>
* <code>%m</code> Minutes as a decimal number (0 to 59).<br>
* <code>%M</code> Minutes as a decimal number (00 to 59).<br>
* <code>%n</code> Newline character (\n).<br>
* <code>%p</code> Either 'am' or 'pm' depending on the time.<br>
* <code>%P</code> Either 'AM' or 'PM' depending on the time.<br>
* <code>%r</code> Time in am/pm notation, same as "%I:%M:%S %p".<br>
* <code>%R</code> Time in 24-hour notation, same as "%H:%M".<br>
* <code>%s</code> Seconds as a decimal number (0 to 59).<br>
* <code>%S</code> Seconds as a decimal number (00 to 59).<br>
* <code>%t</code> Tab character (\t).<br>
* <code>%T</code> Current time equivalent, same as "%H:%M:%S".<br>
* <code>%%</code> Literal '%'.<br>
*
* @param string $format The format string for returned time span.
*
* @return string The time span in specified format.
*
* @access public
*/
function format($format = null)
{
if (is_null($format)) {
$format = $GLOBALS['_DATE_SPAN_FORMAT'];
}
$output = '';
for ($i = 0; $i < strlen($format); $i++) {
$char = $format{$i};
if ($char == '%') {
$nextchar = $format{++$i};
switch ($nextchar) {
case 'C':
$output .= sprintf(
'%d, %02d:%02d:%02d',
$this->day,
$this->hour,
$this->minute,
$this->second
);
break;
case 'd':
$output .= $this->toDays();
break;
case 'D':
$output .= $this->day;
break;
case 'e':
$output .= $this->toHours();
break;
case 'E':
$output .= floor($this->toHours());
break;
case 'f':
$output .= $this->toMinutes();
break;
case 'F':
$output .= floor($this->toMinutes());
break;
case 'g':
$output .= $this->toSeconds();
break;
case 'h':
$output .= $this->hour;
break;
case 'H':
$output .= sprintf('%02d', $this->hour);
break;
case 'i':
$hour =
($this->hour + 1) > 12 ?
$this->hour - 12 :
$this->hour;
$output .= ($hour == 0) ? 12 : $hour;
break;
case 'I':
$hour =
($this->hour + 1) > 12 ?
$this->hour - 12 :
$this->hour;
$output .= sprintf('%02d', $hour==0 ? 12 : $hour);
break;
case 'm':
$output .= $this->minute;
break;
case 'M':
$output .= sprintf('%02d',$this->minute);
break;
case 'n':
$output .= "\n";
break;
case 'p':
$output .= $this->hour >= 12 ? 'pm' : 'am';
break;
case 'P':
$output .= $this->hour >= 12 ? 'PM' : 'AM';
break;
case 'r':
$hour =
($this->hour + 1) > 12 ?
$this->hour - 12 :
$this->hour;
$output .= sprintf(
'%02d:%02d:%02d %s',
$hour==0 ? 12 : $hour,
$this->minute,
$this->second,
$this->hour >= 12 ? 'pm' : 'am'
);
break;
case 'R':
$output .= sprintf(
'%02d:%02d', $this->hour, $this->minute
);
break;
case 's':
$output .= $this->second;
break;
case 'S':
$output .= sprintf('%02d', $this->second);
break;
case 't':
$output .= "\t";
break;
case 'T':
$output .= sprintf(
'%02d:%02d:%02d',
$this->hour, $this->minute, $this->second
);
break;
case '%':
$output .= "%";
break;
default:
$output .= $char . $nextchar;
}
} else {
$output .= $char;
}
}
return $output;
}
// }}}
// {{{ toSeconds()
/**
* Convert time span to seconds.
*
* @return int Time span as an integer number of seconds.
*
* @access public
*/
function toSeconds()
{
return $this->day * 86400 + $this->hour * 3600 +
$this->minute * 60 + $this->second;
}
// }}}
// {{{ toMinutes()
/**
* Convert time span to minutes.
*
* @return float Time span as a decimal number of minutes.
*
* @access public
*/
function toMinutes()
{
return $this->day * 1440 + $this->hour * 60 + $this->minute +
$this->second / 60;
}
// }}}
// {{{ toHours()
/**
* Convert time span to hours.
*
* @return float Time span as a decimal number of hours.
*
* @access public
*/
function toHours()
{
return $this->day * 24 + $this->hour + $this->minute / 60 +
$this->second / 3600;
}
// }}}
// {{{ toDays()
/**
* Convert time span to days.
*
* @return float Time span as a decimal number of days.
*
* @access public
*/
function toDays()
{
return $this->day + $this->hour / 24 + $this->minute / 1440 +
$this->second / 86400;
}
// }}}
// {{{ add()
/**
* Adds a time span.
*
* @param object Date_Span $time Time span to add.
*
* @access public
*/
function add($time)
{
return $this->setFromSeconds(
$this->toSeconds() + $time->toSeconds()
);
}
// }}}
// {{{ substract()
/**
* Subtracts a time span.
*
* Subtracts a time span. If the time span to subtract is larger
* than the original, the result is zero (there's no sense in
* negative time spans).
*
* @param object Date_Span $time Time span to subtract.
*
* @access public
*/
function subtract($time)
{
$sub = $this->toSeconds() - $time->toSeconds();
if ($sub < 0) {
$this->setFromSeconds(0);
} else {
$this->setFromSeconds($sub);
}
}
// }}}
// {{{ equal()
/**
* Tells if time span is equal to $time.
*
* @param object Date_Span $time Time span to compare to.
*
* @return bool True if the time spans are equal.
*
* @access public
*/
function equal($time)
{
return $this->toSeconds() == $time->toSeconds();
}
// }}}
// {{{ greaterEqual()
/**
* Tells if this time span is greater or equal than $time.
*
* @param object Date_Span $time Time span to compare to.
*
* @return bool True if this time span is greater or equal than $time.
*
* @access public
*/
function greaterEqual($time)
{
return $this->toSeconds() >= $time->toSeconds();
}
// }}}
// {{{ lowerEqual()
/**
* Tells if this time span is lower or equal than $time.
*
* @param object Date_Span $time Time span to compare to.
*
* @return bool True if this time span is lower or equal than $time.
*
* @access public
*/
function lowerEqual($time)
{
return $this->toSeconds() <= $time->toSeconds();
}
// }}}
// {{{ greater()
/**
* Tells if this time span is greater than $time.
*
* @param object Date_Span $time Time span to compare to.
*
* @return bool True if this time span is greater than $time.
*
* @access public
*/
function greater($time)
{
return $this->toSeconds() > $time->toSeconds();
}
// }}}
// {{{ lower()
/**
* Tells if this time span is lower than $time.
*
* @param object Date_Span $time Time span to compare to.
*
* @return bool True if this time span is lower than $time.
*
* @access public
*/
function lower($time)
{
return $this->toSeconds() < $time->toSeconds();
}
// }}}
// {{{ compare()
/**
* Compares two time spans.
*
* Compares two time spans. Suitable for use in sorting functions.
*
* @param object Date_Span $time1 The first time span.
* @param object Date_Span $time2 The second time span.
*
* @return int 0 if the time spans are equal, -1 if time1 is lower
* than time2, 1 if time1 is greater than time2.
*
* @static
* @access public
*/
function compare($time1, $time2)
{
if ($time1->equal($time2)) {
return 0;
} elseif ($time1->lower($time2)) {
return -1;
} else {
return 1;
}
}
// }}}
// {{{ isEmpty()
/**
* Tells if the time span is empty (zero length).
*
* @return bool True is it's empty.
*/
function isEmpty()
{
return !$this->day && !$this->hour && !$this->minute && !$this->second;
}
// }}}
// {{{ setDefaultInputFormat()
/**
* Set the default input format.
*
* @param mixed $format New default input format.
*
* @return mixed Previous default input format.
*
* @static
*/
function setDefaultInputFormat($format)
{
$old = $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
$GLOBALS['_DATE_SPAN_INPUT_FORMAT'] = $format;
return $old;
}
// }}}
// {{{ getDefaultInputFormat()
/**
* Get the default input format.
*
* @return mixed Default input format.
*
* @static
*/
function getDefaultInputFormat()
{
return $GLOBALS['_DATE_SPAN_INPUT_FORMAT'];
}
// }}}
// {{{ setDefaultFormat()
/**
* Set the default format.
*
* @param mixed $format New default format.
*
* @return mixed Previous default format.
*
* @static
*/
function setDefaultFormat($format)
{
$old = $GLOBALS['_DATE_SPAN_FORMAT'];
$GLOBALS['_DATE_SPAN_FORMAT'] = $format;
return $old;
}
// }}}
// {{{ getDefaultFormat()
/**
* Get the default format.
*
* @return mixed Default format.
*
* @static
*/
function getDefaultFormat()
{
return $GLOBALS['_DATE_SPAN_FORMAT'];
}
// }}}
// {{{ __clone()
/**
* Returns a copy of the object (workarround for PHP5 forward compatibility).
*
* @return object Date_Span Copy of the object.
*/
function __clone() {
$c = get_class($this);
$s = new $c;
$s->day = $this->day;
$s->hour = $this->hour;
$s->minute = $this->minute;
$s->second = $this->second;
return $s;
}
// }}}
}
// }}}
/*
* Local variables:
* mode: php
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>
|