Skip to content

Commit 77828a3

Browse files
committed
Fix update script for entry date fields.
1 parent 0611396 commit 77828a3

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

src/UpdateScripts/ConvertDatesToUtc.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ private function updateDateFields($item, Fields $fields, ?string $dottedPrefix =
5454
&& empty($dottedPrefix)
5555
&& $field->handle() === 'date'
5656
) {
57-
$item->date($item->date());
57+
$existingDate = $item->date();
58+
$localDate = Carbon::parse($existingDate->format('Y-m-d H:i:s'), config('app.timezone'));
59+
$convertedDate = $localDate->setTimezone('UTC');
60+
61+
$item->date($convertedDate);
5862

5963
return;
6064
}

tests/UpdateScripts/ConvertDatesToUtcTest.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
namespace Tests\UpdateScripts;
44

5-
use Illuminate\Support\Carbon;
65
use PHPUnit\Framework\Attributes\DataProvider;
76
use PHPUnit\Framework\Attributes\Test;
8-
use ReflectionClass;
97
use Statamic\Facades\Blueprint;
108
use Statamic\Facades\Collection;
119
use Statamic\Facades\Entry;
1210
use Statamic\Facades\Fieldset;
1311
use Statamic\Facades\GlobalSet;
14-
use Statamic\Facades\Stache;
1512
use Statamic\Facades\Taxonomy;
1613
use Statamic\Facades\Term;
1714
use Statamic\Facades\User;
@@ -71,24 +68,11 @@ public function it_converts_entry_date_field_in_entries()
7168
],
7269
])->save();
7370

74-
// We want to set the time to 12pm in America/New_York, which is 5pm in UTC.
75-
// Since Entry::date() automatically converts the value to UTC, we need to set it directly.
76-
// It's naughty, but it's the only way I could find to set the "old" value.
77-
$entry = Entry::make()->id('foo')->collection('articles');
78-
79-
$reflection = new ReflectionClass($entry);
80-
$property = $reflection->getProperty('date');
81-
$property->setAccessible(true);
82-
$property->setValue($entry, Carbon::parse('2025-01-01 12:00', 'America/New_York'));
83-
71+
$entry = Entry::make()->id('foo')->collection('articles')->date('2025-01-01-1200');
8472
$entry->save();
8573

8674
$this->runUpdateScript(ConvertDatesToUtc::class);
8775

88-
// We're clearing the Stache here to ensure the Carbon instance above doesn't end
89-
// up being the same one we're testing against below.
90-
Stache::clear();
91-
9276
$entry = Entry::find($entry->id());
9377

9478
$this->assertEquals('2025-01-01 17:00', $entry->date()->format('Y-m-d H:i'));

0 commit comments

Comments
 (0)