briannesbitt / Carbon
Showing 1 of 2 files from the diff.

@@ -12,9 +12,12 @@
Loading
12 12
namespace Carbon\Traits;
13 13
14 14
use Carbon\CarbonInterface;
15 +
use Carbon\CarbonTimeZone;
15 16
use Closure;
16 17
use DateTimeImmutable;
17 18
use DateTimeInterface;
19 +
use InvalidArgumentException;
20 +
use Throwable;
18 21
19 22
trait Test
20 23
{
@@ -84,13 +87,14 @@
Loading
84 87
        $useDateInstanceTimezone = $testNow instanceof DateTimeInterface;
85 88
86 89
        if ($useDateInstanceTimezone) {
87 -
            date_default_timezone_set($testNow->getTimezone()->getName());
90 +
            self::setDefaultTimezone($testNow->getTimezone()->getName(), $testNow);
88 91
        }
89 92
90 93
        static::setTestNow($testNow);
91 94
92 95
        if (!$useDateInstanceTimezone) {
93 -
            date_default_timezone_set(static::getMockedTestNow(\func_num_args() === 1 ? null : $tz)->timezone);
96 +
            $now = static::getMockedTestNow(\func_num_args() === 1 ? null : $tz);
97 +
            self::setDefaultTimezone($now->tzName, $now);
94 98
        }
95 99
    }
96 100
@@ -175,4 +179,30 @@
Loading
175 179
            ? $testInstance->rawFormat(static::MOCK_DATETIME_FORMAT)
176 180
            : $testInstance->format(static::MOCK_DATETIME_FORMAT);
177 181
    }
182 +
183 +
    private static function setDefaultTimezone($timezone, DateTimeInterface $date = null)
184 +
    {
185 +
        $previous = null;
186 +
        $success = false;
187 +
188 +
        try {
189 +
            $success = date_default_timezone_set($timezone);
190 +
        } catch (Throwable $exception) {
191 +
            $previous = $exception;
192 +
        }
193 +
194 +
        if (!$success) {
195 +
            $suggestion = @CarbonTimeZone::create($timezone)->toRegionName($date);
196 +
197 +
            throw new InvalidArgumentException(
198 +
                "Timezone ID '$timezone' is invalid".
199 +
                ($suggestion && $suggestion !== $timezone ? ", did you mean '$suggestion'?" : '.')."\n".
200 +
                "It must be one of the IDs from DateTimeZone::listIdentifiers(),\n".
201 +
                'For the record, hours/minutes offset are relevant only for a particular moment, '.
202 +
                'but not as a default timezone.',
203 +
                0,
204 +
                $previous
205 +
            );
206 +
        }
207 +
    }
178 208
}
Files Complexity Coverage
src/Carbon 2,158 100.00%
Project Totals (886 files) 2158 100.00%

No yaml found.

Create your codecov.yml to customize your Codecov experience

Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading