• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / date-time / 20679683854

03 Jan 2026 04:05PM UTC coverage: 99.079% (-0.05%) from 99.132%
20679683854

push

github

BenMorel
Use PHP 8.2.0 with lowest deps

1829 of 1846 relevant lines covered (99.08%)

183.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/src/TimeZone.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\DateTime;
6

7
use Brick\DateTime\Parser\DateTimeParseException;
8
use DateTimeImmutable;
9
use DateTimeZone;
10
use Override;
11
use Stringable;
12

13
use const PHP_VERSION_ID;
14

15
/**
16
 * A time-zone. This is the parent class for `TimeZoneOffset` and `TimeZoneRegion`.
17
 *
18
 * * `TimeZoneOffset` represents a fixed offset from UTC such as `+02:00`.
19
 * * `TimeZoneRegion` represents a geographical region such as `Europe/London`.
20
 */
21
abstract class TimeZone implements Stringable
22
{
23
    /**
24
     * Obtains an instance of `TimeZone` from a string representation.
25
     *
26
     * @throws DateTimeParseException
27
     */
28
    public static function parse(string $text): TimeZone
29
    {
30
        if ($text === 'Z' || $text === 'z') {
285✔
31
            return TimeZoneOffset::utc();
3✔
32
        }
33

34
        if ($text === '') {
282✔
35
            throw new DateTimeParseException('The string is empty.');
1✔
36
        }
37

38
        if ($text[0] === '+' || $text[0] === '-') {
281✔
39
            return TimeZoneOffset::parse($text);
88✔
40
        }
41

42
        return TimeZoneRegion::parse($text);
193✔
43
    }
44

45
    public static function utc(): TimeZoneOffset
46
    {
47
        return TimeZoneOffset::utc();
93✔
48
    }
49

50
    /**
51
     * Returns the unique time-zone ID.
52
     *
53
     * @psalm-return non-empty-string
54
     */
55
    abstract public function getId(): string;
56

57
    /**
58
     * Returns the offset from UTC at the given instant.
59
     *
60
     * @param Instant $pointInTime The instant.
61
     *
62
     * @return int The offset from UTC in seconds.
63
     */
64
    abstract public function getOffset(Instant $pointInTime): int;
65

66
    public function isEqualTo(TimeZone $other): bool
67
    {
68
        return $this->getId() === $other->getId();
186✔
69
    }
70

71
    public static function fromNativeDateTimeZone(DateTimeZone $dateTimeZone): TimeZone
72
    {
73
        $parsed = TimeZone::parse($dateTimeZone->getName());
8✔
74

75
        /**
76
         * PHP >= 8.1.7 supports sub-minute offsets, but truncates the seconds in getName(). Only getOffset() returns
77
         * the correct offset including seconds, so let's use it to make a correction if we have an offset-based TZ.
78
         * This has been fixed in PHP 8.1.20 and PHP 8.2.7.
79
         */
80
        if ($parsed instanceof TimeZoneOffset && PHP_VERSION_ID < 8_02_07) {
8✔
81
            return TimeZoneOffset::ofTotalSeconds($dateTimeZone->getOffset(new DateTimeImmutable()));
×
82
        }
83

84
        return $parsed;
8✔
85
    }
86

87
    /**
88
     * Returns an equivalent native `DateTimeZone` object for this TimeZone.
89
     */
90
    abstract public function toNativeDateTimeZone(): DateTimeZone;
91

92
    /**
93
     * @psalm-return non-empty-string
94
     */
95
    #[Override]
96
    public function __toString(): string
97
    {
98
        return $this->getId();
652✔
99
    }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc