Skip to content

Commit 7061312

Browse files
committed
Cast entry dates to UTC
1 parent d333ad2 commit 7061312

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/Entries/EntryModel.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Statamic\Eloquent\Entries;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Database\Eloquent\Casts\Attribute;
7+
use Illuminate\Support\Carbon;
68
use Statamic\Eloquent\Database\BaseModel;
79

810
class EntryModel extends BaseModel
@@ -12,11 +14,30 @@ class EntryModel extends BaseModel
1214
protected $table = 'entries';
1315

1416
protected $casts = [
15-
'date' => 'datetime',
1617
'data' => 'json',
1718
'published' => 'boolean',
1819
];
1920

21+
public function date(): Attribute
22+
{
23+
return Attribute::make(
24+
get: function ($value) {
25+
return Carbon::parse($value, 'UTC');
26+
},
27+
set: function ($value) {
28+
if (! $value instanceof Carbon) {
29+
$value = Carbon::parse($value, 'UTC');
30+
}
31+
32+
if ($value->tzName !== 'UTC') {
33+
$value = $value->utc();
34+
}
35+
36+
return $value->format('Y-m-d H:i:s');
37+
},
38+
);
39+
}
40+
2041
public function author()
2142
{
2243
return $this->belongsTo(\App\Models\User::class, 'data->author');

tests/Entries/EntryModelTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Tests\Entries;
44

5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Illuminate\Support\Carbon;
7+
use Illuminate\Support\Facades\DB;
58
use PHPUnit\Framework\Attributes\Test;
69
use Statamic\Eloquent\Entries\EntryModel;
710
use Tests\TestCase;
811

912
class EntryModelTest extends TestCase
1013
{
14+
use RefreshDatabase;
15+
1116
#[Test]
1217
public function it_gets_attributes_from_json_column()
1318
{
@@ -22,4 +27,88 @@ public function it_gets_attributes_from_json_column()
2227
$this->assertEquals('bar', $model->foo);
2328
$this->assertEquals(['foo' => 'bar'], $model->data);
2429
}
30+
31+
#[Test]
32+
public function it_gets_date_as_utc()
33+
{
34+
config()->set('app.timezone', 'America/New_York'); // -05:00
35+
date_default_timezone_set('America/New_York');
36+
37+
DB::table('entries')->insert([
38+
'id' => '1',
39+
'site' => 'en',
40+
'published' => 1,
41+
'date' => '2025-01-01 12:11:10',
42+
'collection' => 'blog',
43+
'data' => '{}',
44+
]);
45+
46+
$date = EntryModel::find(1)->date;
47+
48+
$this->assertInstanceOf(Carbon::class, $date);
49+
$this->assertEquals('2025-01-01T12:11:10+00:00', $date->toIso8601String());
50+
}
51+
52+
#[Test]
53+
public function it_sets_utc_date_from_string()
54+
{
55+
config()->set('app.timezone', 'America/New_York'); // -05:00
56+
date_default_timezone_set('America/New_York');
57+
58+
$model = new EntryModel();
59+
$model->id = 1;
60+
$model->site = 'en';
61+
$model->published = true;
62+
$model->collection = 'blog';
63+
$model->date = '2025-01-01 12:11:10';
64+
$model->data = [];
65+
$model->save();
66+
67+
$this->assertDatabaseHas('entries', [
68+
'id' => 1,
69+
'date' => '2025-01-01 12:11:10',
70+
]);
71+
}
72+
73+
#[Test]
74+
public function it_sets_utc_date_from_carbon()
75+
{
76+
config()->set('app.timezone', 'America/New_York'); // -05:00
77+
date_default_timezone_set('America/New_York');
78+
79+
$model = new EntryModel();
80+
$model->id = 1;
81+
$model->site = 'en';
82+
$model->published = true;
83+
$model->collection = 'blog';
84+
$model->date = Carbon::parse('2025-01-01 12:11:10', 'UTC');
85+
$model->data = [];
86+
$model->save();
87+
88+
$this->assertDatabaseHas('entries', [
89+
'id' => 1,
90+
'date' => '2025-01-01 12:11:10',
91+
]);
92+
}
93+
94+
#[Test]
95+
public function it_sets_non_utc_date_from_carbon()
96+
{
97+
config()->set('app.timezone', 'America/New_York'); // -05:00
98+
date_default_timezone_set('America/New_York');
99+
100+
$model = new EntryModel();
101+
$model->id = 1;
102+
$model->site = 'en';
103+
$model->published = true;
104+
$model->collection = 'blog';
105+
$model->date = Carbon::parse('2025-01-01 12:11:10');
106+
$model->data = [];
107+
$model->save();
108+
109+
$this->assertDatabaseHas('entries', [
110+
'id' => 1,
111+
'date' => '2025-01-01 17:11:10',
112+
]);
113+
}
25114
}

0 commit comments

Comments
 (0)