1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15:
16: class Horde_Date_Repeater_Time extends Horde_Date_Repeater
17: {
18: public $currentTime;
19: public $type;
20: public $ambiguous;
21:
22: public function __construct($time)
23: {
24: $t = str_replace(':', '', $time);
25:
26: switch (strlen($t)) {
27: case 1:
28: case 2:
29: $hours = (int)$t;
30: $this->ambiguous = true;
31: $this->type = ($hours == 12) ? 0 : $hours * 3600;
32: break;
33:
34: case 3:
35: $this->ambiguous = true;
36: $this->type = $t[0] * 3600 + (int)substr($t, 1, 2) * 60;
37: break;
38:
39: case 4:
40: $this->ambiguous = (strpos($time, ':') !== false) && ($t[0] != 0) && ((int)substr($t, 0, 2) <= 12);
41: $hours = (int)substr($t, 0, 2);
42: $this->type = ($hours == 12) ?
43: ((int)substr($t, 2, 2) * 60) :
44: ($hours * 60 * 60 + (int)substr($t, 2, 2) * 60);
45: break;
46:
47: case 5:
48: $this->ambiguous = true;
49: $this->type = $t[0] * 3600 + (int)substr($t, 1, 2) * 60 + (int)substr($t, 3, 2);
50: break;
51:
52: case 6:
53: $this->ambiguous = (strpos($time, ':') !== false) && ($t[0] != 0) && ((int)substr($t, 0, 2) <= 12);
54: $hours = (int)substr($t, 0, 2);
55: $this->type = ($hours == 12) ?
56: ((int)substr($t, 2, 2) * 60 + (int)substr($t, 4, 2)) :
57: ($hours * 60 * 60 + (int)substr($t, 2, 2) * 60 + (int)substr($t, 4, 2));
58: break;
59:
60: default:
61: throw new Horde_Date_Repeater_Exception('Time cannot exceed six digits');
62: }
63: }
64:
65: 66: 67: 68: 69:
70: public function next($pointer = 'future')
71: {
72: parent::next($pointer);
73:
74: $halfDay = 3600 * 12;
75: $fullDay = 3600 * 24;
76:
77: $first = false;
78:
79: if (!$this->currentTime) {
80: $first = true;
81: $midnight = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day));
82: $yesterdayMidnight = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day - 1));
83: $tomorrowMidnight = new Horde_Date(array('year' => $this->now->year, 'month' => $this->now->month, 'day' => $this->now->day + 1));
84:
85: if ($pointer == 'future') {
86: if ($this->ambiguous) {
87: foreach (array($midnight->add($this->type), $midnight->add($halfDay + $this->type), $tomorrowMidnight->add($this->type)) as $t) {
88: if ($t->compareDateTime($this->now) >= 0) {
89: $this->currentTime = $t;
90: break;
91: }
92: }
93: } else {
94: foreach (array($midnight->add($this->type), $tomorrowMidnight->add($this->type)) as $t) {
95: if ($t->compareDateTime($this->now) >= 0) {
96: $this->currentTime = $t;
97: break;
98: }
99: }
100: }
101: } elseif ($pointer == 'past') {
102: if ($this->ambiguous) {
103: foreach (array($midnight->add($halfDay + $this->type), $midnight->add($this->type), $yesterdayMidnight->add($this->type * 2)) as $t) {
104: if ($t->compareDateTime($this->now) <= 0) {
105: $this->currentTime = $t;
106: break;
107: }
108: }
109: } else {
110: foreach (array($midnight->add($this->type), $yesterdayMidnight->add($this->type)) as $t) {
111: if ($t->compareDateTime($this->now) <= 0) {
112: $this->currentTime = $t;
113: break;
114: }
115: }
116: }
117: }
118:
119: if (!$this->currentTime) {
120: throw new Horde_Date_Repeater_Exception('Current time cannot be null at this point');
121: }
122: }
123:
124: if (!$first) {
125: $increment = $this->ambiguous ? $halfDay : $fullDay;
126: $this->currentTime->sec += ($pointer == 'future') ? $increment : -$increment;
127: }
128:
129: return new Horde_Date_Span($this->currentTime, $this->currentTime->add(1));
130: }
131:
132: public function this($context = 'future')
133: {
134: parent::this($context);
135:
136: if ($context == 'none') {
137: $context = 'future';
138: }
139: return $this->next($context);
140: }
141:
142: public function width()
143: {
144: return 1;
145: }
146:
147: public function __toString()
148: {
149: return parent::__toString() . '-time-' . $this->type;
150: }
151:
152: }
153: