source: branches/version-2_13-dev/data/module/Calendar/Engine/UnixTS.php @ 23141

Revision 23141, 10.2 KB checked in by m_uehara, 11 years ago (diff)

#2275
影響が大きいため、2.13.0では対応を行わない r23125 r23128 r23133 を差し戻します。

  • Property svn:eol-style set to LF
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
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.9 2004/08/20 20:00:55 quipo Exp $
21//
22/**
23 * @package Calendar
24 * @version $Id: UnixTS.php,v 1.9 2004/08/20 20:00:55 quipo 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 */
32class 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        if ($FDOM == 0) {
280            $FDOM = $this->getDaysInWeek();
281        }
282        if ($FDOM > $firstDay) {
283            $daysInTheFirstWeek = $this->getDaysInWeek() - $FDOM + $firstDay;
284            $weeks = 1;
285        } else {
286            $daysInTheFirstWeek = $firstDay - $FDOM;
287            $weeks = 0;
288        }
289        $daysInTheFirstWeek %= $this->getDaysInWeek();
290        return (int)(ceil(($this->getDaysInMonth($y, $m) - $daysInTheFirstWeek) /
291                           $this->getDaysInWeek()) + $weeks);
292    }
293
294    /**
295     * Returns the number of the day of the week (0=sunday, 1=monday...)
296     * @param int year (2003)
297     * @param int month (9)
298     * @param int day (4)
299     * @return int weekday number
300     * @access protected
301     */
302    function getDayOfWeek($y, $m, $d)
303    {
304        $stamp = Calendar_Engine_UnixTS::dateToStamp($y,$m,$d);
305        $date = Calendar_Engine_UnixTS::stampCollection($stamp);
306        return $date[8];
307    }
308
309    /**
310     * Returns a list of integer days of the week beginning 0
311     * @param int year (2003)
312     * @param int month (9)
313     * @param int day (4)
314     * @return array (0,1,2,3,4,5,6) 1 = Monday
315     * @access protected
316     */
317    function getWeekDays($y=NULL, $m=NULL, $d=NULL)
318    {
319        return array(0, 1, 2, 3, 4, 5, 6);
320    }
321
322    /**
323     * Returns the default first day of the week
324     * @param int year (2003)
325     * @param int month (9)
326     * @param int day (4)
327     * @return int (default 1 = Monday)
328     * @access protected
329     */
330    function getFirstDayOfWeek($y=NULL, $m=NULL, $d=NULL)
331    {
332        return 1;
333    }
334
335    /**
336     * Returns the number of hours in a day
337     * @return int (24)
338     * @access protected
339     */
340    function getHoursInDay($y=null,$m=null,$d=null)
341    {
342        return 24;
343    }
344
345    /**
346     * Returns the number of minutes in an hour
347     * @return int (60)
348     * @access protected
349     */
350    function getMinutesInHour($y=null,$m=null,$d=null,$h=null)
351    {
352        return 60;
353    }
354
355    /**
356     * Returns the number of seconds in a minutes
357     * @return int (60)
358     * @access protected
359     */
360    function getSecondsInMinute($y=null,$m=null,$d=null,$h=null,$i=null)
361    {
362        return 60;
363    }
364}
365?>
Note: See TracBrowser for help on using the repository browser.