|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of mockable-datetime. |
|
4
|
|
|
* |
|
5
|
|
|
* mockable-datetime is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* mockable-datetime is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with mockable-datetime. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Mcustiel\Mockable; |
|
20
|
|
|
|
|
21
|
|
|
class DateTime |
|
22
|
|
|
{ |
|
23
|
|
|
const DATETIME_SYSTEM = 0; |
|
24
|
|
|
const DATETIME_FIXED = 1; |
|
25
|
|
|
const DATETIME_OFFSET = 2; |
|
26
|
|
|
|
|
27
|
|
|
/** @var int */ |
|
28
|
|
|
private static $type = self::DATETIME_SYSTEM; |
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
private static $timestamp = 0; |
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
private static $offsetTimestamp = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** @param int $timestamp */ |
|
35
|
9 |
|
public static function setFixed(\DateTime $dateTime) |
|
36
|
|
|
{ |
|
37
|
9 |
|
self::$type = self::DATETIME_FIXED; |
|
38
|
9 |
|
self::$timestamp = $dateTime->getTimestamp(); |
|
39
|
9 |
|
} |
|
40
|
|
|
|
|
41
|
21 |
|
public static function setSystem() |
|
42
|
|
|
{ |
|
43
|
21 |
|
self::$type = self::DATETIME_SYSTEM; |
|
44
|
21 |
|
self::$timestamp = 0; |
|
45
|
21 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @param int $timestamp */ |
|
48
|
9 |
|
public static function setOffset(\DateTime $dateTime) |
|
49
|
|
|
{ |
|
50
|
9 |
|
self::$type = self::DATETIME_OFFSET; |
|
51
|
9 |
|
self::$timestamp = $dateTime->getTimestamp(); |
|
52
|
9 |
|
self::$offsetTimestamp = (new \DateTime())->getTimestamp(); |
|
53
|
9 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $time |
|
57
|
|
|
* @param \DateTimeZone $timeZone |
|
58
|
|
|
* |
|
59
|
|
|
* @return \DateTime |
|
60
|
|
|
*/ |
|
61
|
9 |
|
public static function newPhpDateTime($time = 'now', \DateTimeZone $timeZone = null) |
|
62
|
|
|
{ |
|
63
|
9 |
|
if (self::DATETIME_SYSTEM === self::$type) { |
|
64
|
3 |
|
return new \DateTime($time, $timeZone); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
7 |
|
if (self::DATETIME_FIXED === self::$type) { |
|
68
|
4 |
|
return self::getFixedTimeFromConfiguredTimestamp($time, $timeZone); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
$date = self::getFixedTimeFromConfiguredTimestamp($time, $timeZone); |
|
72
|
4 |
|
$timePassedSinceInstantiation = abs( |
|
73
|
4 |
|
(new \DateTime())->getTimestamp() - self::$offsetTimestamp |
|
74
|
4 |
|
); |
|
75
|
4 |
|
$date->modify("+{$timePassedSinceInstantiation} seconds"); |
|
76
|
|
|
|
|
77
|
4 |
|
return $date; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $time |
|
82
|
|
|
* @param \DateTimeZone $timeZone |
|
83
|
|
|
* |
|
84
|
|
|
* @return \DateTimeImmutable |
|
85
|
|
|
*/ |
|
86
|
12 |
|
public static function newImmutablePhpDateTime($time = 'now', \DateTimeZone $timeZone = null) |
|
87
|
|
|
{ |
|
88
|
12 |
|
if (self::DATETIME_SYSTEM === self::$type) { |
|
89
|
4 |
|
return new \DateTimeImmutable($time, $timeZone); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
9 |
|
if (self::DATETIME_FIXED === self::$type) { |
|
93
|
5 |
|
return self::getFixedImmutableTimeFromConfiguredTimestamp($time, $timeZone); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
$date = self::getFixedImmutableTimeFromConfiguredTimestamp($time, $timeZone); |
|
97
|
5 |
|
$timePassedSinceInstantiation = abs( |
|
98
|
5 |
|
(new \DateTime())->getTimestamp() - self::$offsetTimestamp |
|
99
|
5 |
|
); |
|
100
|
|
|
|
|
101
|
5 |
|
return $date->modify("+{$timePassedSinceInstantiation} seconds"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param mixed $time |
|
106
|
|
|
* @param null|mixed $timeZone |
|
107
|
|
|
* |
|
108
|
|
|
* @throws \Exception |
|
109
|
|
|
* |
|
110
|
|
|
* @return \DateTime |
|
111
|
|
|
*/ |
|
112
|
7 |
|
private static function getFixedTimeFromConfiguredTimestamp($time, $timeZone = null) |
|
113
|
|
|
{ |
|
114
|
7 |
|
$date = new \DateTime(sprintf('@%d', self::$timestamp)); |
|
115
|
|
|
|
|
116
|
7 |
|
if ('now' !== $time) { |
|
117
|
3 |
|
$date = $date->modify($time); |
|
118
|
3 |
|
} |
|
119
|
7 |
|
if (null !== $timeZone) { |
|
120
|
|
|
$date = $date->setTimezone($timeZone); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
7 |
|
if (false === $date) { |
|
|
|
|
|
|
124
|
|
|
throw new \RuntimeException('An error happened creating the immutable date'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
7 |
|
return $date; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param mixed $time |
|
132
|
|
|
* @param null|mixed $timeZone |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \Exception |
|
135
|
|
|
* |
|
136
|
|
|
* @return \DateTimeImmutable |
|
137
|
|
|
*/ |
|
138
|
9 |
|
private static function getFixedImmutableTimeFromConfiguredTimestamp($time, $timeZone = null) |
|
139
|
|
|
{ |
|
140
|
9 |
|
$date = new \DateTimeImmutable(sprintf('@%d', self::$timestamp)); |
|
141
|
9 |
|
if ('now' !== $time) { |
|
142
|
3 |
|
$date = $date->modify($time); |
|
143
|
3 |
|
} |
|
144
|
9 |
|
if (null !== $timeZone) { |
|
145
|
2 |
|
$date = $date->setTimezone($timeZone); |
|
146
|
2 |
|
} |
|
147
|
|
|
|
|
148
|
9 |
|
if (false === $date) { |
|
149
|
|
|
throw new \RuntimeException('An error happened creating the immutable date'); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
9 |
|
return $date; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|