1
|
|
|
<?php |
2
|
|
|
namespace Wandu\DateTime; |
3
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
class Time |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param \Carbon\Carbon $carbon |
14
|
|
|
* @return \Wandu\DateTime\Time |
15
|
|
|
*/ |
16
|
1 |
|
public static function fromCarbon(Carbon $carbon) |
17
|
|
|
{ |
18
|
1 |
|
if (!class_exists(Carbon::class)) { |
19
|
|
|
throw new RuntimeException('Unable to fromCarbon. the Carbon is not installed.'); |
20
|
|
|
} |
21
|
1 |
|
return static::fromTimes($carbon->hour, $carbon->minute, $carbon->second, $carbon->timezone); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $text |
26
|
|
|
* @param \DateTimeZone|string|int $timezone |
27
|
|
|
* @return \Wandu\DateTime\Time |
28
|
|
|
*/ |
29
|
2 |
|
public static function fromText(string $text, $timezone = null) |
30
|
|
|
{ |
31
|
2 |
|
$split = explode(':', $text); |
32
|
2 |
|
return static::fromTimes( |
33
|
2 |
|
(int)($split[0] ?? 0), |
34
|
2 |
|
(int)($split[1] ?? 0), |
35
|
2 |
|
(int)($split[2] ?? 0), |
36
|
|
|
$timezone |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int $hours |
42
|
|
|
* @param int $minutes |
43
|
|
|
* @param int $seconds |
44
|
|
|
* @param \DateTimeZone|string|int $timezone |
45
|
|
|
* @return \Wandu\DateTime\Time |
46
|
|
|
*/ |
47
|
16 |
|
public static function fromTimes(int $hours, int $minutes = 0, int $seconds = 0, $timezone = null) |
48
|
|
|
{ |
49
|
16 |
|
return new Time($hours * 3600 + $minutes * 60 + $seconds, $timezone); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** @var int */ |
53
|
|
|
protected $timestamp; |
54
|
|
|
|
55
|
|
|
/** @var \DateTimeZone */ |
56
|
|
|
protected $timezone; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int $timestamp |
60
|
|
|
* @param \DateTimeZone|string|int $timezone |
61
|
|
|
*/ |
62
|
17 |
|
public function __construct(int $timestamp, $timezone = null) |
63
|
|
|
{ |
64
|
17 |
|
$this->setTimestamp($timestamp); |
65
|
17 |
|
$this->timezone = static::safeCreateDateTimeZone($timezone); |
66
|
17 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $timestamp |
70
|
|
|
* @return static |
71
|
|
|
*/ |
72
|
17 |
|
public function setTimestamp($timestamp) |
73
|
|
|
{ |
74
|
17 |
|
while ($timestamp < 0) { // 음수처리 |
75
|
5 |
|
$timestamp += 86400; |
76
|
|
|
} |
77
|
17 |
|
$this->timestamp = $timestamp % 86400; |
78
|
17 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \DateTimeZone|string|int $timezone |
83
|
|
|
*/ |
84
|
1 |
|
public function setTimezone($timezone) |
85
|
|
|
{ |
86
|
1 |
|
$now = new DateTime(); |
87
|
|
|
|
88
|
|
|
// get offset |
89
|
1 |
|
$timezone = static::safeCreateDateTimeZone($timezone); |
90
|
1 |
|
$offset = $timezone->getOffset($now) - $this->timezone->getOffset($now); |
91
|
|
|
|
92
|
1 |
|
$this->timezone = $timezone; |
93
|
1 |
|
$this->setTimestamp($this->timestamp + $offset); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
4 |
|
public function __toString() |
100
|
|
|
{ |
101
|
4 |
|
return sprintf('%02d:%02d:%02d', $this->hours(), $this->minutes(), $this->seconds()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return int |
106
|
|
|
*/ |
107
|
16 |
|
public function seconds(): int |
108
|
|
|
{ |
109
|
16 |
|
return (int)($this->timestamp % 60); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
16 |
|
public function minutes(): int |
116
|
|
|
{ |
117
|
16 |
|
return (int)(floor($this->timestamp / 60) % 60); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
16 |
|
public function hours(): int |
124
|
|
|
{ |
125
|
16 |
|
return (int) floor($this->timestamp / 3600); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @ref https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php#L228 |
130
|
|
|
* |
131
|
|
|
* @param \DateTimeZone|string|int $object |
132
|
|
|
* @return \DateTimeZone |
133
|
|
|
*/ |
134
|
17 |
|
protected static function safeCreateDateTimeZone($object) |
135
|
|
|
{ |
136
|
17 |
|
if ($object === null) { |
137
|
15 |
|
return new DateTimeZone(date_default_timezone_get()); |
138
|
|
|
} |
139
|
2 |
|
if ($object instanceof DateTimeZone) { |
140
|
1 |
|
return $object; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
if (is_numeric($object)) { |
144
|
|
|
$tzName = timezone_name_from_abbr(null, $object * 3600, true); |
145
|
|
|
if ($tzName === false) { |
146
|
|
|
throw new InvalidArgumentException('Unknown or bad timezone ('.$object.')'); |
147
|
|
|
} |
148
|
|
|
$object = $tzName; |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
$tz = @timezone_open((string) $object); |
152
|
1 |
|
if ($tz === false) { |
153
|
|
|
throw new InvalidArgumentException('Unknown or bad timezone ('.$object.')'); |
154
|
|
|
} |
155
|
1 |
|
return $tz; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|