-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate.php
205 lines (195 loc) · 5.09 KB
/
date.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
<?php
/* Eregansu: Date/time string parsing
*
* Copyright 2009 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class EregansuDateTime extends DateTime
{
static $months = array(
'jan' => '01',
'feb' => '02',
'mar' => '03',
'apr' => '04',
'may' => '05',
'jun' => '06',
'jul' => '07',
'aug' => '08',
'sep' => '09',
'oct' => '10',
'nov' => '11',
'dec' => '12',
);
/* Beefier parsing than with PHP's own DateTime; always uses UTC */
public static function parse($s, $tz = null)
{
static $utc = null;
$s = trim(strtolower($s));
if(!strlen($s))
{
return;
}
if($utc === null)
{
$utc = new DateTimeZone('UTC');
}
if($tz === null)
{
$tz = $utc;
}
/* Standard PHP format */
if(preg_match('!^\d{2,4}-\d{1,2}-\d{1,2}(\s+\d{1,2}:\d{1,2}(:\d{1,2})?)?$!', $s))
{
$dt = new EregansuDateTime($s, $tz);
$dt->setTimezone($utc);
return $dt;
}
/* Day, 99 Mon 9999 00:00[:00] (GMT|UTC|(+|-)00[:]00) */
if(preg_match('!^[a-z]+,?\s+(\d{1,2})\s+([a-z]+)\s+(\d{4})\s+(\d{2}:\d{2}(:\d{2})?)(\s.*)?$!i', $s, $matches) && isset($matches[6]) && isset(self::$months[$matches[2]]))
{
$time = explode(':', $matches[4]);
if(!isset($time[2]))
{
$time[2] = '00';
}
$s = sprintf('%04d-%02d-%02d %02d:%02d:%02d',
$matches[3], self::$months[$matches[2]], $matches[1], $time[0], $time[1], @$time[2]);
$dt = new EregansuDateTime($s, $tz);
$dt->applyTimezoneString($matches[6]);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^\d{4}-\d{2}-\d{2}t\d{2}:\d{2}(:\d{2})?[+-]\d{2}:\d{2}$/', $s))
{
$date = explode(' ', preg_replace('/^(\d{4}-\d{2}-\d{2})t(\d{2}:\d{2}(:\d{2}?))([+-]\d{2}:\d{2})$/', '\1 \2 \4', $s));
$dt = new EregansuDateTime($date[0] . ' ' . $date[1], $tz);
$dt->applyTimezoneString($date[2]);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^\d{4}-\d{2}-\d{2}t\d{2}:\d{2}(:\d{2})?[+-]\d{4}$/', $s))
{
$dt = explode(' ', preg_replace('/^(\d{4}-\d{2}-\d{2})t(\d{2}:\d{2}(:\d{2}?))([+-]\d{4})/', '\1 \2 \4', $s));
$dt = new EregansuDateTime($date[0] . ' ' . $date[1], $tz);
$dt->applyTimezoneString($date[2]);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^\d{4}-\d{2}-\d{2}t\d{2}:\d{2}(:\d{2})?z?$/', $s))
{
$s = preg_replace('/^(\d{4}-\d{2}-\d{2})t(\d{2}:\d{2}(:\d{2})?)z?/', '\1 \2', $s);
$dt = new EregansuDateTime($s, $tz);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^(\d{4})(\d{2})(\d{2})t(\d{2})(\d{2})(\d{2})?z?$/', $s, $match))
{
$s = $match[1] . '-' . $match[2] . '-' . $match[3] . ' ' . $match[4] . ':' . $match[5] . ':' . (isset($match[6]) ? $match[6] : '00');
$dt = new EregansuDateTime($s, $tz);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^\d{4}-\d{2}-\d{2}$/', $s))
{
$dt = new EregansuDateTime($s . ' 00:00:00', $tz);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^\d{4}-\d{2}$/', $s))
{
$dt = new EregansuDateTime($s . '-01 00:00:00', $tz);
$dt->setTimezone($utc);
return $dt;
}
if(preg_match('/^\d{4}$/', $s))
{
$dt = new EreganuDateTime($s . '-01-01 00:00:00', $tz);
$dt->setTimezone($utc);
return $dt;
}
trigger_error('Unsupported date format while parsing "' . $s . '"', E_USER_NOTICE);
}
protected function applyTimezoneString($tz)
{
$tz = strtoupper(trim($tz));
if(!strlen($tz) || !strcmp($tz, 'utc') || !strcmp($tz, 'gmt'))
{
return;
}
if(!isset($tz['hour']) || !isset($tz['minute']))
{
return;
}
if(strpos($tz, ':') !== false)
{
$hm = sscanf($tz, '%d:%d');
}
else
{
$hm = array(intval(substr($tz, 0, 3)), intval(substr($tz, 3)));
}
if(empty($hm[0]) && empty($hm[1]))
{
return;
}
$sub = true;
if(intval($hm[0]) < 0)
{
$sub = false;
$hm[0] = intval($hm[0]) * -1;
}
$interval = new DateInterval('PT' . $hm[0] . 'H' . $hm[1] . 'M');
if($sub)
{
$this->sub($interval);
}
else
{
$this->add($interval);
}
}
public function __toString()
{
return str_replace('+00:00', 'Z', $this->format(self::RFC3339));
}
/* Return YYYY-MM-DD */
public function date()
{
return $this->format('Y-m-d');
}
/* Return HH:MM:SS */
public function time()
{
return $this->format('H:i:s');
}
}
function parse_datetime($s, $object = false, $tz = null)
{
if(($dt = EregansuDateTime::parse($s, $tz)) === null)
{
return null;
}
if($object)
{
return $dt;
}
return $dt->getTimestamp();
return null;
}
function format_timezone($date, $format, $zoneName)
{
$s = strftime('%Y-%m-%d %H:%M:%S', $date);
$dt = new DateTime($s, new DateTimeZone($zoneName));
return $dt->format($format);
}