# Time.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /****************************************************************************
    00003  *   Copyright (C) 2005-2008 by Konstantin V. Arkhipov, Anton E. Lebedevich *
    00004  *                                                                          *
    00005  *   This program is free software; you can redistribute it and/or modify   *
    00006  *   it under the terms of the GNU Lesser General Public License as         *
    00007  *   published by the Free Software Foundation; either version 3 of the     *
    00008  *   License, or (at your option) any later version.                        *
    00009  *                                                                          *
    00010  ****************************************************************************/
    00011
    00017     final class Time implements Stringable
    00018     {
    00019         private $hour   = 0;
    00020         private $minute = 0;
    00021         private $second = 0;
    00022
    00023         private $string = null;
    00024
    00028         public static function create($input)
    00029         {
    00030             return new self($input);
    00031         }
    00032
    00033         // currently supports '01:23:45', '012345', '1234', '12'
    00034         public function __construct($input)
    00035         {
    00036             if (Assert::checkInteger($input)) {
    00037                 $time = $input;
    00038             } else {
    00039                 Assert::isString($input);
    00040                 $time = explode(':', $input);
    00041             }
    00042
    00043             $lenght = strlen($input);
    00044
    00045             if (count($time) === 2) {
    00046                 $this->
    00047                     setHour($time[0])->
    00048                     setMinute($time[1]);
    00049             } elseif (count($time) === 3) {
    00050                 $this->
    00051                     setHour($time[0])->
    00052                     setMinute($time[1])->
    00053                     setSecond($time[2]);
    00054             } else {
    00055                 switch ($lenght) {
    00056                     case 1:
    00057                     case 2:
    00058
    00059                         $this->setHour(substr($input, 0, 2));
    00060                         break;
    00061
    00062                     case 3:
    00063
    00064                         $assumedHour = substr($input, 0, 2);
    00065
    00066                         if ($assumedHour > 23)
    00067                             $this->
    00068                                 setHour(substr($input, 0, 1))->
    00069                                 setMinute(substr($input, 1, 2));
    00070                         else
    00071                             $this->
    00072                                 setHour($assumedHour)->
    00073                                 setMinute(substr($input, 2, 1).'0');
    00074
    00075                         break;
    00076
    00077                     case 4:
    00078                     case 5:
    00079                     case 6:
    00080
    00081                         $this->
    00082                             setHour(substr($input, 0, 2))->
    00083                             setMinute(substr($input, 2, 2))->
    00084                             setSecond(substr($input, 4, 2));
    00085
    00086                         break;
    00087
    00088                     default:
    00089                         throw new WrongArgumentException('unknown format');
    00090                 }
    00091             }
    00092         }
    00093
    00094         public function getHour()
    00095         {
    00096             return $this->hour;
    00097         }
    00098
    00102         public function setHour($hour)
    00103         {
    00104             $hour = (int) $hour;
    00105
    00106             Assert::isTrue(
    00107                 $hour >= 0 &&
    00108                 $hour <= 24,
    00109                 'wrong hour specified'
    00110             );
    00111
    00112             $this->hour = $hour;
    00113             $this->string = null;
    00114
    00115             return $this;
    00116         }
    00117
    00118         public function getMinute()
    00119         {
    00120             return $this->minute;
    00121         }
    00122
    00126         public function setMinute($minute)
    00127         {
    00128             $minute = (int) $minute;
    00129
    00130             Assert::isTrue(
    00131                 $minute >= 0
    00132                 && $minute <= 60,
    00133
    00134                 'wrong minute specified'
    00135             );
    00136
    00137             $this->minute = $minute;
    00138             $this->string = null;
    00139
    00140             return $this;
    00141         }
    00142
    00143         public function getSecond()
    00144         {
    00145             return $this->second;
    00146         }
    00147
    00151         public function setSecond($second)
    00152         {
    00153             $second = (int) $second;
    00154
    00155             Assert::isTrue(
    00156                 $second >= 0
    00157                 && $second <= 60,
    00158
    00159                 'wrong second specified'
    00160             );
    00161
    00162             $this->second = $second;
    00163             $this->string = null;
    00164
    00165             return $this;
    00166         }
    00167
    00169         public function toString($delimiter = ':')
    00170         {
    00171             if ($this->string === null)
    00172                 $this->string =
    00173                     $this->doublize($this->hour)
    00174                     .$delimiter
    00175                     .$this->doublize($this->minute);
    00176
    00177             return $this->string;
    00178         }
    00179
    00181         public function toFullString($delimiter = ':')
    00182         {
    00183             return
    00184                 $this->toString($delimiter)
    00185                 .$delimiter.(
    00186                     $this->second
    00187                         ? $this->doublize($this->second)
    00188                         : '00'
    00189                 );
    00190         }
    00191
    00192         public function toMinutes()
    00193         {
    00194             return
    00195                 ($this->hour * 60)
    00196                 + $this->minute
    00197                 + round($this->second / 100, 0);
    00198         }
    00199
    00200         public function toSeconds()
    00201         {
    00202             return
    00203                 ($this->hour * 3600)
    00204                 + ($this->minute * 60)
    00205                 + $this->second;
    00206         }
    00207
    00208         private function doublize($int)
    00209         {
    00210             return sprintf('%02d', $int);
    00211         }
    00212     }
    00213 ?>