1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\Stepup\DateTime; |
20
|
|
|
|
21
|
|
|
use DateInterval; |
22
|
|
|
use DateTime as CoreDateTime; |
23
|
|
|
use Surfnet\Stepup\Exception\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
class UtcDateTime |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The 'c' format, expanded in separate format characters. This string can also be used with |
29
|
|
|
* `DateTime::createFromString()`. |
30
|
|
|
*/ |
31
|
|
|
const FORMAT = 'Y-m-d\\TH:i:sP'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CoreDateTime |
35
|
|
|
*/ |
36
|
|
|
private $dateTime; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param CoreDateTime $dateTime |
40
|
|
|
*/ |
41
|
|
|
public function __construct(CoreDateTime $dateTime) |
42
|
|
|
{ |
43
|
|
|
if ($dateTime->getOffset() !== 0) { |
44
|
|
|
throw new InvalidArgumentException( |
45
|
|
|
'Stepup DateTime requires a UTC datetime, but got DateTime with offset %d', |
46
|
|
|
$dateTime->getOffset() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->dateTime = $dateTime; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param DateInterval $interval |
55
|
|
|
* @return UtcDateTime |
56
|
|
|
*/ |
57
|
|
|
public function add(DateInterval $interval) |
58
|
|
|
{ |
59
|
|
|
$dateTime = clone $this->dateTime; |
60
|
|
|
$dateTime->add($interval); |
61
|
|
|
|
62
|
|
|
return new self($dateTime); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param DateInterval $interval |
67
|
|
|
* @return UtcDateTime |
68
|
|
|
*/ |
69
|
|
|
public function sub(DateInterval $interval) |
70
|
|
|
{ |
71
|
|
|
$dateTime = clone $this->dateTime; |
72
|
|
|
$dateTime->sub($interval); |
73
|
|
|
|
74
|
|
|
return new self($dateTime); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param UtcDateTime $dateTime |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
public function comesBefore(UtcDateTime $dateTime) |
82
|
|
|
{ |
83
|
|
|
return $this->dateTime < $dateTime->dateTime; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param UtcDateTime $dateTime |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
|
|
public function comesBeforeOrIsEqual(UtcDateTime $dateTime) |
91
|
|
|
{ |
92
|
|
|
return $this->dateTime <= $dateTime->dateTime; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param UtcDateTime $dateTime |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
|
|
public function comesAfter(UtcDateTime $dateTime) |
100
|
|
|
{ |
101
|
|
|
return $this->dateTime > $dateTime->dateTime; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param UtcDateTime $dateTime |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
|
|
public function comesAfterOrIsEqual(UtcDateTime $dateTime) |
109
|
|
|
{ |
110
|
|
|
return $this->dateTime >= $dateTime->dateTime; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $format |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function format($format) |
118
|
|
|
{ |
119
|
|
|
$formatted = $this->dateTime->format($format); |
120
|
|
|
|
121
|
|
|
if ($formatted === false) { |
122
|
|
|
throw new InvalidArgumentException(sprintf( |
123
|
|
|
'Given format "%s" is not a valid format for DateTime', |
124
|
|
|
$format |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $formatted; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string An ISO 8601 representation of this DateTime. |
133
|
|
|
*/ |
134
|
|
|
public function __toString() |
135
|
|
|
{ |
136
|
|
|
return $this->format(self::FORMAT); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|