-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ZoneOffset.php
50 lines (41 loc) · 964 Bytes
/
ZoneOffset.php
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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Clock;
use Stringable;
/**
* @author Joshua Estes <[email protected]>
*/
final readonly class ZoneOffset implements ZoneOffsetInterface, Stringable
{
public function __construct(
private int $seconds,
) {}
public function __toString(): string
{
return $this->toString();
}
public function toString(): string
{
$lead = '+';
$hours = $this->getHours();
$minutes = (abs($this->getSeconds()) % (60 * 60)) / 60;
if ($hours < 0) {
$lead = '-';
$hours = abs($hours);
}
return sprintf(
'%s%02d:%02d',
$lead,
$hours,
$minutes
);
}
public function getSeconds(): int
{
return $this->seconds;
}
public function getHours(): int
{
return (int) ($this->getSeconds() / 60 / 60);
}
}