-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathIntlDateTime.php
370 lines (323 loc) · 10.9 KB
/
IntlDateTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
* IntlDateTime is an extended version of php 5 DateTime class with integrated
* IntlDateFormatter functionality which adds support for multiple calendars
* and locales provided by ICU project. (needs php >= 5.3.0 with intl extension)
* However, this class is not compatible with DateTime class because it uses ICU
* pattern syntax for formatting and parsing date strings.
* (@link http://userguide.icu-project.org/formatparse/datetime)
*
* @copyright Copyright 2010, Ali Farhadi (http://farhadi.ir/)
* @license GNU General Public License 3.0 (http://www.gnu.org/licenses/gpl.html)
*/
namespace farhadi;
class IntlDateTime extends \DateTime {
/**
* @var string The current locale in use
*/
protected $locale;
/**
* @var string The current calendar in use
*/
protected $calendar;
/**
* Creates a new instance of IntlDateTime
*
* @param mixed $time Unix timestamp or strtotime() compatible string or another DateTime object
* @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT).
* @param string $calendar any calendar supported by ICU (e.g. gregorian, persian, islamic, ...)
* @param string $locale any locale supported by ICU
* @param string $pattern the date pattern in which $time is formatted.
* @return IntlDateTime
*/
public function __construct($time = null, $timezone = null, $calendar = 'gregorian', $locale = 'en_US', $pattern = null) {
if (!isset($timezone)) $timezone = new \DateTimeZone(date_default_timezone_get());
elseif (!is_a($timezone, 'DateTimeZone')) $timezone = new \DateTimeZone($timezone);
parent::__construct(null, $timezone);
$this->setLocale($locale);
$this->setCalendar($calendar);
if (isset($time)) $this->set($time, null, $pattern);
}
/**
* Returns an instance of IntlDateFormatter with specified options.
*
* @param array $options
* @return IntlDateFormatter
*/
protected function getFormatter($options = array()) {
$locale = empty($options['locale']) ? $this->locale : $options['locale'];
$calendar = empty($options['calendar']) ? $this->calendar : $options['calendar'];
$timezone = empty($options['timezone']) ? $this->getTimezone() : $options['timezone'];
if (is_a($timezone, 'DateTimeZone')) $timezone = $timezone->getName();
$pattern = empty($options['pattern']) ? null : $options['pattern'];
return new \IntlDateFormatter($locale . '@calendar=' . $calendar,
\IntlDateFormatter::FULL, \IntlDateFormatter::FULL, $timezone,
$calendar == 'gregorian' ? \IntlDateFormatter::GREGORIAN : \IntlDateFormatter::TRADITIONAL, $pattern);
}
/**
* Replaces localized digits in $str with latin digits.
*
* @param string $str
* @return string Latinized string
*/
protected function latinizeDigits($str) {
$result = '';
$num = new \NumberFormatter($this->locale, \NumberFormatter::DECIMAL);
preg_match_all('/.[\x80-\xBF]*/', $str, $matches);
foreach ($matches[0] as $char) {
$pos = 0;
$parsedChar = $num->parse($char, \NumberFormatter::TYPE_INT32, $pos);
$result .= $pos ? $parsedChar : $char;
}
return $result;
}
/**
* Tries to guess the date pattern in which $time is formatted.
*
* @param string $time The date string
* @return string Detected ICU pattern on success, FALSE otherwise.
*/
protected function guessPattern($time) {
$time = $this->latinizeDigits(trim($time));
$shortDateRegex = '(\d{2,4})(-|\\\\|/)\d{1,2}\2\d{1,2}';
$longDateRegex = '([^\d]*\s)?\d{1,2}(-| )[^-\s\d]+\4(\d{2,4})';
$timeRegex = '\d{1,2}:\d{1,2}(:\d{1,2})?(\s.*)?';
if (preg_match("@^(?:(?:$shortDateRegex)|(?:$longDateRegex))(\s+$timeRegex)?$@", $time, $match)) {
if (!empty($match[1])) {
$separator = $match[2];
$pattern = strlen($match[1]) == 2 ? 'yy' : 'yyyy';
$pattern .= $separator . 'MM' . $separator . 'dd';
} else {
$separator = $match[4];
$pattern = 'dd' . $separator . 'LLL' . $separator;
$pattern .= strlen($match[5]) == 2 ? 'yy' : 'yyyy';
if (!empty($match[3])) $pattern = (preg_match('/,\s+$/', $match[3]) ? 'E, ' : 'E ') . $pattern;
}
if (!empty($match[6])) {
$pattern .= !empty($match[8]) ? ' hh:mm' : ' HH:mm';
if (!empty($match[7])) $pattern .= ':ss';
if (!empty($match[8])) $pattern .= ' a';
}
return $pattern;
}
return false;
}
/**
* Sets the locale used by the object.
*
* @param string $locale
* @return IntlDateTime The modified DateTime.
*/
public function setLocale($locale) {
$this->locale = $locale;
return $this;
}
/**
* Gets the current locale used by the object.
*
* @return string
*/
public function getLocale() {
return $this->locale;
}
/**
* Sets the calendar used by the object.
*
* @param string $calendar
* @return IntlDateTime The modified DateTime.
*/
public function setCalendar($calendar) {
$this->calendar = strtolower($calendar);
return $this;
}
/**
* Gets the current calendar used by the object.
*
* @return string
*/
public function getCalendar() {
return $this->calendar;
}
/**
* Overrides the getTimestamp method to support timestamps out of the integer range.
*
* @return float Unix timestamp representing the date.
*/
public function getTimestamp() {
return floatval(parent::format('U'));
}
/**
* Overrides the setTimestamp method to support timestamps out of the integer range.
*
* @param float $unixtimestamp Unix timestamp representing the date.
* @return IntlDateTime the modified DateTime.
*/
public function setTimestamp($unixtimestamp) {
$diff = $unixtimestamp - $this->getTimestamp();
$days = floor($diff / 86400);
$seconds = $diff - $days * 86400;
$timezone = $this->getTimezone();
$this->setTimezone('UTC');
parent::modify("$days days $seconds seconds");
$this->setTimezone($timezone);
return $this;
}
/**
* Alters object's internal timestamp with a string acceptable by strtotime() or a Unix timestamp or a DateTime object.
*
* @param mixed $time Unix timestamp or strtotime() compatible string or another DateTime object
* @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT).
* @param string $pattern the date pattern in which $time is formatted.
* @return IntlDateTime The modified DateTime.
*/
public function set($time, $timezone = null, $pattern = null) {
if (is_a($time, 'DateTime')) {
$time = $time->format('U');
} elseif (!is_numeric($time) || $pattern) {
if (!$pattern) {
$pattern = $this->guessPattern($time);
}
if (!$pattern && preg_match('/((?:[+-]?\d+)|next|last|previous)\s*(year|month)s?/i', $time)) {
if (isset($timezone)) {
$tempTimezone = $this->getTimezone();
$this->setTimezone($timezone);
}
$this->setTimestamp(time());
$this->modify($time);
if (isset($timezone)) {
$this->setTimezone($tempTimezone);
}
return $this;
}
$timezone = empty($timezone) ? $this->getTimezone() : $timezone;
if (is_a($timezone, 'DateTimeZone')) $timezone = $timezone->getName();
$defaultTimezone = @date_default_timezone_get();
date_default_timezone_set($timezone);
if ($pattern) {
$time = $this->getFormatter(array('timezone' => 'GMT', 'pattern' => $pattern))->parse($time);
$time -= date('Z', $time);
} else {
$time = strtotime($time);
}
date_default_timezone_set($defaultTimezone);
}
$this->setTimestamp($time);
return $this;
}
/**
* Resets the current date of the object.
*
* @param integer $year
* @param integer $month
* @param integer $day
* @return IntlDateTime The modified DateTime.
*/
public function setDate($year, $month, $day) {
$this->set("$year/$month/$day ".$this->format('HH:mm:ss'), null, 'yyyy/MM/dd HH:mm:ss');
return $this;
}
/**
* Sets the timezone for the object.
*
* @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT).
* @return IntlDateTime The modified DateTime.
*/
public function setTimezone($timezone) {
if (!is_a($timezone, 'DateTimeZone')) $timezone = new \DateTimeZone($timezone);
parent::setTimezone($timezone);
return $this;
}
/**
* Internally used by modify method to calculate calendar-aware modifications
*
* @param array $matches
* @return string An empty string
*/
protected function modifyCallback($matches) {
if (!empty($matches[1])) {
parent::modify($matches[1]);
}
list($y, $m, $d) = explode('-', $this->format('y-M-d'));
$change = strtolower($matches[2]);
$unit = strtolower($matches[3]);
switch ($change) {
case "next":
$change = 1;
break;
case "last":
case "previous":
$change = -1;
break;
}
switch ($unit) {
case "month":
$m += $change;
if ($m > 12) {
$y += floor($m/12);
$m = $m % 12;
} elseif ($m < 1) {
$y += ceil($m/12) - 1;
$m = $m % 12 + 12;
}
break;
case "year":
$y += $change;
break;
}
$this->setDate($y, $m, $d);
return '';
}
/**
* Alter the timestamp by incrementing or decrementing in a format accepted by strtotime().
*
* @param string $modify a string in a relative format accepted by strtotime().
* @return IntlDateTime The modified DateTime.
*/
public function modify($modify) {
$modify = $this->latinizeDigits(trim($modify));
$modify = preg_replace_callback('/(.*?)((?:[+-]?\d+)|next|last|previous)\s*(year|month)s?/i', array($this, 'modifyCallback'), $modify);
if ($modify) parent::modify($modify);
return $this;
}
/**
* Returns date formatted according to given pattern.
*
* @param string $pattern Date pattern in ICU syntax (@link http://userguide.icu-project.org/formatparse/datetime)
* @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT).
* @return string Formatted date on success or FALSE on failure.
*/
public function format($pattern, $timezone = null) {
if (isset($timezone)) {
$tempTimezone = $this->getTimezone();
$this->setTimezone($timezone);
}
// Timezones DST data in ICU are not as accurate as PHP.
// So we get timezone offset from php and pass it to ICU.
$result = $this->getFormatter(array(
'timezone' => 'GMT' . (parent::format('Z') ? parent::format('P') : ''),
'pattern' => $pattern
))->format($this->getTimestamp());
if (isset($timezone)) {
$this->setTimezone($tempTimezone);
}
return $result;
}
/**
* Preserve original DateTime::format functionality
*
* @param string $format Format accepted by date().
* @param mixed $timezone DateTimeZone object or timezone identifier as full name (e.g. Asia/Tehran) or abbreviation (e.g. IRDT).
* @return string Formatted date on success or FALSE on failure.
*/
public function classicFormat($format, $timezone = null) {
if (isset($timezone)) {
$tempTimezone = $this->getTimezone();
$this->setTimezone($timezone);
}
$result = parent::format($format);
if (isset($timezone)) {
$this->setTimezone($tempTimezone);
}
return $result;
}
}