1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the class {@see \AppUtils\Microtime_ParseResult}. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage Microtime |
7
|
|
|
* @see \AppUtils\Microtime_ParseResult |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
use DateTimeZone; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Date parsing result, containing the date string |
18
|
|
|
* and time zone to use for the DateTime constructor. |
19
|
|
|
* |
20
|
|
|
* This is used to simplify creating a new microtime |
21
|
|
|
* instance when using the factory methods, to avoid |
22
|
|
|
* the type checks that are done when using the |
23
|
|
|
* constructor. |
24
|
|
|
* |
25
|
|
|
* @package Application Utils |
26
|
|
|
* @subpackage Microtime |
27
|
|
|
* @author Sebastian Mordziol <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Microtime_ParseResult implements Interface_Stringable |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $dateTime; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DateTimeZone |
38
|
|
|
*/ |
39
|
|
|
private $timeZone; |
40
|
|
|
|
41
|
|
|
public function __construct(string $datetime, DateTimeZone $timeZone) |
42
|
|
|
{ |
43
|
|
|
if(stripos($datetime, 'T') !== false) { |
44
|
|
|
$datetime = $this->parseISO8601($datetime); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->dateTime = $datetime; |
48
|
|
|
$this->timeZone = $timeZone; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function parseISO8601(string $datetime) : string |
52
|
|
|
{ |
53
|
|
|
preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2})\.([0-9]+)Z/', $datetime, $matches); |
54
|
|
|
|
55
|
|
|
if(!empty($matches[0])) { |
56
|
|
|
return sprintf( |
57
|
|
|
'%s %s.%s', |
58
|
|
|
$matches[1], |
59
|
|
|
$matches[2], |
60
|
|
|
substr($matches[3], 0, 6) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $datetime; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __toString() : string |
68
|
|
|
{ |
69
|
|
|
return $this->getDateTime(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getDateTime() : string |
76
|
|
|
{ |
77
|
|
|
return $this->dateTime; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return DateTimeZone |
82
|
|
|
*/ |
83
|
|
|
public function getTimeZone() : DateTimeZone |
84
|
|
|
{ |
85
|
|
|
return $this->timeZone; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|