1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace POData\Providers\Metadata\Type; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class DateTime. |
9
|
|
|
*/ |
10
|
|
|
class DateTime implements IType |
11
|
|
|
{ |
12
|
|
|
protected static $comboRegex = |
13
|
|
|
"/^datetime\'(\d{4})-(\d{2})-(\d{2})((\s|T)([0-1][0-9]|2[0-4]):([0-5][0-9])(:([0-5][0-9])([Z]|[\+|-]\d{2}:\d{2})?)?)?\'$/"; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Gets the type code |
17
|
|
|
* Note: implementation of IType::getTypeCode. |
18
|
|
|
* |
19
|
|
|
* @return TypeCode |
20
|
|
|
*/ |
21
|
|
|
public function getTypeCode() |
22
|
|
|
{ |
23
|
|
|
return TypeCode::DATETIME; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Checks this type is compatible with another type |
28
|
|
|
* Note: implementation of IType::isCompatibleWith. |
29
|
|
|
* |
30
|
|
|
* @param IType $type Type to check compatibility |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function isCompatibleWith(IType $type) |
35
|
|
|
{ |
36
|
|
|
return TypeCode::DATETIME == $type->getTypeCode(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Validate a value in Astoria uri is in a format for this type |
41
|
|
|
* Note: implementation of IType::validate. |
42
|
|
|
* |
43
|
|
|
* @param string $value The value to validate |
44
|
|
|
* @param string &$outValue The stripped form of $value that can |
45
|
|
|
* be used in PHP expressions |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function validate($value, &$outValue) |
50
|
|
|
{ |
51
|
|
|
//1. The datetime value present in the $filter option should have 'datetime' prefix. |
52
|
|
|
//2. Month and day should be two digit |
53
|
|
|
if (!preg_match( |
54
|
|
|
self::$comboRegex, |
55
|
|
|
strval($value), |
56
|
|
|
$matches |
57
|
|
|
)) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//strip off prefix, and quotes from both ends |
62
|
|
|
$value = trim($value, 'datetime\''); |
|
|
|
|
63
|
|
|
$valLen = strlen($value) - 6; |
64
|
|
|
$offsetChek = $value[$valLen]; |
65
|
|
|
if (18 < $valLen && ('-' == $offsetChek || '+' == $offsetChek)) { |
66
|
|
|
$value = substr($value, 0, $valLen); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
//Validate the date using PHP Carbon class |
70
|
|
|
try { |
71
|
|
|
new Carbon($value, new \DateTimeZone('UTC')); |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$outValue = "'" . $value . "'"; |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets full name of this type in EDM namespace |
83
|
|
|
* Note: implementation of IType::getFullTypeName. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getFullTypeName() |
88
|
|
|
{ |
89
|
|
|
return 'Edm.DateTime'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Converts the given string value to datetime type. |
94
|
|
|
* Note: This function will not perform any conversion. |
95
|
|
|
* |
96
|
|
|
* @param string $stringValue Value to convert |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function convert($stringValue) |
101
|
|
|
{ |
102
|
|
|
return $stringValue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Convert the given value to a form that can be used in OData uri. |
107
|
|
|
* Note: The calling function should not pass null value, as this |
108
|
|
|
* function will not perform any check for nullability. |
109
|
|
|
* |
110
|
|
|
* @param mixed $value Value to convert |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function convertToOData($value) |
115
|
|
|
{ |
116
|
|
|
return 'datetime\'' . urlencode($value) . '\''; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Gets year from datetime. |
121
|
|
|
* |
122
|
|
|
* @param string $dateTime datetime to get the year from |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public static function year($dateTime) |
127
|
|
|
{ |
128
|
|
|
$date = new Carbon($dateTime); |
129
|
|
|
|
130
|
|
|
return $date->format('Y'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Gets month from datetime. |
135
|
|
|
* |
136
|
|
|
* @param string $dateTime datetime to get the month from |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public static function month($dateTime) |
141
|
|
|
{ |
142
|
|
|
$date = new Carbon($dateTime); |
143
|
|
|
|
144
|
|
|
return $date->format('m'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Gets day from datetime. |
149
|
|
|
* |
150
|
|
|
* @param string $dateTime datetime to get the day from |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public static function day($dateTime) |
155
|
|
|
{ |
156
|
|
|
$date = new Carbon($dateTime); |
157
|
|
|
|
158
|
|
|
return $date->format('d'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Gets hour from datetime. |
163
|
|
|
* |
164
|
|
|
* @param string $dateTime datetime to get the hour from |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public static function hour($dateTime) |
169
|
|
|
{ |
170
|
|
|
$date = new Carbon($dateTime); |
171
|
|
|
|
172
|
|
|
return $date->format('H'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Gets minute from datetime. |
177
|
|
|
* |
178
|
|
|
* @param string $dateTime datetime to get the minute from |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public static function minute($dateTime) |
183
|
|
|
{ |
184
|
|
|
$date = new Carbon($dateTime); |
185
|
|
|
|
186
|
|
|
return $date->format('i'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Gets second from datetime. |
191
|
|
|
* |
192
|
|
|
* @param string $dateTime datetime to get the second from |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public static function second($dateTime) |
197
|
|
|
{ |
198
|
|
|
$date = new Carbon($dateTime); |
199
|
|
|
|
200
|
|
|
return $date->format('s'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Compare two dates. Note that this function will not perform any |
205
|
|
|
* validation on dates, one should use either validate or |
206
|
|
|
* validateWithoutPrefix to validate the date before calling this |
207
|
|
|
* function. |
208
|
|
|
* |
209
|
|
|
* @param string $dateTime1 First date |
210
|
|
|
* @param string $dateTime2 Second date |
211
|
|
|
* |
212
|
|
|
* @return int |
213
|
|
|
*/ |
214
|
|
|
public static function dateTimeCmp($dateTime1, $dateTime2) |
215
|
|
|
{ |
216
|
|
|
$firstStamp = self::dateTimeCmpCheckInput($dateTime1, 'Invalid input - datetime1 must be DateTime or string'); |
217
|
|
|
$secondStamp = self::dateTimeCmpCheckInput($dateTime2, 'Invalid input - datetime2 must be DateTime or string'); |
218
|
|
|
|
219
|
|
|
if ($firstStamp == $secondStamp) { |
220
|
|
|
return 0; |
221
|
|
|
} |
222
|
|
|
return $firstStamp < $secondStamp ? -1 : 1; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Gets full name of the type implementing this interface in EDM namespace |
227
|
|
|
* Note: implementation of IType::getFullTypeName. |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function getName() |
232
|
|
|
{ |
233
|
|
|
return $this->getFullTypeName(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
protected static function dateTimeCmpCheckInput($dateTime, $msg) |
237
|
|
|
{ |
238
|
|
|
if (is_object($dateTime) && $dateTime instanceof \DateTime) { |
239
|
|
|
$firstStamp = $dateTime->getTimestamp(); |
240
|
|
|
return $firstStamp; |
241
|
|
|
} |
242
|
|
|
if (is_string($dateTime)) { |
243
|
|
|
$firstStamp = strtotime($dateTime); |
244
|
|
|
return $firstStamp; |
245
|
|
|
} |
246
|
|
|
throw new \Exception($msg); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|