Overview

Packages

  • Date

Classes

  • Horde_Date
  • Horde_Date_Exception
  • Horde_Date_Recurrence
  • Horde_Date_Repeater
  • Horde_Date_Repeater_Day
  • Horde_Date_Repeater_DayName
  • Horde_Date_Repeater_DayPortion
  • Horde_Date_Repeater_Fortnight
  • Horde_Date_Repeater_Hour
  • Horde_Date_Repeater_Minute
  • Horde_Date_Repeater_Month
  • Horde_Date_Repeater_MonthName
  • Horde_Date_Repeater_Season
  • Horde_Date_Repeater_SeasonName
  • Horde_Date_Repeater_Second
  • Horde_Date_Repeater_Time
  • Horde_Date_Repeater_Week
  • Horde_Date_Repeater_Weekend
  • Horde_Date_Repeater_Year
  • Horde_Date_Span
  • Horde_Date_Translation
  • Horde_Date_Utils

Exceptions

  • Horde_Date_Repeater_Exception
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * See the enclosed file COPYING for license information (LGPL). If you
  6:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  7:  *
  8:  * @category Horde
  9:  * @package  Date
 10:  */
 11: 
 12: /**
 13:  * @category Horde
 14:  * @package  Date
 15:  */
 16: class Horde_Date_Repeater_DayPortion extends Horde_Date_Repeater
 17: {
 18:     /**
 19:      * 6am-12am (6 * 60 * 60, 12 * 60 * 60)
 20:      */
 21:     public static $morning = array(21600, 43200);
 22: 
 23:     /**
 24:      * 1pm-5pm (13 * 60 * 60, 17 * 60 * 60)
 25:      */
 26:     public static $afternoon = array(46800, 61200);
 27: 
 28:     /**
 29:      * 5pm-8pm (17 * 60 * 60, 20 * 60 * 60)
 30:      */
 31:     public static $evening = array(61200, 72000);
 32: 
 33:     /**
 34:      * 8pm-12pm (20 * 60 * 60, 24 * 60 * 60)
 35:      */
 36:     public static $night = array(72000, 86400);
 37: 
 38:     public $range;
 39:     public $currentSpan;
 40:     public $type;
 41: 
 42:     public function __construct($type)
 43:     {
 44:         $this->type = $type;
 45: 
 46:         if (is_int($type)) {
 47:             $this->range = array(($type * 3600), (($type + 12) * 3600));
 48:         } else {
 49:             $lookup = array(
 50:                 'am' => array(0, (12 * 3600 - 1)),
 51:                 'pm' => array((12 * 3600), (24 * 3600 - 1)),
 52:                 'morning' => self::$morning,
 53:                 'afternoon' => self::$afternoon,
 54:                 'evening' => self::$evening,
 55:                 'night' => self::$night,
 56:             );
 57:             if (!isset($lookup[$type])) {
 58:                 throw new InvalidArgumentException("Invalid type '$type' for Repeater_DayPortion");
 59:             }
 60:             $this->range = $lookup[$type];
 61:         }
 62:     }
 63: 
 64:     public function next($pointer = 'future')
 65:     {
 66:         parent::next($pointer);
 67: 
 68:         if (!$this->currentSpan) {
 69:             $nowSeconds = $this->now->hour * 3600 + $this->now->min * 60 + $this->now->sec;
 70:             if ($nowSeconds < $this->range[0]) {
 71:                 switch ($pointer) {
 72:                 case 'future':
 73:                     $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'sec' => $this->range[0]));
 74:                     break;
 75: 
 76:                 case 'past':
 77:                     $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day - 1, 'sec' => $this->range[0]));
 78:                     break;
 79:                 }
 80:             } elseif ($nowSeconds > $this->range[1]) {
 81:                 switch ($pointer) {
 82:                 case 'future':
 83:                     $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1, 'sec' => $this->range[0]));
 84:                     break;
 85: 
 86:                 case 'past':
 87:                     $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'sec' => $this->range[0]));
 88:                     break;
 89:                 }
 90:             } else {
 91:                 switch ($pointer) {
 92:                 case 'future':
 93:                     $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1, 'sec' => $this->range[0]));
 94:                     break;
 95: 
 96:                 case 'past':
 97:                     $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day - 1, 'sec' => $this->range[0]));
 98:                     break;
 99:                 }
100:             }
101: 
102:             $rangeEnd = $rangeStart->add($this->range[1] - $this->range[0]);
103:             $this->currentSpan = new Horde_Date_Span($rangeStart, $rangeEnd);
104:         } else {
105:             switch ($pointer) {
106:             case 'future':
107:                 $this->currentSpan = $this->currentSpan->add(array('day' => 1));
108:                 break;
109: 
110:             case 'past':
111:                 $this->currentSpan = $this->currentSpan->add(array('day' => -1));
112:                 break;
113:             }
114:         }
115: 
116:         return $this->currentSpan;
117:     }
118: 
119:     public function this($context = 'future')
120:     {
121:         parent::this($context);
122: 
123:         $rangeStart = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day, 'sec' => $this->range[0]));
124:         $this->currentSpan = new Horde_Date_Span($rangeStart, $rangeStart->add($this->range[1] - $this->range[0]));
125:         return $this->currentSpan;
126:     }
127: 
128:     public function offset($span, $amount, $pointer)
129:     {
130:         $this->now = $span->begin;
131:         $portionSpan = $this->next($pointer);
132:         $direction = ($pointer == 'future') ? 1 : -1;
133:         return $portionSpan->add(array('day' => $direction * ($amount - 1)));
134:     }
135: 
136:     public function width()
137:     {
138:         if (!$this->range) {
139:             throw new Horde_Date_Repeater_Exception('Range has not been set');
140:         }
141: 
142:         if ($this->currentSpan) {
143:             return $this->currentSpan->width();
144:         }
145: 
146:         if (is_int($this->type)) {
147:             return (12 * 3600);
148:         } else {
149:             return $this->range[1] - $this->range[0];
150:         }
151:     }
152: 
153:     public function __toString()
154:     {
155:         return parent::__toString() . '-dayportion-' . $this->type;
156:     }
157: 
158: }
159: 
API documentation generated by ApiGen