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