Skip to content

Commit

Permalink
Add statamic.system.localize_dates_in_modifiers config option
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Feb 28, 2025
1 parent 4dc546f commit f66bcf0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@

'display_timezone' => 'UTC',

/*
|--------------------------------------------------------------------------
| Localize Dates in Modifiers?
|--------------------------------------------------------------------------
|
| Since Statamic stores dates in UTC, any modifiers you chain onto a date
| field will be working with the UTC value. If you'd prefer modifiers to
| always use your `display_timezone`, set this to `true`.
|
*/

'localize_dates_in_modifiers' => false,

/*
|--------------------------------------------------------------------------
| Default Character Set
Expand Down
4 changes: 4 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3218,6 +3218,10 @@ private function carbon($value)
$value = (is_numeric($value)) ? Date::createFromTimestamp($value, config('app.timezone')) : Date::parse($value);
}

if (config('statamic.system.localize_dates_in_modifiers')) {
$value->setTimezone(config('statamic.system.display_timezone'));
}

return $value;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Modifiers/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function it_formats_date()
$this->assertSame('1st January 2025 3:45pm', $this->modify(Carbon::parse('2025-01-01 15:45'), 'jS F Y g:ia'));
}

#[Test]
public function it_formats_date_and_outputs_in_display_timezone()
{
config()->set('statamic.system.localize_dates_in_modifiers', true);

$this->assertSame('1st January 2025 4:45pm', $this->modify(Carbon::parse('2025-01-01 15:45'), 'jS F Y g:ia'));
}

public function modify($value, $format)
{
return Modify::value($value)->format($format)->fetch();
Expand Down
8 changes: 8 additions & 0 deletions tests/Modifiers/FormatTranslatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function it_formats_date()
$this->assertSame('Mittwoch 1 Januar 2025, 15:45', $this->modify(Carbon::parse('2025-01-01 15:45'), 'l j F Y, H:i'));
}

#[Test]
public function it_formats_date_and_outputs_in_display_timezone()
{
config()->set('statamic.system.localize_dates_in_modifiers', true);

$this->assertSame('Mittwoch 1 Januar 2025, 16:45', $this->modify(Carbon::parse('2025-01-01 15:45'), 'l j F Y, H:i'));
}

public function modify($value, $format)
{
return Modify::value($value)->formatTranslated($format)->fetch();
Expand Down
8 changes: 8 additions & 0 deletions tests/Modifiers/IsoFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function it_formats_date()
$this->assertSame('2025.01.01 15:45', $this->modify(Carbon::parse('2025-01-01 15:45'), 'YYYY.MM.DD HH:mm'));
}

#[Test]
public function it_formats_date_and_outputs_in_display_timezone()
{
config()->set('statamic.system.localize_dates_in_modifiers', true);

$this->assertSame('2025.01.01 16:45', $this->modify(Carbon::parse('2025-01-01 15:45'), 'YYYY.MM.DD HH:mm'));
}

public function modify($value, $format)
{
return Modify::value($value)->isoFormat($format)->fetch();
Expand Down

0 comments on commit f66bcf0

Please sign in to comment.