ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDateTime.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once('Services/Calendar/classes/class.ilDateTimeException.php');
6 include_once('Services/Calendar/classes/class.ilTimeZone.php');
7 
8 
9 define('IL_CAL_DATETIME',1);
10 define('IL_CAL_DATE',2);
11 define('IL_CAL_UNIX',3);
12 define('IL_CAL_FKT_DATE',4);
13 define('IL_CAL_FKT_GETDATE',5);
14 define('IL_CAL_TIMESTAMP',6);
15 
16 define('IL_CAL_YEAR','year');
17 define('IL_CAL_MONTH','month');
18 define('IL_CAL_WEEK','week');
19 define('IL_CAL_DAY','day');
20 define('IL_CAL_HOUR','hour');
21 
22 
32 {
33  const YEAR = 'year';
34  const MONTH = 'month';
35  const WEEK = 'week';
36  const DAY = 'day';
37  const HOUR = 'hour';
38  const MINUTE = 'minute';
39 
40  protected $log;
41 
42  protected $timezone = null;
43  protected $default_timezone = null;
44 
45  protected $unix = 0;
46 
47 
48 
59  public function __construct($a_date = null,$a_format = 0,$a_tz = '')
60  {
61  global $ilLog;
62 
63  $this->log = $ilLog;
64 
65  try
66  {
67  $this->timezone = ilTimeZone::_getInstance($a_tz);
68  $this->default_timezone = ilTimeZone::_getInstance('');
69 
70  if(!$a_date)
71  {
72  $this->setDate(0,IL_CAL_UNIX);
73  }
74  else
75  {
76  $this->setDate($a_date,$a_format);
77  }
78  }
79  catch(ilTimeZoneException $exc)
80  {
81  $this->log->write(__METHOD__.': '.$exc->getMessage());
82  throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_tz);
83  }
84  }
85 
91  public function isNull()
92  {
93  return $this->unix ? false : true;
94  }
95 
103  public function switchTimeZone($a_timezone_identifier = '')
104  {
105  try
106  {
107  $this->timezone = ilTimeZone::_getInstance($a_timezone_identifier);
108  return true;
109  }
110  catch(ilTimeZoneException $e)
111  {
112  $this->log->write('Unsupported timezone given: '.$a_timezone_identifier);
113  throw new ilDateTimeException('Unsupported timezone given. Timezone: '.$a_timezone_identifier);
114  }
115  }
116 
123  public function getTimeZoneIdentifier()
124  {
125  return $this->timezone->getIdentifier();
126  }
127 
142  public static function _before(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
143  {
144  switch($a_compare_field)
145  {
146  case IL_CAL_YEAR:
147  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
148 
149  case IL_CAL_MONTH:
150  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
151 
152  case IL_CAL_DAY:
153  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) < $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
154 
155  case '':
156  default:
157  return $start->get(IL_CAL_UNIX) < $end->get(IL_CAL_UNIX);
158 
159  }
160  }
161 
173  public static function _equals(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
174  {
175  switch($a_compare_field)
176  {
177  case IL_CAL_YEAR:
178  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
179 
180  case IL_CAL_MONTH:
181  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
182 
183  case IL_CAL_DAY:
184  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) == $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
185 
186  case '':
187  default:
188  return $start->get(IL_CAL_UNIX) == $end->get(IL_CAL_UNIX);
189 
190  }
191  }
192 
205  public static function _after(ilDateTime $start,ilDateTime $end,$a_compare_field = '',$a_tz = '')
206  {
207  switch($a_compare_field)
208  {
209  case IL_CAL_YEAR:
210  return $start->get(IL_CAL_FKT_DATE,'Y',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Y',$a_tz);
211 
212  case IL_CAL_MONTH:
213  return (int) $start->get(IL_CAL_FKT_DATE,'Ym',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ym',$a_tz);
214 
215  case IL_CAL_DAY:
216  return (int) $start->get(IL_CAL_FKT_DATE,'Ymd',$a_tz) > $end->get(IL_CAL_FKT_DATE,'Ymd',$a_tz);
217 
218  case '':
219  default:
220  return $start->get(IL_CAL_UNIX) > $end->get(IL_CAL_UNIX);
221 
222  }
223  }
224 
233  public static function _within(ilDateTime $dt, ilDateTime $start, ilDateTime $end, $a_compare_field = '', $a_tz = '')
234  {
235  return
236  (ilDateTime::_after($dt, $start,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $start,$a_compare_field,$a_tz)) &&
237  (ilDateTime::_before($dt, $end,$a_compare_field,$a_tz) or ilDateTime::_equals($dt, $end,$a_compare_field,$a_tz));
238  }
239 
248  public function increment($a_type,$a_count = 1)
249  {
250  $count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
251 
252  $this->timezone->switchTZ();
253  switch($a_type)
254  {
255  case self::YEAR:
256  $this->unix = strtotime($count_str.'year',$this->unix);
257  break;
258 
259  case self::MONTH:
260  $this->unix = strtotime($count_str.'month',$this->unix);
261  break;
262 
263  case self::WEEK:
264  $this->unix = strtotime($count_str.'week',$this->unix);
265  break;
266 
267  case self::DAY:
268  $this->unix = strtotime($count_str.'day',$this->unix);
269  break;
270 
271  case self::HOUR:
272  $this->unix = strtotime($count_str.'hour',$this->unix);
273  break;
274 
275  case self::MINUTE:
276 
277  $this->unix = strtotime($count_str.'minute',$this->unix);
278  $d = new ilDateTime($this->unix,IL_CAL_UNIX);
279 
280 
281  break;
282 
283  }
284  $this->timezone->restoreTZ();
285  return $this->unix;
286  }
287 
294  public function getUnixTime()
295  {
296  return $this->unix;
297  }
298 
299 
306  public function getUTCOffset()
307  {
308  $this->timezone->switchTZ();
309  // TODO: This is wrong: calculate UTC offset of given date
310  $offset = mktime(0,0,0,2,1,1970) - gmmktime(0,0,0,2,1,1970);
311  $this->timezone->restoreTZ();
312  return $offset;
313  }
314 
323  public function setDate($a_date,$a_format)
324  {
325  switch($a_format)
326  {
327  case IL_CAL_UNIX:
328  $this->unix = $a_date;
329  break;
330 
331  case IL_CAL_DATETIME:
332  $matches = 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);
333  if($matches < 1)
334  {
335  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
336  $this->log->write(__METHOD__.': '.print_r($matches,true));
337  $this->log->logStack();
338  throw new ilDateTimeException('Cannot parse date.');
339  }
340 
341  // UTC designator
342  if($d_parts[9] == 'Z')
343  {
344  $utc = ilTimeZone::_getInstance('UTC');
345  $utc->switchTZ();
346  }
347  else
348  {
349  $this->timezone->switchTZ();
350  }
351  $this->unix = mktime(
352  isset($d_parts[5]) ? $d_parts[5] : 0,
353  isset($d_parts[6]) ? $d_parts[6] : 0,
354  isset($d_parts[7]) ? $d_parts[7] : 0,
355  $d_parts[2],
356  $d_parts[3],
357  $d_parts[1]);
358 
359  if($d_parts[0] == '0000-00-00 00:00:00')
360  {
361  $this->unix = 0;
362  }
363 
364  if($d_parts[9] == 'Z')
365  {
366  $utc->restoreTZ();
367  }
368  else
369  {
370  $this->timezone->restoreTZ();
371  }
372  break;
373 
374  case IL_CAL_DATE:
375  // Pure dates are not timezone sensible.
377  $timezone->switchTZ();
378  $unix = strtotime($a_date);
379  $timezone->restoreTZ();
380  if($unix === false)
381  {
382  $this->log->write(__METHOD__.': Cannot parse date : '.$a_date);
383  $this->unix = 0;
384  return false;
385  }
386  $this->unix = $unix;
387  break;
388 
389  case IL_CAL_FKT_GETDATE:
390  if (!isset($a_date['seconds']))
391  {
392  $a_date['seconds'] = false;
393  }
394  // Format like getdate parameters
395  $this->timezone->switchTZ();
396  $this->unix = mktime(
397  $a_date['hours'],
398  $a_date['minutes'],
399  $a_date['seconds'],
400  $a_date['mon'],
401  $a_date['mday'],
402  $a_date['year']);
403  $this->timezone->restoreTZ();
404 
405  // TODO: choose better error handling
406  if(!$a_date['year'])
407  {
408  $this->unix = 0;
409  }
410  break;
411 
412  case IL_CAL_TIMESTAMP:
413  if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
414  {
415  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
416  throw new ilDateTimeException('Cannot parse date.');
417  }
418  $this->timezone->switchTZ();
419  $this->unix = mktime(
420  isset($d_parts[4]) ? $d_parts[4] : 0,
421  isset($d_parts[5]) ? $d_parts[5] : 0,
422  isset($d_parts[6]) ? $d_parts[6] : 0,
423  $d_parts[2],
424  $d_parts[3],
425  $d_parts[1]);
426 
427  if($d_parts[0] == '00000000000000' or
428  $d_parts[0] == '00000000')
429  {
430  $this->unix = 0;
431  }
432  $this->timezone->restoreTZ();
433  break;
434  }
435  return true;
436  }
437 
446  public function get($a_format,$a_format_str = '',$a_tz = '')
447  {
448  if($a_tz)
449  {
450  try
451  {
453  }
454  catch(ilTimeZoneException $exc)
455  {
456  $this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
457  }
458  }
459  else
460  {
461  #$timezone = $this->timezone;
463  }
464 
465  switch($a_format)
466  {
467  case IL_CAL_UNIX:
468  $date = $this->getUnixTime();
469  break;
470 
471  case IL_CAL_DATE:
472  $timezone->switchTZ();
473  $date = date('Y-m-d',$this->getUnixTime());
474  $timezone->restoreTZ();
475  break;
476 
477  case IL_CAL_DATETIME:
478  $timezone->switchTZ();
479  $date = date('Y-m-d H:i:s',$this->getUnixTime());
480  $timezone->restoreTZ();
481  break;
482 
483  case IL_CAL_FKT_DATE:
484  $timezone->switchTZ();
485  $date = date($a_format_str,$this->getUnixTime());
486  $timezone->restoreTZ();
487  break;
488 
489  case IL_CAL_FKT_GETDATE:
490  $timezone->switchTZ();
491  $date = getdate($this->getUnixTime());
492  $timezone->restoreTZ();
493 
494  // add iso 8601 week day number (Sunday = 7)
495  $date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
496  break;
497 
498  case IL_CAL_TIMESTAMP:
499  $timezone->switchTZ();
500  $date = date('YmdHis',$this->getUnixTime());
501  $timezone->restoreTZ();
502  break;
503  }
504  return $date;
505  }
506 
515  public function __toString()
516  {
517  return $this->get(IL_CAL_DATETIME).'<br>';
518  }
519 }
520 ?>