Time.class.php

Go to the documentation of this file.
00001 <?php
00002 /****************************************************************************
00003  *   Copyright (C) 2005-2007 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 /* $Id: Time.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     final class Time implements Stringable
00019     {
00020         private $hour   = 0;
00021         private $minute = 0;
00022         private $second = 0;
00023         
00024         private $string = null;
00025         
00029         public static function create($input)
00030         {
00031             return new self($input);
00032         }
00033         
00034         // currently supports '01:23:45', '012345', '1234', '12'
00035         public function __construct($input)
00036         {
00037             if (Assert::checkInteger($input)) {
00038                 $time = $input;
00039             } else {
00040                 Assert::isString($input);
00041                 $time = explode(':', $input);
00042             }
00043             
00044             $lenght = strlen($input);
00045             
00046             if (count($time) === 2) {
00047                 $this->
00048                     setHour($time[0])->
00049                     setMinute($time[1]);
00050             } elseif (count($time) === 3) {
00051                 $this->
00052                     setHour($time[0])->
00053                     setMinute($time[1])->
00054                     setSecond($time[2]);
00055             } else {
00056                 switch ($lenght) {
00057                     case 1:
00058                     case 2:
00059                         
00060                         $this->setHour(substr($input, 0, 2));
00061                         break;
00062                         
00063                     case 3:
00064                         
00065                         $assumedHour = substr($input, 0, 2);
00066                         
00067                         if ($assumedHour > 12)
00068                             $this->
00069                                 setHour($input[0])->
00070                                 setMinute(substr($input, 1, 3));
00071                         else
00072                             $this->
00073                                 setHour($assumedHour)->
00074                                 setMinute($input[2].'0');
00075                         
00076                         break;
00077                     
00078                     case 4:
00079                     case 5:
00080                     case 6:
00081                         
00082                         $this->
00083                             setHour(substr($input, 0, 2))->
00084                             setMinute(substr($input, 2, 4))->
00085                             setSecond(substr($input, 4, 6));
00086                         
00087                         break;
00088                         
00089                     default:
00090                         throw new WrongArgumentException('unknown format');
00091                 }
00092             }
00093         }
00094         
00095         public function getHour()
00096         {
00097             return $this->hour;
00098         }
00099         
00103         public function setHour($hour)
00104         {
00105             $hour = (int) $hour;
00106             
00107             Assert::isTrue(
00108                 $hour >= 0 &&
00109                 $hour <= 24,
00110                 'wrong hour specified'
00111             );
00112             
00113             $this->hour = $hour;
00114             $this->string = null;
00115             
00116             return $this;
00117         }
00118         
00119         public function getMinute()
00120         {
00121             return $this->minute;
00122         }
00123         
00127         public function setMinute($minute)
00128         {
00129             $minute = (int) $minute;
00130             
00131             Assert::isTrue(
00132                 $minute >= 0
00133                 && $minute <= 60,
00134                 
00135                 'wrong minute specified'
00136             );
00137             
00138             $this->minute = $minute;
00139             $this->string = null;
00140             
00141             return $this;
00142         }
00143         
00144         public function getSecond()
00145         {
00146             return $this->second;
00147         }
00148         
00152         public function setSecond($second)
00153         {
00154             $second = (int) $second;
00155             
00156             Assert::isTrue(
00157                 $second >= 0
00158                 && $second <= 60,
00159                 
00160                 'wrong second specified'
00161             );
00162             
00163             $this->second = $second;
00164             $this->string = null;
00165             
00166             return $this;
00167         }
00168         
00170         public function toString($delimiter = ':')
00171         {
00172             if ($this->string === null)
00173                 $this->string =
00174                     $this->doublize($this->hour)
00175                     .$delimiter
00176                     .$this->doublize($this->minute);
00177             
00178             return $this->string;
00179         }
00180         
00182         public function toFullString($delimiter = ':')
00183         {
00184             return
00185                 $this->toString($delimiter)
00186                 .$delimiter.(
00187                     $this->second
00188                         ? $this->doublize($this->second)
00189                         : '00'
00190                 );
00191         }
00192         
00193         public function toMinutes()
00194         {
00195             return
00196                 ($this->hour * 60)
00197                 + $this->minute
00198                 + round($this->second / 100, 0);
00199         }
00200         
00201         public function toSeconds()
00202         {
00203             return
00204                 ($this->hour * 3600)
00205                 + ($this->minute * 60)
00206                 + $this->second;
00207         }
00208         
00209         private function doublize($int)
00210         {
00211             return sprintf('%02d', $int);
00212         }
00213     }
00214 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4