Skip to content

Commit

Permalink
Improve performances for MonthDay, YearMonth and YearWeek __toString().
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Sep 29, 2023
1 parent c81713a commit 2410d4e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/MonthDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Brick\DateTime\Parser\IsoParsers;
use JsonSerializable;

use function sprintf;

/**
* A month-day in the ISO-8601 calendar system, such as `--12-03`.
*/
Expand Down Expand Up @@ -235,6 +233,9 @@ public function jsonSerialize(): string

public function __toString(): string
{
return sprintf('--%02d-%02d', $this->month, $this->day);
return '--'
. ($this->month < 10 ? '0' . $this->month : $this->month)
. '-'
. ($this->day < 10 ? '0' . $this->day : $this->day);
}
}
9 changes: 7 additions & 2 deletions src/YearMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use Brick\DateTime\Utility\Math;
use JsonSerializable;

use function sprintf;
use function abs;
use function str_pad;

use const STR_PAD_LEFT;

/**
* Represents the combination of a year and a month.
Expand Down Expand Up @@ -300,6 +303,8 @@ public function jsonSerialize(): string
*/
public function __toString(): string
{
return sprintf('%02u-%02u', $this->year, $this->month);
return ($this->year < 1000 ? ($this->year < 0 ? '-' : '') . str_pad((string) abs($this->year), 4, '0', STR_PAD_LEFT) : $this->year)
. '-'
. ($this->month < 10 ? '0' . $this->month : $this->month);
}
}
11 changes: 7 additions & 4 deletions src/YearWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

use JsonSerializable;

use function sprintf;
use function abs;
use function str_pad;

use const STR_PAD_LEFT;

/**
* Represents the combination of a year and a week.
Expand Down Expand Up @@ -271,8 +274,8 @@ public function jsonSerialize(): string

public function __toString(): string
{
$pattern = ($this->year < 0 ? '%05d' : '%04d') . '-W%02d';

return sprintf($pattern, $this->year, $this->week);
return ($this->year < 1000 ? ($this->year < 0 ? '-' : '') . str_pad((string) abs($this->year), 4, '0', STR_PAD_LEFT) : $this->year)
. '-W'
. ($this->week < 10 ? '0' . $this->week : $this->week);
}
}
41 changes: 37 additions & 4 deletions tests/YearMonthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,13 +459,46 @@ public function providerToLocalDateRange(): array
];
}

public function testJsonSerialize(): void
/**
* @dataProvider providerToString
*/
public function testJsonSerialize(int $year, int $week, string $expected): void
{
$yearWeek = YearMonth::of($year, $week);
self::assertSame(json_encode($expected), json_encode($yearWeek));
}

/**
* @dataProvider providerToString
*
* @param int $year The year.
* @param int $month The month.
* @param string $expected The expected result string.
*/
public function testToString(int $year, int $month, string $expected): void
{
self::assertSame(json_encode('2013-09'), json_encode(YearMonth::of(2013, 9)));
self::assertSame($expected, (string) YearMonth::of($year, $month));
}

public function testToString(): void
public function providerToString(): array
{
self::assertSame('2013-09', (string) YearMonth::of(2013, 9));
return [
[-999999, 12, '-999999-12'],
[-185321, 11, '-185321-11'],
[-18532, 11, '-18532-11'],
[-2023, 11, '-2023-11'],
[-2023, 11, '-2023-11'],
[-2023, 1, '-2023-01'],
[-999, 1, '-0999-01'],
[-2, 1, '-0002-01'],
[2, 1, '0002-01'],
[999, 1, '0999-01'],
[2023, 1, '2023-01'],
[2023, 11, '2023-11'],
[2023, 11, '2023-11'],
[18532, 11, '18532-11'],
[185321, 11, '185321-11'],
[999999, 12, '999999-12'],
];
}
}

0 comments on commit 2410d4e

Please sign in to comment.