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

brick / date-time / 16647816368

31 Jul 2025 11:32AM UTC coverage: 99.022% (-0.05%) from 99.075%
16647816368

Pull #120

github

web-flow
Merge add9a6a8a into d8bbfbc44
Pull Request #120: Add a helper TimeZone::fromDefaultTimeZone static constructor

2 of 3 new or added lines in 1 file covered. (66.67%)

1 existing line in 1 file now uncovered.

1823 of 1841 relevant lines covered (99.02%)

140.2 hits per line

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

89.47
/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 Stringable;
11

12
use const PHP_VERSION_ID;
13

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

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

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

41
        return TimeZoneRegion::parse($text);
211✔
42
    }
43

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

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

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

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

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

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

88
        return $parsed;
9✔
89
    }
90

91
    public static function fromDefaultTimeZone(): TimeZone
92
    {
93
        return static::fromNativeDateTimeZone(date_default_timezone_get() === false
1✔
NEW
94
            ? new DateTimeZone('UTC')
×
95
            : new DateTimeZone(date_default_timezone_get()));
1✔
96
    }
97

98
    /**
99
     * Returns an equivalent native `DateTimeZone` object for this TimeZone.
100
     */
101
    abstract public function toNativeDateTimeZone(): DateTimeZone;
102

103
    /**
104
     * @psalm-return non-empty-string
105
     */
106
    public function __toString(): string
107
    {
108
        return $this->getId();
298✔
109
    }
110
}
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