ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDateTime.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once('Services/Calendar/classes/class.ilDateTimeException.php');
25 include_once('Services/Calendar/classes/class.ilTimeZone.php');
26 
36 define('IL_CAL_DATETIME',1);
37 define('IL_CAL_DATE',2);
38 define('IL_CAL_UNIX',3);
39 define('IL_CAL_FKT_DATE',4);
40 define('IL_CAL_FKT_GETDATE',5);
41 define('IL_CAL_TIMESTAMP',6);
42 
43 define('IL_CAL_YEAR','year');
44 define('IL_CAL_MONTH','month');
45 define('IL_CAL_WEEK','week');
46 define('IL_CAL_DAY','day');
47 define('IL_CAL_HOUR','hour');
48 
50 {
51  const YEAR = 'year';
52  const MONTH = 'month';
53  const WEEK = 'week';
54  const DAY = 'day';
55  const HOUR = 'hour';
56 
57  protected $log;
58 
59  protected $timezone = null;
60  protected $default_timezone = null;
61 
62  protected $unix = 0;
63 
64 
65 
76  public function __construct($a_date = null,$a_format = 0,$a_tz = '')
77  {
78  global $ilLog;
79 
80  $this->log = $ilLog;
81 
82  try
83  {
84  $this->timezone = ilTimeZone::_getInstance($a_tz);
85  $this->default_timezone = ilTimeZone::_getInstance('');
86 
87  if(!$a_date)
88  {
89  $this->setDate(0,IL_CAL_UNIX);
90  }
91  else
92  {
93  $this->setDate($a_date,$a_format);
94  }
95  }
96  catch(ilTimeZoneException $exc)
97  {
98  $this->log->write(__METHOD__.': '.$exc->getMessage());
99  throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_tz);
100  }
101  }
102 
108  public function isNull()
109  {
110  return $this->unix ? false : true;
111  }
112 
120  public function switchTimeZone($a_timezone_identifier = '')
121  {
122  try
123  {
124  $this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
125  return true;
126  }
127  catch(ilTimeZoneException $e)
128  {
129  $this->log->write('Unsupported timezone given: '.$a_timezone_identifier);
130  throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_timezone_identifier);
131  }
132  }
133 
140  public function getTimeZoneIdentifier()
141  {
142  return $this->timezone->getIdentifier();
143  }
144 
159  public static function _before(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
160  {
161  switch($a_compare_field)
162  {
163  case IL_CAL_YEAR:
164  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
165 
166  case IL_CAL_MONTH:
167  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
168 
169  case IL_CAL_DAY:
170  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
171 
172  case '':
173  default:
174  return $start->get(IL_CAL_UNIX) < $end->get(IL_CAL_UNIX);
175 
176  }
177  }
178 
190  public static function _equals(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
191  {
192  switch($a_compare_field)
193  {
194  case IL_CAL_YEAR:
195  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
196 
197  case IL_CAL_MONTH:
198  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
199 
200  case IL_CAL_DAY:
201  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
202 
203  case '':
204  default:
205  return $start->get(IL_CAL_UNIX) == $end->get(IL_CAL_UNIX);
206 
207  }
208  }
209 
222  public static function _after(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
223  {
224  switch($a_compare_field)
225  {
226  case IL_CAL_YEAR:
227  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
228 
229  case IL_CAL_MONTH:
230  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
231 
232  case IL_CAL_DAY:
233  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
234 
235  case '':
236  default:
237  return $start->get(IL_CAL_UNIX) > $end->get(IL_CAL_UNIX);
238 
239  }
240  }
241 
250  public function increment($a_type,$a_count = 1)
251  {
252  $count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
253 
254  $this->timezone->switchTZ();
255  switch($a_type)
256  {
257  case self::YEAR:
258  $this->unix = strtotime($count_str.'year',$this->unix);
259  break;
260 
261  case self::MONTH:
262  // strtotime fails in the following case:
263  // 2008-03-31 (+1 month) => 2008-05-01
264  // In that case, we substract the new month day
265 
266  $old_day_of_month = $this->get(IL_CAL_FKT_DATE,'j');
267  $this->unix = strtotime($count_str.'month',$this->unix);
268 
269  // TODO: Fix monthly calculations.
270 
271  /*
272  $new_day_of_month = $this->get(IL_CAL_FKT_DATE,'j');
273  if($new_day_of_month != $old_day_of_month)
274  {
275  $this->unix = $this->increment(IL_CAL_DAY,$new_day_of_month * -1);
276  }
277  */
278  break;
279 
280  case self::WEEK:
281  $this->unix = strtotime($count_str.'week',$this->unix);
282  break;
283 
284  case self::DAY:
285  $this->unix = strtotime($count_str.'day',$this->unix);
286  break;
287 
288  case self::HOUR:
289  $this->unix = strtotime($count_str.'hour',$this->unix);
290  break;
291 
292  }
293  $this->timezone->restoreTZ();
294  return $this->unix;
295  }
296 
303  public function getUnixTime()
304  {
305  return $this->unix;
306  }
307 
308 
315  public function getUTCOffset()
316  {
317  $this->timezone->switchTZ();
318  // TODO: This is wrong: calculate UTC offset of given date
319  $offset = mktime(0,0,0,2,1,1970) - gmmktime(0,0,0,2,1,1970);
320  $this->timezone->restoreTZ();
321  return $offset;
322  }
323 
332  public function setDate($a_date,$a_format)
333  {
334  switch($a_format)
335  {
336  case IL_CAL_UNIX:
337  $this->unix = $a_date;
338  break;
339 
340  case IL_CAL_DATETIME:
341 
342  if(preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)$/i',$a_date,$d_parts) === false)
343  {
344  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
345  throw new ilDateTimeException('Cannot parse date.');
346  }
347 
348  $this->timezone->switchTZ();
349  $this->unix = mktime(
350  isset($d_parts[5]) ? $d_parts[5] : 0,
351  isset($d_parts[6]) ? $d_parts[6] : 0,
352  isset($d_parts[7]) ? $d_parts[7] : 0,
353  $d_parts[2],
354  $d_parts[3],
355  $d_parts[1]);
356 
357  if($d_parts[0] == '0000-00-00 00:00:00')
358  {
359  $this->unix = 0;
360  }
361 
362  $this->timezone->restoreTZ();
363  break;
364 
365  case IL_CAL_DATE:
366  // Pure dates are not timezone sensible.
368  $timezone->switchTZ();
369  $unix = strtotime($a_date);
370  $timezone->restoreTZ();
371  if(!$unix or $unix == false)
372  {
373  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
374  $this->unix = 0;
375  return false;
376  }
377  $this->unix = $unix;
378  break;
379 
380  case IL_CAL_FKT_GETDATE:
381  // Format like getdate parameters
382  $this->timezone->switchTZ();
383  $this->unix = mktime(
384  $a_date['hours'],
385  $a_date['minutes'],
386  $a_date['seconds'],
387  $a_date['mon'],
388  $a_date['mday'],
389  $a_date['year']);
390  $this->timezone->restoreTZ();
391 
392  // TODO: choose better error handling
393  if(!$a_date['year'])
394  {
395  $this->unix = 0;
396  }
397  break;
398 
399  case IL_CAL_TIMESTAMP:
400  if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
401  {
402  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
403  throw new ilDateTimeException('Cannot parse date.');
404  }
405  $this->timezone->switchTZ();
406  $this->unix = mktime(
407  isset($d_parts[4]) ? $d_parts[4] : 0,
408  isset($d_parts[5]) ? $d_parts[5] : 0,
409  isset($d_parts[6]) ? $d_parts[6] : 0,
410  $d_parts[2],
411  $d_parts[3],
412  $d_parts[1]);
413 
414  if($d_parts[0] == '00000000000000' or
415  $d_parts[0] == '00000000')
416  {
417  $this->unix = 0;
418  }
419  $this->timezone->restoreTZ();
420  break;
421  }
422  return true;
423  }
424 
433  public function get($a_format,$a_format_str = '',$a_tz = '')
434  {
435  if($a_tz)
436  {
437  try
438  {
440  }
441  catch(ilTimeZoneException $exc)
442  {
443  $this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
444  }
445  }
446  else
447  {
448  #$timezone = $this->timezone;
450  }
451 
452  switch($a_format)
453  {
454  case IL_CAL_UNIX:
455  $date = $this->getUnixTime();
456  break;
457 
458  case IL_CAL_DATE:
459  $timezone->switchTZ();
460  $date = date('Y-m-d',$this->getUnixTime());
461  $timezone->restoreTZ();
462  break;
463 
464  case IL_CAL_DATETIME:
465  $timezone->switchTZ();
466  $date = date('Y-m-d H:i:s',$this->getUnixTime());
467  $timezone->restoreTZ();
468  break;
469 
470  case IL_CAL_FKT_DATE:
471  $timezone->switchTZ();
472  $date = date($a_format_str,$this->getUnixTime());
473  $timezone->restoreTZ();
474  break;
475 
476  case IL_CAL_FKT_GETDATE:
477  $timezone->switchTZ();
478  $date = getdate($this->getUnixTime());
479  $timezone->restoreTZ();
480 
481  // add iso 8601 week day number (Sunday = 7)
482  $date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
483  break;
484 
485  case IL_CAL_TIMESTAMP:
486  $timezone->switchTZ();
487  $date = date('YmdHis',$this->getUnixTime());
488  $timezone->restoreTZ();
489  break;
490  }
491  return $date;
492  }
493 
501  public function __toString()
502  {
503  return $this->get(IL_CAL_DATETIME,'','UTC').'<br>';
504  }
505 }
506 ?>