-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Timestamp.php
244 lines (221 loc) · 6.11 KB
/
Timestamp.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
<?php
namespace LdapRecord\Models\Attributes;
use Carbon\Carbon;
use Carbon\CarbonInterface;
use DateTime;
use LdapRecord\LdapRecordException;
use LdapRecord\Utilities;
class Timestamp
{
/**
* The current timestamp type.
*
* @var string
*/
protected $type;
/**
* The available timestamp types.
*
* @var array
*/
protected $types = [
'ldap',
'windows',
'windows-int',
];
/**
* Constructor.
*
* @param string $type
*
* @throws LdapRecordException
*/
public function __construct($type)
{
$this->setType($type);
}
/**
* Set the type of timestamp to convert from / to.
*
* @param string $type
*
* @throws LdapRecordException
*/
public function setType($type)
{
if (! in_array($type, $this->types)) {
throw new LdapRecordException("Unrecognized LDAP date type [$type]");
}
$this->type = $type;
}
/**
* Converts the value to an LDAP date string.
*
* @param mixed $value
*
* @return float|string
*
* @throws LdapRecordException
*/
public function fromDateTime($value)
{
$value = is_array($value) ? reset($value) : $value;
// If the value is being converted to a windows integer format but it
// is already in that format, we will simply return the value back.
if ($this->type == 'windows-int' && $this->valueIsWindowsIntegerType($value)) {
return $value;
}
// If the value is numeric, we will assume it's a UNIX timestamp.
elseif (is_numeric($value)) {
$value = Carbon::createFromTimestamp($value);
}
// If a string is given, we will pass it into a new carbon instance.
elseif (is_string($value)) {
$value = Carbon::parse($value);
}
// If a date object is given, we will convert it to a carbon instance.
elseif ($value instanceof DateTime) {
$value = Carbon::instance($value);
}
switch ($this->type) {
case 'ldap':
$value = $this->convertDateTimeToLdapTime($value);
break;
case 'windows':
$value = $this->convertDateTimeToWindows($value);
break;
case 'windows-int':
$value = $this->convertDateTimeToWindowsInteger($value);
break;
default:
throw new LdapRecordException("Unrecognized date type [{$this->type}]");
}
return $value;
}
/**
* Determine if the value given is in Windows Integer (NTFS Filetime) format.
*
* @param int|string $value
*
* @return bool
*/
protected function valueIsWindowsIntegerType($value)
{
return is_numeric($value) && strlen((string) $value) === 18;
}
/**
* Converts the LDAP timestamp value to a Carbon instance.
*
* @param mixed $value
*
* @return Carbon|false
*
* @throws LdapRecordException
*/
public function toDateTime($value)
{
$value = is_array($value) ? reset($value) : $value;
if ($value instanceof CarbonInterface || $value instanceof DateTime) {
return Carbon::instance($value);
}
switch ($this->type) {
case 'ldap':
$value = $this->convertLdapTimeToDateTime($value);
break;
case 'windows':
$value = $this->convertWindowsTimeToDateTime($value);
break;
case 'windows-int':
$value = $this->convertWindowsIntegerTimeToDateTime($value);
break;
default:
throw new LdapRecordException("Unrecognized date type [{$this->type}]");
}
return $value instanceof DateTime ? Carbon::instance($value) : $value;
}
/**
* Converts standard LDAP timestamps to a date time object.
*
* @param string $value
*
* @return DateTime|false
*/
protected function convertLdapTimeToDateTime($value)
{
return DateTime::createFromFormat(
strpos($value, 'Z') !== false ? 'YmdHis\Z' : 'YmdHisT',
$value
);
}
/**
* Converts date objects to a standard LDAP timestamp.
*
* @param DateTime $date
*
* @return string
*/
protected function convertDateTimeToLdapTime(DateTime $date)
{
return $date->format(
$date->getOffset() == 0 ? 'YmdHis\Z' : 'YmdHisO'
);
}
/**
* Converts standard windows timestamps to a date time object.
*
* @param string $value
*
* @return DateTime|false
*/
protected function convertWindowsTimeToDateTime($value)
{
return DateTime::createFromFormat(
strpos($value, '0Z') !== false ? 'YmdHis.0\Z' : 'YmdHis.0T',
$value
);
}
/**
* Converts date objects to a windows timestamp.
*
* @param DateTime $date
*
* @return string
*/
protected function convertDateTimeToWindows(DateTime $date)
{
return $date->format(
$date->getOffset() == 0 ? 'YmdHis.0\Z' : 'YmdHis.0O'
);
}
/**
* Converts standard windows integer dates to a date time object.
*
* @param int $value
*
* @return DateTime|false
*
* @throws \Exception
*/
protected function convertWindowsIntegerTimeToDateTime($value)
{
// ActiveDirectory dates that contain integers may return
// "0" when they are not set. We will validate that here.
if (! $value) {
return false;
}
return (new DateTime())->setTimestamp(
Utilities::convertWindowsTimeToUnixTime($value)
);
}
/**
* Converts date objects to a windows integer timestamp.
*
* @param DateTime $date
*
* @return float
*/
protected function convertDateTimeToWindowsInteger(DateTime $date)
{
return Utilities::convertUnixTimeToWindowsTime($date->getTimestamp());
}
}