Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
 
4
/**
5
 * Contains the Calendar_Engine_PearDate class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Date and Time
31
 * @package   Calendar
32
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
33
 * @copyright 2003-2007 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: PearDate.php 269076 2008-11-15 21:41:38Z quipo $
36
 * @link      http://pear.php.net/package/Calendar
37
 */
38
 
39
/**
40
 * Load PEAR::Date class
41
 */
42
require_once 'Date.php';
43
 
44
/**
45
 * Performs calendar calculations based on the PEAR::Date class
46
 * Timestamps are in the ISO-8601 format (YYYY-MM-DD HH:MM:SS)
47
 *
48
 * @category  Date and Time
49
 * @package   Calendar
50
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
51
 * @copyright 2003-2007 Lorenzo Alberton
52
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
53
 * @link      http://pear.php.net/package/Calendar
54
 * @access protected
55
 */
56
class Calendar_Engine_PearDate /* implements Calendar_Engine_Interface */
57
{
58
    /**
59
     * Makes sure a given timestamp is only ever parsed once
60
     * Uses a static variable to prevent date() being used twice
61
     * for a date which is already known
62
     *
63
     * @param mixed $stamp Any timestamp format recognized by Pear::Date
64
     *
65
     * @return object Pear::Date object
66
     * @access protected
67
     */
68
    function stampCollection($stamp)
69
    {
70
        static $stamps = array();
71
        if (!isset($stamps[$stamp])) {
72
            $stamps[$stamp] = new Date($stamp);
73
        }
74
        return $stamps[$stamp];
75
    }
76
 
77
    /**
78
     * Returns a numeric year given a iso-8601 datetime
79
     *
80
     * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
81
     *
82
     * @return int year (e.g. 2003)
83
     * @access protected
84
     */
85
    function stampToYear($stamp)
86
    {
87
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
88
        return (int)$date->year;
89
    }
90
 
91
    /**
92
     * Returns a numeric month given a iso-8601 datetime
93
     *
94
     * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
95
     *
96
     * @return int month (e.g. 9)
97
     * @access protected
98
     */
99
    function stampToMonth($stamp)
100
    {
101
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
102
        return (int)$date->month;
103
    }
104
 
105
    /**
106
     * Returns a numeric day given a iso-8601 datetime
107
     *
108
     * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
109
     *
110
     * @return int day (e.g. 15)
111
     * @access protected
112
     */
113
    function stampToDay($stamp)
114
    {
115
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
116
        return (int)$date->day;
117
    }
118
 
119
    /**
120
     * Returns a numeric hour given a iso-8601 datetime
121
     *
122
     * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
123
     *
124
     * @return int hour (e.g. 13)
125
     * @access protected
126
     */
127
    function stampToHour($stamp)
128
    {
129
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
130
        return (int)$date->hour;
131
    }
132
 
133
    /**
134
     * Returns a numeric minute given a iso-8601 datetime
135
     *
136
     * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
137
     *
138
     * @return int minute (e.g. 34)
139
     * @access protected
140
     */
141
    function stampToMinute($stamp)
142
    {
143
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
144
        return (int)$date->minute;
145
    }
146
 
147
    /**
148
     * Returns a numeric second given a iso-8601 datetime
149
     *
150
     * @param string $stamp iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
151
     *
152
     * @return int second (e.g. 51)
153
     * @access protected
154
     */
155
    function stampToSecond($stamp)
156
    {
157
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
158
        return (int)$date->second;
159
    }
160
 
161
    /**
162
     * Returns a iso-8601 datetime
163
     *
164
     * @param int $y year (2003)
165
     * @param int $m month (9)
166
     * @param int $d day (13)
167
     * @param int $h hour (13)
168
     * @param int $i minute (34)
169
     * @param int $s second (53)
170
     *
171
     * @return string iso-8601 datetime
172
     * @access protected
173
     */
174
    function dateToStamp($y, $m, $d, $h=0, $i=0, $s=0)
175
    {
176
        $r = array();
177
        Calendar_Engine_PearDate::adjustDate($y, $m, $d, $h, $i, $s);
178
        $key = $y.$m.$d.$h.$i.$s;
179
        if (!isset($r[$key])) {
180
            $r[$key] = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
181
                                $y, $m, $d, $h, $i, $s);
182
        }
183
        return $r[$key];
184
    }
185
 
186
    /**
187
     * Set the correct date values (useful for math operations on dates)
188
     *
189
     * @param int &$y year   (2003)
190
     * @param int &$m month  (9)
191
     * @param int &$d day    (13)
192
     * @param int &$h hour   (13)
193
     * @param int &$i minute (34)
194
     * @param int &$s second (53)
195
     *
196
     * @return void
197
     * @access protected
198
     */
199
    function adjustDate(&$y, &$m, &$d, &$h, &$i, &$s)
200
    {
201
        if ($s < 0) {
202
            $m -= floor($s / 60);
203
            $s = -$s % 60;
204
        }
205
        if ($s > 60) {
206
            $m += floor($s / 60);
207
            $s %= 60;
208
        }
209
        if ($i < 0) {
210
            $h -= floor($i / 60);
211
            $i = -$i % 60;
212
        }
213
        if ($i > 60) {
214
            $h += floor($i / 60);
215
            $i %= 60;
216
        }
217
        if ($h < 0) {
218
            $d -= floor($h / 24);
219
            $h = -$h % 24;
220
        }
221
        if ($h > 24) {
222
            $d += floor($h / 24);
223
            $h %= 24;
224
        }
225
        for(; $m < 1; $y--, $m+=12);
226
        for(; $m > 12; $y++, $m-=12);
227
 
228
        while ($d < 1) {
229
            if ($m > 1) {
230
                $m--;
231
            } else {
232
                $m = 12;
233
                $y--;
234
            }
235
            $d += Date_Calc::daysInMonth($m, $y);
236
        }
237
        for ($max_days = Date_Calc::daysInMonth($m, $y); $d > $max_days; ) {
238
            $d -= $max_days;
239
            if ($m < 12) {
240
                $m++;
241
            } else {
242
                $m = 1;
243
                $y++;
244
            }
245
        }
246
    }
247
 
248
    /**
249
     * The upper limit on years that the Calendar Engine can work with
250
     *
251
     * @return int 9999
252
     * @access protected
253
     */
254
    function getMaxYears()
255
    {
256
        return 9999;
257
    }
258
 
259
    /**
260
     * The lower limit on years that the Calendar Engine can work with
261
     *
262
     * @return int 0
263
     * @access protected
264
     */
265
    function getMinYears()
266
    {
267
        return 0;
268
    }
269
 
270
    /**
271
     * Returns the number of months in a year
272
     *
273
     * @param int $y year
274
     *
275
     * @return int (12)
276
     * @access protected
277
     */
278
    function getMonthsInYear($y=null)
279
    {
280
        return 12;
281
    }
282
 
283
    /**
284
     * Returns the number of days in a month, given year and month
285
     *
286
     * @param int $y year (2003)
287
     * @param int $m month (9)
288
     *
289
     * @return int days in month
290
     * @access protected
291
     */
292
    function getDaysInMonth($y, $m)
293
    {
294
        return (int)Date_Calc::daysInMonth($m, $y);
295
    }
296
 
297
    /**
298
     * Returns numeric representation of the day of the week in a month,
299
     * given year and month
300
     *
301
     * @param int $y year (2003)
302
     * @param int $m month (9)
303
     *
304
     * @return int from 0 to 7
305
     * @access protected
306
     */
307
    function getFirstDayInMonth($y, $m)
308
    {
309
        return (int)Date_Calc::dayOfWeek(1, $m, $y);
310
    }
311
 
312
    /**
313
     * Returns the number of days in a week
314
     *
315
     * @param int $y year (2003)
316
     * @param int $m month (9)
317
     * @param int $d day (4)
318
     *
319
     * @return int (7)
320
     * @access protected
321
     */
322
    function getDaysInWeek($y=null, $m=null, $d=null)
323
    {
324
        return 7;
325
    }
326
 
327
    /**
328
     * Returns the number of the week in the year (ISO-8601), given a date
329
     *
330
     * @param int $y year (2003)
331
     * @param int $m month (9)
332
     * @param int $d day (4)
333
     *
334
     * @return int week number
335
     * @access protected
336
     */
337
    function getWeekNInYear($y, $m, $d)
338
    {
339
        //return Date_Calc::weekOfYear($d, $m, $y); //beware, Date_Calc doesn't follow ISO-8601 standard!
340
        list($nYear, $nWeek) = Date_Calc::weekOfYear4th($d, $m, $y);
341
        return $nWeek;
342
    }
343
 
344
    /**
345
     * Returns the number of the week in the month, given a date
346
     *
347
     * @param int $y        year (2003)
348
     * @param int $m        month (9)
349
     * @param int $d        day (4)
350
     * @param int $firstDay first day of the week (default: monday)
351
     *
352
     * @return int week number
353
     * @access protected
354
     */
355
    function getWeekNInMonth($y, $m, $d, $firstDay=1)
356
    {
357
        $weekEnd = ($firstDay == 0) ? $this->getDaysInWeek()-1 : $firstDay-1;
358
        $end_of_week = (int)Date_Calc::nextDayOfWeek($weekEnd, 1, $m, $y, '%e', true);
359
        $w = 1;
360
        while ($d > $end_of_week) {
361
            ++$w;
362
            $end_of_week += $this->getDaysInWeek();
363
        }
364
        return $w;
365
    }
366
 
367
    /**
368
     * Returns the number of weeks in the month
369
     *
370
     * @param int $y        year (2003)
371
     * @param int $m        month (9)
372
     * @param int $firstDay first day of the week (default: monday)
373
     *
374
     * @return int weeks number
375
     * @access protected
376
     */
377
    function getWeeksInMonth($y, $m, $firstDay=1)
378
    {
379
        $FDOM = Date_Calc::firstOfMonthWeekday($m, $y);
380
        if ($FDOM == 0) {
381
            $FDOM = $this->getDaysInWeek();
382
        }
383
        if ($FDOM > $firstDay) {
384
            $daysInTheFirstWeek = $this->getDaysInWeek() - $FDOM + $firstDay;
385
            $weeks = 1;
386
        } else {
387
            $daysInTheFirstWeek = $firstDay - $FDOM;
388
            $weeks = 0;
389
        }
390
        $daysInTheFirstWeek %= $this->getDaysInWeek();
391
        return (int)(ceil(($this->getDaysInMonth($y, $m) - $daysInTheFirstWeek) /
392
                           $this->getDaysInWeek()) + $weeks);
393
    }
394
 
395
    /**
396
     * Returns the number of the day of the week (0=sunday, 1=monday...)
397
     *
398
     * @param int $y year (2003)
399
     * @param int $m month (9)
400
     * @param int $d day (4)
401
     *
402
     * @return int weekday number
403
     * @access protected
404
     */
405
    function getDayOfWeek($y, $m, $d)
406
    {
407
        return Date_Calc::dayOfWeek($d, $m, $y);
408
    }
409
 
410
    /**
411
     * Returns a list of integer days of the week beginning 0
412
     *
413
     * @param int $y year (2003)
414
     * @param int $m month (9)
415
     * @param int $d day (4)
416
     *
417
     * @return array (0, 1, 2, 3, 4, 5, 6) 1 = Monday
418
     * @access protected
419
     */
420
    function getWeekDays($y=null, $m=null, $d=null)
421
    {
422
        return array(0, 1, 2, 3, 4, 5, 6);
423
    }
424
 
425
    /**
426
     * Returns the default first day of the week
427
     *
428
     * @param int $y year (2003)
429
     * @param int $m month (9)
430
     * @param int $d day (4)
431
     *
432
     * @return int (default 1 = Monday)
433
     * @access protected
434
     */
435
    function getFirstDayOfWeek($y=null, $m=null, $d=null)
436
    {
437
        return 1;
438
    }
439
 
440
    /**
441
     * Returns the number of hours in a day
442
     *
443
     * @param int $y year (2003)
444
     * @param int $m month (9)
445
     * @param int $d day (4)
446
     *
447
     * @return int (24)
448
     * @access protected
449
     */
450
    function getHoursInDay($y=null,$m=null,$d=null)
451
    {
452
        return 24;
453
    }
454
 
455
    /**
456
     * Returns the number of minutes in an hour
457
     *
458
     * @param int $y year (2003)
459
     * @param int $m month (9)
460
     * @param int $d day (4)
461
     * @param int $h hour
462
     *
463
     * @return int (60)
464
     * @access protected
465
     */
466
    function getMinutesInHour($y=null,$m=null,$d=null,$h=null)
467
    {
468
        return 60;
469
    }
470
 
471
    /**
472
     * Returns the number of seconds in a minutes
473
     *
474
     * @param int $y year (2003)
475
     * @param int $m month (9)
476
     * @param int $d day (4)
477
     * @param int $h hour
478
     * @param int $i minute
479
     *
480
     * @return int (60)
481
     * @access protected
482
     */
483
    function getSecondsInMinute($y=null,$m=null,$d=null,$h=null,$i=null)
484
    {
485
        return 60;
486
    }
487
 
488
    /**
489
     * Checks if the given day is the current day
490
     *
491
     * @param mixed $stamp Any timestamp format recognized by Pear::Date
492
     *
493
     * @return boolean
494
     * @access protected
495
     */
496
    function isToday($stamp)
497
    {
498
        static $today = null;
499
        if (is_null($today)) {
500
            $today = new Date();
501
        }
502
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
503
        return (   $date->day == $today->getDay()
504
                && $date->month == $today->getMonth()
505
                && $date->year == $today->getYear()
506
        );
507
    }
508
}
509
?>