ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Time.php
Go to the documentation of this file.
1 <?php
8 namespace SimpleSAML\Utils;
9 
11 
12 
13 class Time
14 {
15 
21  private static $tz_initialized = false;
22 
23 
32  public static function generateTimestamp($instant = null)
33  {
34  if ($instant === null) {
35  $instant = time();
36  }
37  return gmdate('Y-m-d\TH:i:s\Z', $instant);
38  }
39 
40 
52  public static function initTimezone()
53  {
54  if (self::$tz_initialized) {
55  return;
56  }
57 
59 
60  $timezone = $globalConfig->getString('timezone', null);
61  if ($timezone !== null) {
62  if (!date_default_timezone_set($timezone)) {
63  throw new \SimpleSAML_Error_Exception('Invalid timezone set in the "timezone" option in config.php.');
64  }
65  self::$tz_initialized = true;
66  return;
67  }
68  // we don't have a timezone configured
69 
70  Logger::maskErrors(E_ALL);
71  $serverTimezone = date_default_timezone_get();
73 
74  // set the timezone to the default
75  date_default_timezone_set($serverTimezone);
76  self::$tz_initialized = true;
77  }
78 
79 
92  public static function parseDuration($duration, $timestamp = null)
93  {
94  if (!(is_string($duration) && (is_int($timestamp) || is_null($timestamp)))) {
95  throw new \InvalidArgumentException('Invalid input parameters');
96  }
97 
98  // parse the duration. We use a very strict pattern
99  $durationRegEx = '#^(-?)P(?:(?:(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?(?:T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+)'.
100  '(?:[.,]\d+)?S)?)?)|(?:(\\d+)W))$#D';
101  if (!preg_match($durationRegEx, $duration, $matches)) {
102  throw new \InvalidArgumentException('Invalid ISO 8601 duration: '.$duration);
103  }
104 
105  $durYears = (empty($matches[2]) ? 0 : (int) $matches[2]);
106  $durMonths = (empty($matches[3]) ? 0 : (int) $matches[3]);
107  $durDays = (empty($matches[4]) ? 0 : (int) $matches[4]);
108  $durHours = (empty($matches[5]) ? 0 : (int) $matches[5]);
109  $durMinutes = (empty($matches[6]) ? 0 : (int) $matches[6]);
110  $durSeconds = (empty($matches[7]) ? 0 : (int) $matches[7]);
111  $durWeeks = (empty($matches[8]) ? 0 : (int) $matches[8]);
112 
113  if (!empty($matches[1])) {
114  // negative
115  $durYears = -$durYears;
116  $durMonths = -$durMonths;
117  $durDays = -$durDays;
118  $durHours = -$durHours;
119  $durMinutes = -$durMinutes;
120  $durSeconds = -$durSeconds;
121  $durWeeks = -$durWeeks;
122  }
123 
124  if ($timestamp === null) {
125  $timestamp = time();
126  }
127 
128  if ($durYears !== 0 || $durMonths !== 0) {
129  /* Special handling of months and years, since they aren't a specific interval, but
130  * instead depend on the current time.
131  */
132 
133  /* We need the year and month from the timestamp. Unfortunately, PHP doesn't have the
134  * gmtime function. Instead we use the gmdate function, and split the result.
135  */
136  $yearmonth = explode(':', gmdate('Y:n', $timestamp));
137  $year = (int) ($yearmonth[0]);
138  $month = (int) ($yearmonth[1]);
139 
140  // remove the year and month from the timestamp
141  $timestamp -= gmmktime(0, 0, 0, $month, 1, $year);
142 
143  // add years and months, and normalize the numbers afterwards
144  $year += $durYears;
145  $month += $durMonths;
146  while ($month > 12) {
147  $year += 1;
148  $month -= 12;
149  }
150  while ($month < 1) {
151  $year -= 1;
152  $month += 12;
153  }
154 
155  // add year and month back into timestamp
156  $timestamp += gmmktime(0, 0, 0, $month, 1, $year);
157  }
158 
159  // add the other elements
160  $timestamp += $durWeeks * 7 * 24 * 60 * 60;
161  $timestamp += $durDays * 24 * 60 * 60;
162  $timestamp += $durHours * 60 * 60;
163  $timestamp += $durMinutes * 60;
164  $timestamp += $durSeconds;
165 
166  return $timestamp;
167  }
168 }
static popErrorMask()
Pop an error mask.
Definition: Logger.php:324
static initTimezone()
Initialize the timezone.
Definition: Time.php:52
static generateTimestamp($instant=null)
This function generates a timestamp on the form used by the SAML protocols.
Definition: Time.php:32
static parseDuration($duration, $timestamp=null)
Interpret a ISO8601 duration value relative to a given timestamp.
Definition: Time.php:92
static $tz_initialized
Definition: Time.php:21
$globalConfig
static maskErrors($mask)
Disable error reporting for the given log levels.
Definition: Logger.php:306
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.