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