ILIAS  eassessment Revision 61809
 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 function increment($a_type,$a_count = 1)
234  {
235  $count_str = $a_count > 0 ? ('+'.$a_count.' ') : ($a_count.' ');
236 
237  $this->timezone->switchTZ();
238  switch($a_type)
239  {
240  case self::YEAR:
241  $this->unix = strtotime($count_str.'year',$this->unix);
242  break;
243 
244  case self::MONTH:
245  // strtotime fails in the following case:
246  // 2008-03-31 (+1 month) => 2008-05-01
247  // In that case, we substract the new month day
248 
249  $old_day_of_month = $this->get(IL_CAL_FKT_DATE,'j');
250  $this->unix = strtotime($count_str.'month',$this->unix);
251 
252  // TODO: Fix monthly calculations.
253 
254  /*
255  $new_day_of_month = $this->get(IL_CAL_FKT_DATE,'j');
256  if($new_day_of_month != $old_day_of_month)
257  {
258  $this->unix = $this->increment(IL_CAL_DAY,$new_day_of_month * -1);
259  }
260  */
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 
333  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) < 1)
334  {
335  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
336  throw new ilDateTimeException('Cannot parse date.');
337  }
338 
339  $this->timezone->switchTZ();
340  $this->unix = mktime(
341  isset($d_parts[5]) ? $d_parts[5] : 0,
342  isset($d_parts[6]) ? $d_parts[6] : 0,
343  isset($d_parts[7]) ? $d_parts[7] : 0,
344  $d_parts[2],
345  $d_parts[3],
346  $d_parts[1]);
347 
348  if($d_parts[0] == '0000-00-00 00:00:00')
349  {
350  $this->unix = 0;
351  }
352 
353  $this->timezone->restoreTZ();
354  break;
355 
356  case IL_CAL_DATE:
357  // Pure dates are not timezone sensible.
359  $timezone->switchTZ();
360  $unix = strtotime($a_date);
361  $timezone->restoreTZ();
362  if($unix === false)
363  {
364  $this->log->write(__METHOD__.': Cannot parse date : '.$a_date);
365  $this->unix = 0;
366  return false;
367  }
368  $this->unix = $unix;
369  break;
370 
371  case IL_CAL_FKT_GETDATE:
372  if (!isset($a_date['seconds']))
373  {
374  $a_date['seconds'] = false;
375  }
376  // Format like getdate parameters
377  $this->timezone->switchTZ();
378  $this->unix = mktime(
379  $a_date['hours'],
380  $a_date['minutes'],
381  $a_date['seconds'],
382  $a_date['mon'],
383  $a_date['mday'],
384  $a_date['year']);
385  $this->timezone->restoreTZ();
386 
387  // TODO: choose better error handling
388  if(!$a_date['year'])
389  {
390  $this->unix = 0;
391  }
392  break;
393 
394  case IL_CAL_TIMESTAMP:
395  if(preg_match("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/", $a_date,$d_parts) == false)
396  {
397  $this->log->write(__METHOD__.': Cannot parse date: '.$a_date);
398  throw new ilDateTimeException('Cannot parse date.');
399  }
400  $this->timezone->switchTZ();
401  $this->unix = mktime(
402  isset($d_parts[4]) ? $d_parts[4] : 0,
403  isset($d_parts[5]) ? $d_parts[5] : 0,
404  isset($d_parts[6]) ? $d_parts[6] : 0,
405  $d_parts[2],
406  $d_parts[3],
407  $d_parts[1]);
408 
409  if($d_parts[0] == '00000000000000' or
410  $d_parts[0] == '00000000')
411  {
412  $this->unix = 0;
413  }
414  $this->timezone->restoreTZ();
415  break;
416  }
417  return true;
418  }
419 
428  public function get($a_format,$a_format_str = '',$a_tz = '')
429  {
430  if($a_tz)
431  {
432  try
433  {
435  }
436  catch(ilTimeZoneException $exc)
437  {
438  $this->log->write(__METHOD__.': Invalid timezone given. Timezone: '.$a_tz);
439  }
440  }
441  else
442  {
443  #$timezone = $this->timezone;
445  }
446 
447  switch($a_format)
448  {
449  case IL_CAL_UNIX:
450  $date = $this->getUnixTime();
451  break;
452 
453  case IL_CAL_DATE:
454  $timezone->switchTZ();
455  $date = date('Y-m-d',$this->getUnixTime());
456  $timezone->restoreTZ();
457  break;
458 
459  case IL_CAL_DATETIME:
460  $timezone->switchTZ();
461  $date = date('Y-m-d H:i:s',$this->getUnixTime());
462  $timezone->restoreTZ();
463  break;
464 
465  case IL_CAL_FKT_DATE:
466  $timezone->switchTZ();
467  $date = date($a_format_str,$this->getUnixTime());
468  $timezone->restoreTZ();
469  break;
470 
471  case IL_CAL_FKT_GETDATE:
472  $timezone->switchTZ();
473  $date = getdate($this->getUnixTime());
474  $timezone->restoreTZ();
475 
476  // add iso 8601 week day number (Sunday = 7)
477  $date['isoday'] = $date['wday'] == 0 ? 7 : $date['wday'];
478  break;
479 
480  case IL_CAL_TIMESTAMP:
481  $timezone->switchTZ();
482  $date = date('YmdHis',$this->getUnixTime());
483  $timezone->restoreTZ();
484  break;
485  }
486  return $date;
487  }
488 
496  public function __toString()
497  {
498  return $this->get(IL_CAL_DATETIME,'','UTC').'<br>';
499  }
500 }
501 ?>