Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Details | Compare with Previous | 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: Harry Fuecks <hfuecks@phppatterns.com>                      |
18
// +----------------------------------------------------------------------+
19
//
20
// $Id: UnixTS.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
21
//
22
/**
23
 * @package Calendar
24
 * @version $Id: UnixTS.php,v 1.1 2005-09-30 14:58:00 ddelon Exp $
25
 */
26
/**
27
 * Performs calendar calculations based on the PHP date() function and
28
 * Unix timestamps (using PHP's mktime() function).
29
 * @package Calendar
30
 * @access protected
31
 */
32
class Calendar_Engine_UnixTS /* implements Calendar_Engine_Interface */
33
{
34
    /**
35
     * Makes sure a given timestamp is only ever parsed once
36
     * <pre>
37
     * array (
38
     *  [0] => year (e.g 2003),
39
     *  [1] => month (e.g 9),
40
     *  [2] => day (e.g 6),
41
     *  [3] => hour (e.g 14),
42
     *  [4] => minute (e.g 34),
43
     *  [5] => second (e.g 45),
44
     *  [6] => num days in month (e.g. 31),
45
     *  [7] => week in year (e.g. 50),
46
     *  [8] => day in week (e.g. 0 for Sunday)
47
     * )
48
     * </pre>
49
     * Uses a static variable to prevent date() being used twice
50
     * for a date which is already known
51
     * @param int Unix timestamp
52
     * @return array
53
     * @access protected
54
     */
55
    function stampCollection($stamp)
56
    {
57
        static $stamps = array();
58
        if ( !isset($stamps[$stamp]) ) {
59
            $date = @date('Y n j H i s t W w',$stamp);
60
            $stamps[$stamp] = sscanf($date, "%d %d %d %d %d %d %d %d %d");
61
        }
62
        return $stamps[$stamp];
63
    }
64
 
65
    /**
66
     * Returns a numeric year given a timestamp
67
     * @param int Unix timestamp
68
     * @return int year (e.g. 2003)
69
     * @access protected
70
     */
71
    function stampToYear($stamp)
72
    {
73
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
74
        return (int)$date[0];
75
    }
76
 
77
    /**
78
     * Returns a numeric month given a timestamp
79
     * @param int Unix timestamp
80
     * @return int month (e.g. 9)
81
     * @access protected
82
     */
83
    function stampToMonth($stamp)
84
    {
85
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
86
        return (int)$date[1];
87
    }
88
 
89
    /**
90
     * Returns a numeric day given a timestamp
91
     * @param int Unix timestamp
92
     * @return int day (e.g. 15)
93
     * @access protected
94
     */
95
    function stampToDay($stamp)
96
    {
97
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
98
        return (int)$date[2];
99
    }
100
 
101
    /**
102
     * Returns a numeric hour given a timestamp
103
     * @param int Unix timestamp
104
     * @return int hour (e.g. 13)
105
     * @access protected
106
     */
107
    function stampToHour($stamp)
108
    {
109
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
110
        return (int)$date[3];
111
    }
112
 
113
    /**
114
     * Returns a numeric minute given a timestamp
115
     * @param int Unix timestamp
116
     * @return int minute (e.g. 34)
117
     * @access protected
118
     */
119
    function stampToMinute($stamp)
120
    {
121
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
122
        return (int)$date[4];
123
    }
124
 
125
    /**
126
     * Returns a numeric second given a timestamp
127
     * @param int Unix timestamp
128
     * @return int second (e.g. 51)
129
     * @access protected
130
     */
131
    function stampToSecond($stamp)
132
    {
133
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
134
        return (int)$date[5];
135
    }
136
 
137
    /**
138
     * Returns a timestamp
139
     * @param int year (2003)
140
     * @param int month (9)
141
     * @param int day (13)
142
     * @param int hour (13)
143
     * @param int minute (34)
144
     * @param int second (53)
145
     * @return int Unix timestamp
146
     * @access protected
147
     */
148
    function dateToStamp($y, $m, $d, $h=0, $i=0, $s=0)
149
    {
150
        static $dates = array();
151
        if ( !isset($dates[$y][$m][$d][$h][$i][$s]) ) {
152
            $dates[$y][$m][$d][$h][$i][$s] = @mktime($h, $i, $s, $m, $d, $y);
153
        }
154
        return $dates[$y][$m][$d][$h][$i][$s];
155
    }
156
 
157
    /**
158
     * The upper limit on years that the Calendar Engine can work with
159
     * @return int (2037)
160
     * @access protected
161
     */
162
    function getMaxYears()
163
    {
164
        return 2037;
165
    }
166
 
167
    /**
168
     * The lower limit on years that the Calendar Engine can work with
169
     * @return int (1970 if it's Windows and 1902 for all other OSs)
170
     * @access protected
171
     */
172
    function getMinYears()
173
    {
174
        return $min = strpos(PHP_OS, 'WIN') === false ? 1902 : 1970;
175
    }
176
 
177
    /**
178
     * Returns the number of months in a year
179
     * @return int (12)
180
    * @access protected
181
     */
182
    function getMonthsInYear($y=null)
183
    {
184
        return 12;
185
    }
186
 
187
    /**
188
     * Returns the number of days in a month, given year and month
189
     * @param int year (2003)
190
     * @param int month (9)
191
     * @return int days in month
192
     * @access protected
193
     */
194
    function getDaysInMonth($y, $m)
195
    {
196
        $stamp = Calendar_Engine_UnixTS::dateToStamp($y,$m,1);
197
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
198
        return $date[6];
199
    }
200
 
201
    /**
202
     * Returns numeric representation of the day of the week in a month,
203
     * given year and month
204
     * @param int year (2003)
205
     * @param int month (9)
206
     * @return int from 0 to 6
207
     * @access protected
208
     */
209
    function getFirstDayInMonth($y, $m)
210
    {
211
        $stamp = Calendar_Engine_UnixTS::dateToStamp($y,$m,1);
212
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
213
        return $date[8];
214
    }
215
 
216
    /**
217
     * Returns the number of days in a week
218
     * @param int year (2003)
219
     * @param int month (9)
220
     * @param int day (4)
221
     * @return int (7)
222
     * @access protected
223
     */
224
    function getDaysInWeek($y=NULL, $m=NULL, $d=NULL)
225
    {
226
        return 7;
227
    }
228
 
229
    /**
230
     * Returns the number of the week in the year (ISO-8601), given a date
231
     * @param int year (2003)
232
     * @param int month (9)
233
     * @param int day (4)
234
     * @return int week number
235
     * @access protected
236
     */
237
    function getWeekNInYear($y, $m, $d)
238
    {
239
        $stamp = Calendar_Engine_UnixTS::dateToStamp($y,$m,$d);
240
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
241
        return $date[7];
242
    }
243
 
244
    /**
245
     * Returns the number of the week in the month, given a date
246
     * @param int year (2003)
247
     * @param int month (9)
248
     * @param int day (4)
249
     * @param int first day of the week (default: monday)
250
     * @return int week number
251
     * @access protected
252
     */
253
    function getWeekNInMonth($y, $m, $d, $firstDay=1)
254
    {
255
        $weekEnd = ($firstDay == 0) ? $this->getDaysInWeek()-1 : $firstDay-1;
256
        $end_of_week = 1;
257
        while (@date('w', @mktime(0, 0, 0, $m, $end_of_week, $y)) != $weekEnd) {
258
            ++$end_of_week; //find first weekend of the month
259
        }
260
        $w = 1;
261
        while ($d > $end_of_week) {
262
            ++$w;
263
            $end_of_week += $this->getDaysInWeek();
264
        }
265
        return $w;
266
    }
267
 
268
    /**
269
     * Returns the number of weeks in the month
270
     * @param int year (2003)
271
     * @param int month (9)
272
     * @param int first day of the week (default: monday)
273
     * @return int weeks number
274
     * @access protected
275
     */
276
    function getWeeksInMonth($y, $m, $firstDay=1)
277
    {
278
        $FDOM = $this->getFirstDayInMonth($y, $m);
279
 
280
        if ($FDOM > $firstDay) {
281
            $firstWeekDays = $this->getDaysInWeek() - $FDOM + $firstDay;
282
            $weeks = 1;
283
        } else {
284
            $firstWeekDays = $firstDay - $FDOM;
285
            $weeks = 0;
286
        }
287
 
288
        $firstWeekDays %= $this->getDaysInWeek();
289
 
290
        $result = (int)(ceil(($this->getDaysInMonth($y, $m) - $firstWeekDays) /
291
                           $this->getDaysInWeek()) + $weeks);
292
 
293
        // Hack - 0 as FDOM is a special case
294
        if ( $FDOM != 0 ) {
295
            return $result;
296
        } else {
297
            return $result + 1;
298
        }
299
    }
300
 
301
    /**
302
     * Returns the number of the day of the week (0=sunday, 1=monday...)
303
     * @param int year (2003)
304
     * @param int month (9)
305
     * @param int day (4)
306
     * @return int weekday number
307
     * @access protected
308
     */
309
    function getDayOfWeek($y, $m, $d)
310
    {
311
        $stamp = Calendar_Engine_UnixTS::dateToStamp($y,$m,$d);
312
        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
313
        return $date[8];
314
    }
315
 
316
    /**
317
     * Returns a list of integer days of the week beginning 0
318
     * @param int year (2003)
319
     * @param int month (9)
320
     * @param int day (4)
321
     * @return array (0,1,2,3,4,5,6) 1 = Monday
322
     * @access protected
323
     */
324
    function getWeekDays($y=NULL, $m=NULL, $d=NULL)
325
    {
326
        return array(0, 1, 2, 3, 4, 5, 6);
327
    }
328
 
329
    /**
330
     * Returns the default first day of the week
331
     * @param int year (2003)
332
     * @param int month (9)
333
     * @param int day (4)
334
     * @return int (default 1 = Monday)
335
     * @access protected
336
     */
337
    function getFirstDayOfWeek($y=NULL, $m=NULL, $d=NULL)
338
    {
339
        return 1;
340
    }
341
 
342
    /**
343
     * Returns the number of hours in a day
344
     * @return int (24)
345
     * @access protected
346
     */
347
    function getHoursInDay($y=null,$m=null,$d=null)
348
    {
349
        return 24;
350
    }
351
 
352
    /**
353
     * Returns the number of minutes in an hour
354
     * @return int (60)
355
     * @access protected
356
     */
357
    function getMinutesInHour($y=null,$m=null,$d=null,$h=null)
358
    {
359
        return 60;
360
    }
361
 
362
    /**
363
     * Returns the number of seconds in a minutes
364
     * @return int (60)
365
     * @access protected
366
     */
367
    function getSecondsInMinute($y=null,$m=null,$d=null,$h=null,$i=null)
368
    {
369
        return 60;
370
    }
371
}
372
?>