-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimeZone.php
More file actions
134 lines (118 loc) · 4.1 KB
/
TimeZone.php
File metadata and controls
134 lines (118 loc) · 4.1 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
<?php
namespace Icecave\Chrono;
use Icecave\Chrono\Detail\Iso8601;
use Icecave\Chrono\Format\DefaultFormatter;
use Icecave\Chrono\Format\FormattableInterface;
use Icecave\Chrono\Format\FormatterInterface;
use Icecave\Parity\Exception\NotComparableException;
use Icecave\Parity\ExtendedComparableTrait;
use Icecave\Parity\SubClassComparable;
class TimeZone implements Iso8601Interface, FormattableInterface, SubClassComparable
{
use ExtendedComparableTrait;
/**
* @param int $offset The offset from UTC in seconds.
* @param bool $isDst True if the timezone is currently honouring daylight saving time; otherwise, false.
*/
public function __construct($offset = 0, $isDst = false)
{
$offset = $offset % 86400;
$this->offset = intval(round($offset / 60)) * 60;
$this->isDst = $isDst;
}
/**
* Standard time zone formats:
* Z
* +/-hh
* +/-hhmm
* +/-hh:mm
*
* @link http://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators
*
* @param string $isoString A string containing a time zone in any ISO-8601 compatible time zone format, with the exception of allowing negative zero's.
* @param bool $isDst True if the time zone is currently honouring daylight saving time; otheriwse, false.
*
* @return TimeZone The TimeZone constructed from the ISO compatible string.
*/
public static function fromIsoString($isoString, $isDst = false)
{
$offset = Iso8601::parseTimeZone($isoString);
return new self($offset, $isDst);
}
/**
* @return int The offset from UTC in seconds.
*/
public function offset()
{
return $this->offset;
}
/**
* @return bool True if the timezone represents UTC (0 offset, no DST).
*/
public function isUtc()
{
return $this->offset() === 0
&& !$this->isDst();
}
/**
* @return bool True if the timezone is currently honouring daylight saving time; otheriwse, false.
*/
public function isDst()
{
return $this->isDst;
}
/**
* Compare this object with another value, yielding a result according to the following table:
*
* +--------------------+---------------+
* | Condition | Result |
* +--------------------+---------------+
* | $this == $value | $result === 0 |
* | $this < $value | $result < 0 |
* | $this > $value | $result > 0 |
* +--------------------+---------------+
*
* @param mixed $timeZone The timezone to compare.
*
* @return int 0 if $this and $timeZone are equal, <0 if $this < $timeZone, or >0 if $this > $timeZone.
* @throws NotComparableException Indicates that the implementation does not know how to compare $this to $timeZone.
*/
public function compare($timeZone): int
{
if (!$timeZone instanceof self) {
throw new NotComparableException($this, $timeZone);
}
return $this->offset() - $timeZone->offset()
?: intval($this->isDst()) - intval($timeZone->isDst());
}
/**
* @param string $formatSpecifier The format of the output string.
* @param FormatterInterface|null $formatter The formatter to use, or null to use the default.
*
* @return string The formatted string.
*/
public function format($formatSpecifier, FormatterInterface $formatter = null)
{
if (null === $formatter) {
$formatter = DefaultFormatter::instance();
}
return $formatter->formatTimeZone($this, $formatSpecifier);
}
/**
* @return string A string representing this object in an ISO compatible time zone format ([+-]hh:mm).
*/
public function isoString()
{
return Iso8601::formatTimeZone($this->offset());
}
/**
* @return string A string representing this object in an ISO compatible time zone format ([+-]hh:mm).
*/
public function __toString()
{
return $this->isoString();
}
private $timeZone;
private $offset;
private $isDst;
}