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