1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\DandomainDateTime; |
3
|
|
|
|
4
|
|
|
use DateTime; |
5
|
|
|
use DateTimeInterface; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class represents a date time in Dandomain where the time zone is always Europe/Copenhagen |
12
|
|
|
*/ |
13
|
|
|
class DateTimeImmutable extends \DateTimeImmutable |
14
|
|
|
{ |
15
|
2 |
|
public function __construct($time = 'now') |
16
|
|
|
{ |
17
|
2 |
|
if (strpos($time, '@') !== false) { |
18
|
1 |
|
throw new InvalidArgumentException('Use DateTimeImmutable::createFromTimestamp instead'); |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
parent::__construct($time, static::timeZone()); |
22
|
1 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @throws Exception |
26
|
|
|
*/ |
27
|
|
|
public static function instance(DateTimeInterface $dt) : DateTimeImmutable |
28
|
|
|
{ |
29
|
|
|
if ($dt instanceof static) { |
30
|
|
|
return clone $dt; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return new static($dt->format('Y-m-d H:i:s.u')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
|
|
public static function createFromFormat($format, $time, $timezone = null) : DateTimeImmutable |
40
|
|
|
{ |
41
|
|
|
if ($timezone !== null) { |
42
|
|
|
throw new InvalidArgumentException('Do not pass time zone as an argument'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$dt = parent::createFromFormat($format, $time, static::timeZone()); |
46
|
|
|
return static::instance($dt); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param DateTime $dateTime |
51
|
|
|
* @return DateTimeImmutable |
52
|
|
|
*/ |
53
|
|
|
public static function createFromMutable($dateTime) : DateTimeImmutable |
54
|
|
|
{ |
55
|
|
|
$dt = static::createFromMutable($dateTime); |
56
|
|
|
return $dt->setTimezone(static::timeZone()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws Exception |
61
|
|
|
*/ |
62
|
|
|
public static function createFromTimestamp($timestamp) : DateTimeImmutable |
63
|
|
|
{ |
64
|
|
|
if (is_string($timestamp) && ($pos = strpos($timestamp, '.')) !== false) { |
65
|
|
|
$timestamp = substr($timestamp, 0, $pos); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$dateTime = new DateTime('@'.$timestamp); |
69
|
|
|
$dateTime->setTimezone(static::timeZone()); |
70
|
|
|
return static::instance($dateTime); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws Exception |
75
|
|
|
*/ |
76
|
|
|
public static function createFromJson(string $json) : DateTimeImmutable |
77
|
|
|
{ |
78
|
|
|
preg_match('/(\d+)\+/', $json, $matches); |
79
|
|
|
if (!isset($matches[1])) { |
80
|
|
|
throw new InvalidArgumentException('$json is not a valid JSON date. Input: ' . $json); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// remove the last three digits since the json date is given in milliseconds |
84
|
|
|
$timestamp = substr($matches[1], 0, -3); |
85
|
|
|
|
86
|
|
|
return static::createFromTimestamp($timestamp); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the default Dandomain time zone |
91
|
|
|
* |
92
|
|
|
* @return DateTimeZone |
93
|
|
|
*/ |
94
|
1 |
|
protected static function timeZone() : DateTimeZone |
95
|
|
|
{ |
96
|
1 |
|
return new DateTimeZone('Europe/Copenhagen'); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|