Skip to content

Commit

Permalink
Merge pull request #775 from erikn69/patch-19
Browse files Browse the repository at this point in the history
fix: datetime values are shifted by the timezone modifier
  • Loading branch information
MortenDHansen authored Mar 13, 2023
2 parents b200968 + 6c6f2f0 commit 5f86086
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use InvalidArgumentException;
use OwenIt\Auditing\Contracts\AttributeEncoder;

trait Audit
Expand Down Expand Up @@ -138,17 +141,42 @@ protected function getFormattedValue(Model $model, string $key, $value)

// Cast to native PHP type
if ($model->hasCast($key)) {
if ($model->getCastType($key) == 'datetime' ) {
$value = $this->castDatetimeUTC($model, $value);
}

return $model->castAttribute($key, $value);
}

// Honour DateTime attribute
if ($value !== null && in_array($key, $model->getDates(), true)) {
return $model->asDateTime($value);
return $model->asDateTime($this->castDatetimeUTC($model, $value));
}

return $value;
}

private function castDatetimeUTC($model, $value)
{
if (!is_string($value)) {
return $value;
}

if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) {
return Date::instance(Carbon::createFromFormat('Y-m-d', $value, Date::now('UTC')->getTimezone())->startOfDay());
}

if (preg_match('/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/', $value)) {
return Date::instance(Carbon::createFromFormat('Y-m-d H:i:s', $value, Date::now('UTC')->getTimezone()));
}

try {
return Date::createFromFormat($model->getDateFormat(), $value, Date::now('UTC')->getTimezone());
} catch (InvalidArgumentException $e) {
return $value;
}
}

/**
* {@inheritdoc}
*/
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/AuditableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,39 @@ public function itFailsToTransitionWhenTheAuditAuditableTypeDoesNotMatchTheModel
$model->transitionTo($audit);
}

/**
* @group Auditable::transitionTo
* @test
*/
public function itWorksOnTimesRestoredCorrectly()
{
config(['app.timezone' => 'America/New_York']);
date_default_timezone_set('America/New_York');

$originalStart = new Carbon('2022-01-01 12:00:00');

$article = factory(Article::class)->create([
'title' => 'How To Audit Eloquent Models',
'content' => 'First step: install the laravel-auditing package.',
'reviewed' => 1,
'published_at' => $originalStart,
]);

$model = Article::first();

$this->assertEquals($model->published_at, $originalStart);

$model->published_at = new Carbon('2022-01-01 12:30:00');
$model->save();

$audit = $model->audits->last();
$audit->auditable_id = $model->id;

$model->transitionTo($audit, true);

$this->assertEquals($model->published_at, $originalStart);
}

/**
* @group Auditable::transitionTo
* @test
Expand Down

0 comments on commit 5f86086

Please sign in to comment.