Preparing to remove the bundled PHPTAL and let the PHPTAL skins (if enabled) work...
[mediawiki.git] / PHPTAL-NP-0.7.0 / libs / Types / ODate.php
blobc63f75d6df63e681ce23f2ddec1202230f2b3067
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 //
4 // Copyright (c) 2003 Laurent Bedubourg
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Authors: Laurent Bedubourg <laurent.bedubourg@free.fr>
21 //
22 // $id$
24 /**
25 * Generic date part format.
27 define('TYPES_DATE_FMT_DATE', '%1$04s-%2$02s-%3$02s');
28 /**
29 * French date part format.
31 define('TYPES_DATE_FMT_DATE_FR', '%3$02s/%2$02s/%1$04s');
32 /**
33 * Hours format.
35 define('TYPES_DATA_FMT_HOUR', '%4$02s:%5$02s:%6$02s');
37 /**
38 * Simple date manipulation object.
40 * Note:
41 * -----
43 * ODate::toString() can take a sprintf() format string as first parameter.
45 * @author Laurent Bedubourg <laurent.bedubourg@free.fr>
47 class ODate
49 var $_year = 0;
50 var $_month = 0;
51 var $_day = 0;
52 var $_hours = 0;
53 var $_minutes = 0;
54 var $_seconds = 0;
56 /**
57 * ODate constructor.
59 * This constructor is for php v4.4 compliance.
61 * @param int $y optional -- year
62 * @param int $mt optional -- month
63 * @param int $d optional -- day
64 * @param int $h optional -- hour
65 * @param int $m optional -- minutes
66 * @param int $s optional -- seconds
68 function __construct($y=0, $mt=0, $d=0,
69 $h=0, $m=0, $s=0)
71 $this->setYear($y);
72 $this->setMonth($mt);
73 $this->setDay($d);
74 $this->setHours($h);
75 $this->setMinutes($m);
76 $this->setSeconds($s);
79 /**
80 * ODate constructor.
82 * @param int $y optional -- year
83 * @param int $mt optional -- month
84 * @param int $d optional -- day
85 * @param int $h optional -- hour
86 * @param int $m optional -- minutes
87 * @param int $s optional -- seconds
89 function ODate($y=0, $mt=0, $d=0,
90 $h=0, $m=0, $s=0)
92 $this->__construct($y, $mt, $d, $h, $m, $s);
95 /**
96 * Add a date to current date.
98 function add($date)
100 $this->inc($date->getYear(),
101 $date->getMonth(),
102 $date->getDay(),
103 $date->getHours(),
104 $date->getMinutes(),
105 $date->getSeconds());
109 * Add values to this date.
111 * @param int $y optional -- year
112 * @param int $mt optional -- month
113 * @param int $d optional -- day
114 * @param int $h optional -- hour
115 * @param int $m optional -- minutes
116 * @param int $s optional -- seconds
118 function inc($y,$mt,$d,$h,$m,$s)
120 $this->addSeconds($s);
121 $this->addMinutes($m);
122 $this->addHours($h);
123 $this->addDays($d);
124 $this->addMonths($mt);
125 $this->addYears($y);
129 * Set date part.
131 * @param int $y -- year
132 * @param int $mt -- month
133 * @param int $d -- day
135 function setDate($y,$m,$d)
137 $this->setYear($y);
138 $this->setMonth($m);
139 $this->setDay($d);
143 * Set time part.
145 * @param int $h -- hour
146 * @param int $m -- minutes
147 * @param int $s -- seconds
149 function setTime($y,$m,$d)
151 $this->setHours($h);
152 $this->setMinutes($m);
153 $this->setSeconds($s);
157 * Add some years to this date.
159 * @param int $y -- year
161 function addYears($y)
163 $this->_year += $y;
167 * Add some months to this date.
169 * @param int $m -- month
171 function addMonths($m)
173 $this->_month += $m;
174 if ($this->_month > 12) {
175 $this->addYears($this->_month / 12);
176 $this->_month = $this->_month % 12;
181 * Add some days to this date.
183 * @param int $d -- days
185 function addDays($d)
187 $m = 0;
188 $this->_day += $d;
189 $m = $this->numberOfMonthDays();
190 while ($this->_day > $m) {
191 $this->addMonths(1);
192 $this->_day -= $m;
193 $m = $this->numberOfMonthDays();
198 * Add some hours to this date.
200 * @param int $h -- hours
202 function addHours($h)
204 $this->_hours += $h;
205 if ($this->_hours >= 24) {
206 $this->addDays($this->_hours / 24);
207 $this->_hours = $this->_hours % 24;
212 * Add minutes to this date.
214 * @param int $m -- minutes
216 function addMinutes($m)
218 $this->_minutes += $m;
219 if ($this->_minutes >= 60) {
220 $this->addHours($this->_minutes / 60);
221 $this->_minutes = $this->_minutes % 60;
226 * Add seconds to this date.
228 * @param int $s -- seconds
230 function addSeconds($s)
232 $this->_seconds += $s;
233 if ($this->_seconds >= 60) {
234 $this->addMinutes($this->_seconds / 60);
235 $this->_seconds = $this->_seconds % 60;
240 * Return true if bissextile year.
242 * @return boolean
244 function isBissextile()
246 if ($this->_year % 400 == 0) return true;
247 if ($this->_year % 100 == 0) return false;
248 if ($this->_year % 4 == 0) return true;
249 return false;
253 * Get the number of days in current month
255 * @return int
257 function numberOfMonthDays()
259 if ($this->_month == 2 && $this->isBissextile()) {
260 return 29;
262 $DOM = array(-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
263 return $DOM[$this->_month];
267 * Set now time values to this object.
269 function setNow()
271 $d = getdate();
272 $this->setSeconds($d['seconds']);
273 $this->setMinutes($d['minutes']);
274 $this->setHours($d['hours']);
275 $this->setDay($d['mday']);
276 $this->setMonth($d['mon']);
277 $this->setYear($d['year']);
281 * Ensure that date is correct.
283 function cleanup()
285 $this->add(0,0,0,0,0,0);
289 * Compare with another date, return < 0 if less, == 0 if equals, > 0 if
290 * greater.
292 * @param ODate $date -- date to compare with
294 * @return int (strcmp)
296 function compare($date)
298 return strcmp($this->toNumericString(),
299 $date->toNumericString());
303 * Return a concatenation of date fields (numeric date).
305 * @return string
307 function toNumericString()
309 return (int)"$this->_year$this->_month$this->_day".
310 "$this->_hours$this->_minutes$this->_seconds";
314 * Simple date format.
316 * @param string $format optional
317 * Format to pass to sprintf (ex:"%04d-%02d-%02d %02d:%02d:%02d")
319 * @return string
321 function toString($format="%04d-%02d-%02d %02d:%02d:%02d")
323 $this->cleanup();
324 return sprintf($format,
325 $this->_year,
326 $this->_month,
327 $this->_day,
328 $this->_hours,
329 $this->_minutes,
330 $this->_seconds);
335 // OTHER GETTER SETTERS
339 * Retrieve year.
340 * @return int
342 function getYear()
344 return $this->_year;
348 * Retrieve month.
349 * @return int
351 function getMonth()
353 return $this->_month;
357 * Retrieve day.
358 * @return int
360 function getDay()
362 return $this->_day;
366 * Retrieve hour.
367 * @return int
369 function getHours()
371 return $this->_hours;
375 * Retrieve minutes.
376 * @return int
378 function getMinutes()
380 return $this->_minutes;
384 * Retrieve seconds.
385 * @return int
387 function getSeconds()
389 return $this->_seconds;
393 * Set year.
394 * @param int $y -- year
396 function setYear($y)
398 $this->_year = $y;
402 * Set month.
403 * @param int $m -- month
405 function setMonth($m)
407 $this->_month = $m;
411 * Set day.
412 * @param int $d -- day
414 function setDay($d)
416 $this->_day = $d;
420 * Set hours.
421 * @param int $h -- hours
423 function setHours($h)
425 $this->_hours = $h;
429 * Set minutes.
430 * @param int $m -- minutes
432 function setMinutes($m)
434 $this->_minutes = $m;
438 * Set seconds.
439 * @param int $s -- seconds
441 function setSeconds($s)
443 $this->_seconds = $s;