-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInterval.php
More file actions
146 lines (130 loc) · 4.31 KB
/
Interval.php
File metadata and controls
146 lines (130 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Icecave\Chrono\Interval;
use Icecave\Chrono\DateTime;
use Icecave\Chrono\Detail\Iso8601;
use Icecave\Chrono\Iso8601Interface;
use Icecave\Chrono\TimePointInterface;
use Icecave\Chrono\TimeSpan\Period;
use InvalidArgumentException;
/**
* An interval represents a stretch of time between two known time points.
*/
class Interval extends AbstractInterval implements Iso8601Interface
{
/**
* @param TimePointInterface $start The start of the interval.
* @param TimePointInterface $end The start of the interval.
*/
public function __construct(TimePointInterface $start, TimePointInterface $end)
{
if ($start->isGreaterThan($end)) {
throw new InvalidArgumentException('Start point must not be greater than end point.');
}
$this->start = $start;
$this->end = $end;
}
/**
* Standard interval formats:
* <start datetime>/<end datetime>
* <start datetime>/<duration>
* <duration>/<end datetime>
* <duration>
*
* @link http://en.wikipedia.org/wiki/ISO_8601#Time_intervals
*
* Note: Duration only format is not supported.
*
* @param string $isoString A string containing an interval in any ISO-8601 compatible interval format.
*
* @return Interval The Interval constructed from the ISO compatible string.
*/
public static function fromIsoString($isoString)
{
$result = Iso8601::parseInterval($isoString);
$type = $result['type'];
$interval = $result['interval'];
if ($type === 'duration/datetime') {
list($duration, $end) = $interval;
$period = Period::fromIsoString($duration);
$end = DateTime::fromIsoString($end);
$start = $period->inverse()->resolveToTimePoint($end);
} elseif ($type === 'datetime/duration') {
list($start, $duration) = $interval;
$start = DateTime::fromIsoString($start);
$period = Period::fromIsoString($duration);
$end = $period->resolveToTimePoint($start);
} else {
list($start, $end) = $interval;
$start = DateTime::fromIsoString($start);
$end = DateTime::fromIsoString($end);
}
return new self($start, $end);
}
/**
* @return TimePointInterface The start of the interval.
*/
public function start()
{
return $this->start;
}
/**
* @return TimePointInterface The end of the interval.
*/
public function end()
{
return $this->end;
}
/**
* @return string A string representing this object in an ISO compatible format (YYYY-MM-DDThh:mm:ss[+-]hh:mm/PnYnMnDTnHnMnS).
*/
public function isoStringWithPeriod()
{
$start = $this->start();
$start = Iso8601::formatDateTime(
$start->year(),
$start->month(),
$start->day(),
$start->hour(),
$start->minute(),
$start->second(),
$start->timeZone()->isoString()
);
return $start . '/' . $this->period()->isoString();
}
/**
* @return string A string representing this object in an ISO compatible format (YYYY-MM-DDThh:mm:ss[+-]hh:mm/YYYY-MM-DDThh:mm:ss[+-]hh:mm).
*/
public function isoString()
{
$start = $this->start();
$start = Iso8601::formatDateTime(
$start->year(),
$start->month(),
$start->day(),
$start->hour(),
$start->minute(),
$start->second(),
$start->timeZone()->isoString()
);
$end = $this->end();
$end = Iso8601::formatDateTime(
$end->year(),
$end->month(),
$end->day(),
$end->hour(),
$end->minute(),
$end->second(),
$end->timeZone()->isoString()
);
return $start . '/' . $end;
}
/**
* @return string A string representing this object in an ISO compatible format (YYYY-MM-DDThh:mm:ss[+-]hh:mm/YYYY-MM-DDThh:mm:ss[+-]hh:mm).
*/
public function __toString()
{
return $this->isoString();
}
private $start;
private $end;
}