Skip to content

Commit

Permalink
Merge pull request #14 from michael-rubel/feature/localization-in-dat…
Browse files Browse the repository at this point in the history
…e-formatters

Localization in date formatters
  • Loading branch information
michael-rubel committed Aug 30, 2022
2 parents 68f8329 + ee8e2cb commit cdf1baf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
9 changes: 4 additions & 5 deletions src/Collection/DateFormatter.php
Expand Up @@ -20,13 +20,12 @@ public function __construct(
public string|null $timezone = null,
public string|null $date_format = 'Y-m-d',
) {
if (! $this->timezone) {
$this->timezone = config('app.timezone', 'UTC');
}

if (! $this->date instanceof CarbonInterface) {
$this->date = app(Carbon::class)->parse($this->date);
}

$this->timezone = $this->timezone ?? config('app.timezone', 'UTC');
$this->date_format = $this->date_format ?? 'Y-m-d';
}

/**
Expand All @@ -38,6 +37,6 @@ public function format(): string
{
return $this->date
->setTimezone($this->timezone)
->format($this->date_format);
->translatedFormat($this->date_format);
}
}
9 changes: 4 additions & 5 deletions src/Collection/DateTimeFormatter.php
Expand Up @@ -20,13 +20,12 @@ public function __construct(
public string|null $timezone = null,
public string|null $datetime_format = 'Y-m-d H:i',
) {
if (! $this->timezone) {
$this->timezone = config('app.timezone', 'UTC');
}

if (! $this->datetime instanceof CarbonInterface) {
$this->datetime = app(Carbon::class)->parse($this->datetime);
}

$this->timezone = $this->timezone ?? config('app.timezone', 'UTC');
$this->datetime_format = $this->datetime_format ?? 'Y-m-d H:i';
}

/**
Expand All @@ -38,6 +37,6 @@ public function format(): string
{
return $this->datetime
->setTimezone($this->timezone)
->format($this->datetime_format);
->translatedFormat($this->datetime_format);
}
}
12 changes: 12 additions & 0 deletions tests/DateFormatterTest.php
Expand Up @@ -146,4 +146,16 @@ public function testCanExtendDateTimeFormatter()
$result = format(DateFormatter::class, '2021-10-31');
$this->assertEquals('31-10-2021', $result);
}

/** @test */
public function testCanFormatDateLocalized()
{
Carbon::setLocale('en');
$result = format(DateFormatter::class, now(), 'UTC', 'd F Y');
$this->assertSame('30 October 2021', $result);

Carbon::setLocale('pl');
$result = format(DateFormatter::class, now(), 'Europe/Warsaw', 'd F Y');
$this->assertSame('30 października 2021', $result);
}
}
14 changes: 13 additions & 1 deletion tests/DateTimeFormatterTest.php
Expand Up @@ -70,7 +70,7 @@ public function testFormatHelperFailsWhenMultipleArraysPassed()
{
$this->expectException(\TypeError::class);

format(DateFormatter::class, ['datetime' => '2021-10-30 15:00:00'], ['test' => true]);
format(DateTimeFormatter::class, ['datetime' => '2021-10-30 15:00:00'], ['test' => true]);
}

/** @test */
Expand Down Expand Up @@ -137,4 +137,16 @@ public function testCanExtendDateTimeFormatter()
$result = format(DateTimeFormatter::class, '2021-10-31');
$this->assertEquals('2021-10-31 02:00:00', $result);
}

/** @test */
public function testCanFormatDateTimeLocalized()
{
Carbon::setLocale('en');
$result = format(DateTimeFormatter::class, now(), 'UTC', 'd F Y H:i');
$this->assertSame('30 October 2021 14:00', $result);

Carbon::setLocale('pl');
$result = format(DateFormatter::class, now(), 'Europe/Warsaw', 'd F Y H:i');
$this->assertSame('30 października 2021 16:00', $result);
}
}

0 comments on commit cdf1baf

Please sign in to comment.