Skip to content

Commit

Permalink
Fix calling TimeZone::createFromPhpDateTimeZone with default values (#…
Browse files Browse the repository at this point in the history
…250)

The default value for the begin date created a date B.C. which could not be parsed.
Therefore the new default value was set to the year of Christ birth.
The daylight saving time was introduced in 1916, therefore any begin date before that year will not produce further output.

Calling TimeZone::createFromPhpDateTimeZone with default values will now trigger a (silenced) deprecation error.

Fix #240
  • Loading branch information
markuspoerschke authored Apr 21, 2021
1 parent f3b9cfb commit 74ea814
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 72 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- TZOFFSETTO and TZOFFSETFROM should never be -0000 [#246](https://github.com/markuspoerschke/iCal/pull/246)
- Calling TimeZone::createFromPhpDateTimeZone with default values fails with assertion

### Deprecated

- Method `Eluceo\iCal\Domain\Entity\TimeZone::createFromPhpDateTimeZone` will not have default values
for `$beginDateTime` and `$endDateTime` in the next major version.

## [2.0.0] - 2021-03-29

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
],
"require": {
"php": ">=7.4 || ~8.0.0",
"ext-mbstring": "*"
"ext-mbstring": "*",
"symfony/deprecation-contracts": "^2.1"
},
"conflict": {
"php": "7.4.6"
Expand Down
139 changes: 70 additions & 69 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/Domain/Entity/TimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public static function createFromPhpDateTimeZone(
?DateTimeInterface $beginDateTime = null,
?DateTimeInterface $endDateTime = null
): self {
if ($beginDateTime === null || $endDateTime === null) {
trigger_deprecation('eluceo/ical', '2.1.0', 'Relying on the default values for begin and end date when calling TimeZone::createFromPhpDateTimeZone() is deprecated. Please provide a begin and an end date.');
}

$transitions = $phpDateTimeZone->getTransitions(
$beginDateTime ? $beginDateTime->getTimestamp() : PHP_INT_MIN,
$beginDateTime ? $beginDateTime->getTimestamp() : (new DateTimeImmutable('0000-01-01 12:00:00'))->getTimestamp(),
$endDateTime ? $endDateTime->getTimestamp() : PHP_INT_MAX
);
$timeZone = new self($phpDateTimeZone->getName());
Expand All @@ -48,7 +52,7 @@ public static function createFromPhpDateTimeZone(
DateTimeImmutable::ISO8601,
$transitionArray['time']
);
assert($fromDateTime instanceof DateTimeImmutable);
assert($fromDateTime instanceof DateTimeImmutable, $transitionArray['time']);
$localFromDateTime = $fromDateTime->setTimezone($phpDateTimeZone);

$timeZone->addTransition(new TimeZoneTransition(
Expand Down

0 comments on commit 74ea814

Please sign in to comment.