Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build period with given timezone #3041

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,19 @@ private static function makeInterval(mixed $input): ?CarbonInterval
}
}

private static function makeTimezone(mixed $input): ?CarbonTimeZone
{
if (!\is_string($input)) {
return null;
}

try {
return CarbonTimeZone::create($input);
} catch (Throwable) {
return null;
}
}

/**
* Register a custom macro.
*
Expand Down Expand Up @@ -633,13 +646,14 @@ public function __construct(...$arguments)
}

$optionsSet = false;
$originalArguments = [];
$sortedArguments = [];

foreach ($arguments as $argument) {
$parsedDate = null;

if ($argument instanceof DateTimeZone) {
$this->setTimezone($argument);
$sortedArguments = $this->configureTimezone($argument, $sortedArguments, $originalArguments);
} elseif (!isset($sortedArguments['interval']) &&
(
(\is_string($argument) && preg_match(
Expand All @@ -655,8 +669,10 @@ public function __construct(...$arguments)
$sortedArguments['interval'] = $parsedInterval;
} elseif (!isset($sortedArguments['start']) && $parsedDate = $this->makeDateTime($argument)) {
$sortedArguments['start'] = $parsedDate;
$originalArguments['start'] = $argument;
} elseif (!isset($sortedArguments['end']) && ($parsedDate = $parsedDate ?? $this->makeDateTime($argument))) {
$sortedArguments['end'] = $parsedDate;
$originalArguments['end'] = $argument;
} elseif (!isset($sortedArguments['recurrences']) &&
!isset($sortedArguments['end']) &&
(\is_int($argument) || \is_float($argument))
Expand All @@ -666,6 +682,8 @@ public function __construct(...$arguments)
} elseif (!$optionsSet && (\is_int($argument) || $argument === null)) {
$optionsSet = true;
$sortedArguments['options'] = (((int) $this->options) | ((int) $argument));
} elseif ($parsedTimezone = self::makeTimezone($argument)) {
$sortedArguments = $this->configureTimezone($parsedTimezone, $sortedArguments, $originalArguments);
} else {
throw new InvalidPeriodParameterException('Invalid constructor parameters.');
}
Expand Down Expand Up @@ -953,11 +971,13 @@ public function getOptions(): int
*/
public function toggleOptions(int $options, ?bool $state = null): static
{
$self = $this->copyIfImmutable();

if ($state === null) {
$state = ($this->options & $options) !== $options;
}

return $this->setOptions(
return $self->setOptions(
$state ?
$this->options | $options :
$this->options & ~$options,
Expand Down Expand Up @@ -1244,7 +1264,7 @@ public function setRecurrences(int|float|null $recurrences): static
*/
public function setStartDate(mixed $date, ?bool $inclusive = null): static
{
if (!$this->isInfiniteDate($date) && !($date = ([$this->dateClass, 'make'])($date))) {
if (!$this->isInfiniteDate($date) && !($date = ([$this->dateClass, 'make'])($date, $this->timezone))) {
throw new InvalidPeriodDateException('Invalid start date.');
}

Expand All @@ -1270,7 +1290,7 @@ public function setStartDate(mixed $date, ?bool $inclusive = null): static
*/
public function setEndDate(mixed $date, ?bool $inclusive = null): static
{
if ($date !== null && !$this->isInfiniteDate($date) && !$date = ([$this->dateClass, 'make'])($date)) {
if ($date !== null && !$this->isInfiniteDate($date) && !$date = ([$this->dateClass, 'make'])($date, $this->timezone)) {
throw new InvalidPeriodDateException('Invalid end date.');
}

Expand Down Expand Up @@ -2378,7 +2398,7 @@ protected function prepareForReturn(CarbonInterface $date)
$date = ([$this->dateClass, 'make'])($date);

if ($this->timezone) {
$date = $date->setTimezone($this->timezone);
return $date->setTimezone($this->timezone);
}

return $date;
Expand Down Expand Up @@ -2541,4 +2561,19 @@ private function setFromAssociativeArray(array $parameters): void
$this->setOptions($parameters['options']);
}
}

private function configureTimezone(DateTimeZone $timezone, array $sortedArguments, array $originalArguments): array
{
$this->setTimezone($timezone);

if (\is_string($originalArguments['start'] ?? null)) {
$sortedArguments['start'] = $this->makeDateTime($originalArguments['start']);
}

if (\is_string($originalArguments['end'] ?? null)) {
$sortedArguments['end'] = $this->makeDateTime($originalArguments['end']);
}

return $sortedArguments;
}
}
4 changes: 2 additions & 2 deletions src/Carbon/Traits/Creator.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ public static function createFromLocaleIsoFormat(string $format, string $locale,
*
* @return static|null
*/
public static function make($var): ?self
public static function make($var, DateTimeZone|string|null $timezone = null): ?self
{
if ($var instanceof DateTimeInterface) {
return static::instance($var);
Expand All @@ -888,7 +888,7 @@ public static function make($var): ?self
!preg_match('/^R\d/', $var) &&
preg_match('/[a-z\d]/i', $var)
) {
$date = static::parse($var);
$date = static::parse($var, $timezone);
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/CarbonPeriod/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,13 +481,44 @@ public function testCreateEmpty()
public function testCreateFromDateStringsWithTimezones()
{
$periodClass = static::$periodClass;
$periodClass = \Carbon\CarbonPeriodImmutable::class;
$period = $periodClass::create(
$start = '2018-03-25 10:15:30 Europe/Oslo',
$end = '2018-03-28 17:25:30 Asia/Kamchatka',
);

$this->assertSame('2018-03-25 10:15:30 Europe/Oslo', $period->first()->format('Y-m-d H:i:s e'));
$this->assertSame('2018-03-27 10:15:30 Europe/Oslo', $period->last()->format('Y-m-d H:i:s e'));
$this->assertSame($start, $period->getStartDate()->format('Y-m-d H:i:s e'));
$this->assertSame($end, $period->getEndDate()->format('Y-m-d H:i:s e'));

$period = $periodClass::create(
'2024-01-01',
'2024-01-05',
\Carbon\CarbonTimeZone::create('Australia/Melbourne'),
);
$this->assertSame('Australia/Melbourne', $period->first()->timezone->getName());
$this->assertSame('Australia/Melbourne', $period->last()->timezone->getName());
$this->assertSame('Australia/Melbourne', $period->getStartDate()->timezone->getName());
$this->assertSame('Australia/Melbourne', $period->getEndDate()->timezone->getName());
$this->assertSame('2024-01-01 00:00:00 Australia/Melbourne', $period->first()->format('Y-m-d H:i:s e'));
$this->assertSame('2024-01-05 00:00:00 Australia/Melbourne', $period->last()->format('Y-m-d H:i:s e'));
$this->assertSame('2024-01-01 00:00:00 Australia/Melbourne', $period->getStartDate()->format('Y-m-d H:i:s e'));
$this->assertSame('2024-01-05 00:00:00 Australia/Melbourne', $period->getEndDate()->format('Y-m-d H:i:s e'));

$period = $periodClass::create(
'2024-01-01',
'2024-01-05',
'Australia/Melbourne',
);
$this->assertSame('Australia/Melbourne', $period->first()->timezone->getName());
$this->assertSame('Australia/Melbourne', $period->last()->timezone->getName());
$this->assertSame('Australia/Melbourne', $period->getStartDate()->timezone->getName());
$this->assertSame('Australia/Melbourne', $period->getEndDate()->timezone->getName());
$this->assertSame('2024-01-01 00:00:00 Australia/Melbourne', $period->first()->format('Y-m-d H:i:s e'));
$this->assertSame('2024-01-05 00:00:00 Australia/Melbourne', $period->last()->format('Y-m-d H:i:s e'));
$this->assertSame('2024-01-01 00:00:00 Australia/Melbourne', $period->getStartDate()->format('Y-m-d H:i:s e'));
$this->assertSame('2024-01-05 00:00:00 Australia/Melbourne', $period->getEndDate()->format('Y-m-d H:i:s e'));
}

public function testCreateWithIntervalInFromStringFormat()
Expand Down
Loading