Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
493 ddelon 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
//
4
// +----------------------------------------------------------------------+
5
// | PHP Version 4                                                        |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 1997-2002 The PHP Group                                |
8
// +----------------------------------------------------------------------+
9
// | This source file is subject to version 2.02 of the PHP license,      |
10
// | that is bundled with this package in the file LICENSE, and is        |
11
// | available at through the world-wide-web at                           |
12
// | http://www.php.net/license/3_0.txt.                                  |
13
// | If you did not receive a copy of the PHP license and are unable to   |
14
// | obtain it through the world-wide-web, please send a note to          |
15
// | license@php.net so we can mail you a copy immediately.               |
16
// +----------------------------------------------------------------------+
17
// | Authors: Lorenzo Alberton <l dot alberton at quipo dot it>           |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: PearDate.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
21
//
22
/**
23
 * @package Calendar
24
 * @version $Id: PearDate.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
25
 */
26
/**
27
 * Load PEAR::Date class
28
 */
29
require_once 'Date.php';
30
 
31
/**
32
 * Performs calendar calculations based on the PEAR::Date class
33
 * Timestamps are in the ISO-8601 format (YYYY-MM-DD HH:MM:SS)
34
 * @package Calendar
35
 * @access protected
36
 */
37
class Calendar_Engine_PearDate /* implements Calendar_Engine_Interface */
38
{
39
    /**
40
     * Makes sure a given timestamp is only ever parsed once
41
     * Uses a static variable to prevent date() being used twice
42
     * for a date which is already known
43
     * @param mixed Any timestamp format recognized by Pear::Date
44
     * @return object Pear::Date object
45
     * @access protected
46
     */
47
    function stampCollection($stamp)
48
    {
49
        static $stamps = array();
50
        if (!isset($stamps[$stamp])) {
51
            $stamps[$stamp] = new Date($stamp);
52
        }
53
        return $stamps[$stamp];
54
    }
55
 
56
    /**
57
     * Returns a numeric year given a iso-8601 datetime
58
     * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
59
     * @return int year (e.g. 2003)
60
     * @access protected
61
     */
62
    function stampToYear($stamp)
63
    {
64
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
65
        return (int)$date->year;
66
    }
67
 
68
    /**
69
     * Returns a numeric month given a iso-8601 datetime
70
     * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
71
     * @return int month (e.g. 9)
72
     * @access protected
73
     */
74
    function stampToMonth($stamp)
75
    {
76
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
77
        return (int)$date->month;
78
    }
79
 
80
    /**
81
     * Returns a numeric day given a iso-8601 datetime
82
     * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
83
     * @return int day (e.g. 15)
84
     * @access protected
85
     */
86
    function stampToDay($stamp)
87
    {
88
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
89
        return (int)$date->day;
90
    }
91
 
92
    /**
93
     * Returns a numeric hour given a iso-8601 datetime
94
     * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
95
     * @return int hour (e.g. 13)
96
     * @access protected
97
     */
98
    function stampToHour($stamp)
99
    {
100
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
101
        return (int)$date->hour;
102
    }
103
 
104
    /**
105
     * Returns a numeric minute given a iso-8601 datetime
106
     * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
107
     * @return int minute (e.g. 34)
108
     * @access protected
109
     */
110
    function stampToMinute($stamp)
111
    {
112
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
113
        return (int)$date->minute;
114
    }
115
 
116
    /**
117
     * Returns a numeric second given a iso-8601 datetime
118
     * @param string iso-8601 datetime (YYYY-MM-DD HH:MM:SS)
119
     * @return int second (e.g. 51)
120
     * @access protected
121
     */
122
    function stampToSecond($stamp)
123
    {
124
        $date = Calendar_Engine_PearDate::stampCollection($stamp);
125
        return (int)$date->second;
126
    }
127
 
128
    /**
129
     * Returns a iso-8601 datetime
130
     * @param int year (2003)
131
     * @param int month (9)
132
     * @param int day (13)
133
     * @param int hour (13)
134
     * @param int minute (34)
135
     * @param int second (53)
136
     * @return string iso-8601 datetime
137
     * @access protected
138
     */
139
    function dateToStamp($y, $m, $d, $h=0, $i=0, $s=0)
140
    {
141
        $r = array();
142
        Calendar_Engine_PearDate::adjustDate($y, $m, $d, $h, $i, $s);
143
        $key = $y.$m.$d.$h.$i.$s;
144
        if (!isset($r[$key])) {
145
            $r[$key] = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
146
                                $y, $m, $d, $h, $i, $s);
147
        }
148
        return $r[$key];
149
    }
150
 
151
    /**
152
     * Set the correct date values (useful for math operations on dates)
153
     * @param int year   (2003)
154
     * @param int month  (9)
155
     * @param int day    (13)
156
     * @param int hour   (13)
157
     * @param int minute (34)
158
     * @param int second (53)
159
     * @access protected
160
     */
161
    function adjustDate(&$y, &$m, &$d, &$h, &$i, &$s)
162
    {
163
        if ($s < 0) {
164
            $m -= floor($s / 60);
165
            $s = -$s % 60;
166
        }
167
        if ($s > 60) {
168
            $m += floor($s / 60);
169
            $s %= 60;
170
        }
171
        if ($i < 0) {
172
            $h -= floor($i / 60);
173
            $i = -$i % 60;
174
        }
175
        if ($i > 60) {
176
            $h += floor($i / 60);
177
            $i %= 60;
178
        }
179
        if ($h < 0) {
180
            $d -= floor($h / 24);
181
            $h = -$h % 24;
182
        }
183
        if ($h > 24) {
184
            $d += floor($h / 24);
185
            $h %= 24;
186
        }
187
        for(; $m < 1; $y--, $m+=12);
188
        for(; $m > 12; $y++, $m-=12);
189
 
190
        while ($d < 1) {
191
            if ($m > 1) {
192
                $m--;
193
            } else {
194
                $m = 12;
195
                $y--;
196
            }
197
            $d += Date_Calc::daysInMonth($m, $y);
198
        }
199
        for ($max_days = Date_Calc::daysInMonth($m, $y); $d > $max_days; ) {
200
            $d -= $max_days;
201
            if ($m < 12) {
202
                $m++;
203
            } else {
204
                $m = 1;
205
                $y++;
206
            }
207
        }
208
    }
209
 
210
    /**
211
     * The upper limit on years that the Calendar Engine can work with
212
     * @return int 9999
213
     * @access protected
214
     */
215
    function getMaxYears()
216
    {
217
        return 9999;
218
    }
219
 
220
    /**
221
     * The lower limit on years that the Calendar Engine can work with
222
     * @return int 0
223
     * @access protected
224
     */
225
    function getMinYears()
226
    {
227
        return 0;
228
    }
229
 
230
    /**
231
     * Returns the number of months in a year
232
     * @return int (12)
233
     * @access protected
234
     */
235
    function getMonthsInYear($y=null)
236
    {
237
        return 12;
238
    }
239
 
240
    /**
241
     * Returns the number of days in a month, given year and month
242
     * @param int year (2003)
243
     * @param int month (9)
244
     * @return int days in month
245
     * @access protected
246
     */
247
    function getDaysInMonth($y, $m)
248
    {
249
        return (int)Date_Calc::daysInMonth($m, $y);
250
    }
251
 
252
    /**
253
     * Returns numeric representation of the day of the week in a month,
254
     * given year and month
255
     * @param int year (2003)
256
     * @param int month (9)
257
     * @return int from 0 to 7
258
     * @access protected
259
     */
260
    function getFirstDayInMonth($y, $m)
261
    {
262
        return (int)Date_Calc::dayOfWeek(1, $m, $y);
263
    }
264
 
265
    /**
266
     * Returns the number of days in a week
267
     * @param int year (2003)
268
     * @param int month (9)
269
     * @param int day (4)
270
     * @return int (7)
271
     * @access protected
272
     */
273
    function getDaysInWeek($y=NULL, $m=NULL, $d=NULL)
274
    {
275
        return 7;
276
    }
277
 
278
    /**
279
     * Returns the number of the week in the year (ISO-8601), given a date
280
     * @param int year (2003)
281
     * @param int month (9)
282
     * @param int day (4)
283
     * @return int week number
284
     * @access protected
285
     */
286
    function getWeekNInYear($y, $m, $d)
287
    {
288
        return Date_Calc::weekOfYear($d, $m, $y); //beware, Date_Calc doesn't follow ISO-8601 standard!
289
    }
290
 
291
    /**
292
     * Returns the number of the week in the month, given a date
293
     * @param int year (2003)
294
     * @param int month (9)
295
     * @param int day (4)
296
     * @param int first day of the week (default: monday)
297
     * @return int week number
298
     * @access protected
299
     */
300
    function getWeekNInMonth($y, $m, $d, $firstDay=1)
301
    {
302
        $weekEnd = ($firstDay == 0) ? $this->getDaysInWeek()-1 : $firstDay-1;
303
        $end_of_week = (int)Date_Calc::nextDayOfWeek($weekEnd, 1, $m, $y, '%e', true);
304
        $w = 1;
305
        while ($d > $end_of_week) {
306
           ++$w;
307
            $end_of_week += $this->getDaysInWeek();
308
        }
309
        return $w;
310
    }
311
 
312
    /**
313
     * Returns the number of weeks in the month
314
     * @param int year (2003)
315
     * @param int month (9)
316
     * @param int first day of the week (default: monday)
317
     * @return int weeks number
318
     * @access protected
319
     */
320
    function getWeeksInMonth($y, $m, $firstDay=1)
321
    {
322
        $FDOM = Date_Calc::firstOfMonthWeekday($m, $y);
323
 
324
        if ($FDOM > $firstDay) {
325
            $firstWeekDays = $this->getDaysInWeek() - $FDOM + $firstDay;
326
            $weeks = 1;
327
        } else {
328
            $firstWeekDays = $firstDay - $FDOM;
329
            $weeks = 0;
330
        }
331
 
332
        $firstWeekDays %= $this->getDaysInWeek();
333
 
334
        $result = (int)(ceil(($this->getDaysInMonth($y, $m) - $firstWeekDays) /
335
                           $this->getDaysInWeek()) + $weeks);
336
 
337
        if ( $FDOM != 0 ) {
338
            return $result;
339
        } else {
340
            return $result + 1;
341
        }
342
    }
343
 
344
    /**
345
     * Returns the number of the day of the week (0=sunday, 1=monday...)
346
     * @param int year (2003)
347
     * @param int month (9)
348
     * @param int day (4)
349
     * @return int weekday number
350
     * @access protected
351
     */
352
    function getDayOfWeek($y, $m, $d)
353
    {
354
        return Date_Calc::dayOfWeek($d, $m, $y);
355
    }
356
 
357
    /**
358
     * Returns a list of integer days of the week beginning 0
359
     * @param int year (2003)
360
     * @param int month (9)
361
     * @param int day (4)
362
     * @return array (0, 1, 2, 3, 4, 5, 6) 1 = Monday
363
     * @access protected
364
     */
365
    function getWeekDays($y=NULL, $m=NULL, $d=NULL)
366
    {
367
        return array(0, 1, 2, 3, 4, 5, 6);
368
    }
369
 
370
   /**
371
     * Returns the default first day of the week
372
     * @param int year (2003)
373
     * @param int month (9)
374
     * @param int day (4)
375
     * @return int (default 1 = Monday)
376
     * @access protected
377
     */
378
    function getFirstDayOfWeek($y=NULL, $m=NULL, $d=NULL)
379
    {
380
        return 1;
381
    }
382
 
383
    /**
384
     * Returns the number of hours in a day
385
     * @return int (24)
386
     * @access protected
387
     */
388
    function getHoursInDay($y=null,$m=null,$d=null)
389
    {
390
        return 24;
391
    }
392
 
393
    /**
394
     * Returns the number of minutes in an hour
395
     * @return int (60)
396
     * @access protected
397
     */
398
    function getMinutesInHour($y=null,$m=null,$d=null,$h=null)
399
    {
400
        return 60;
401
    }
402
 
403
    /**
404
     * Returns the number of seconds in a minutes
405
     * @return int (60)
406
     * @access protected
407
     */
408
    function getSecondsInMinute($y=null,$m=null,$d=null,$h=null,$i=null)
409
    {
410
        return 60;
411
    }
412
}
413
?>