diff --git a/src/MonthDay.php b/src/MonthDay.php index 7b1ddb4..207dae8 100644 --- a/src/MonthDay.php +++ b/src/MonthDay.php @@ -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`. */ @@ -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); } } diff --git a/src/YearMonth.php b/src/YearMonth.php index 6c4e1b6..b710e0c 100644 --- a/src/YearMonth.php +++ b/src/YearMonth.php @@ -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. @@ -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); } } diff --git a/src/YearWeek.php b/src/YearWeek.php index 8a410cc..81b55c9 100644 --- a/src/YearWeek.php +++ b/src/YearWeek.php @@ -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. @@ -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); } } diff --git a/tests/YearMonthTest.php b/tests/YearMonthTest.php index d758741..b28656c 100644 --- a/tests/YearMonthTest.php +++ b/tests/YearMonthTest.php @@ -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'], + ]; } }