Skip to content

Commit

Permalink
fix: Time formatting on backup tasks view
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jul 16, 2024
1 parent d64bc5a commit f095bec
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/Models/BackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,18 @@ public function lastRunFormatted(?User $user = null): string
->isoFormat('D MMMM YYYY HH:mm');
}

/**
* Format the backup task's run time according to the user's timezone.
*/
public function runTimeFormatted(?User $user = null): string
{
$userTimezone = $user?->timezone ?? Auth::user()?->timezone ?? config('app.timezone');

$utcTime = Carbon::parse($this->time_to_run_at, 'UTC');

return $utcTime->timezone($userTimezone)->format('H:i');
}

/**
* Get the latest log for the backup task.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
@if ($backupTask->usingCustomCronExpression())
{{ $backupTask->custom_cron_expression }}
@else
{{ ucfirst(__($backupTask->frequency)) }} {{ __('at') }} {{ $backupTask->time_to_run_at }}
{{ ucfirst(__($backupTask->frequency)) }} {{ __('at') }} {{ $backupTask->runTimeFormatted(Auth::user()) }}
@endif
@endif
</p>
Expand Down Expand Up @@ -175,7 +175,7 @@
@if ($backupTask->usingCustomCronExpression())
{{ $backupTask->custom_cron_expression }}
@else
{{ ucfirst(__($backupTask->frequency)) }} {{ __('at') }} {{ $backupTask->time_to_run_at }}
{{ ucfirst(__($backupTask->frequency)) }} {{ __('at') }} {{ $backupTask->runTimeFormatted(Auth::user()) }}
@endif
@endif
</p>
Expand Down
77 changes: 77 additions & 0 deletions tests/Unit/Models/BackupTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,80 @@
afterEach(function (): void {
Carbon::setTestNow();
});

it('formats time correctly for UTC timezone', function (): void {
$user = User::factory()->create(['timezone' => 'UTC']);
$backupTask = BackupTask::factory()->create(['time_to_run_at' => '04:45']);

$result = $backupTask->runTimeFormatted();

expect($result)->toBe('04:45')
->and($user->timezone)->toBe('UTC');
});

it('correctly adjusts time for London timezone', function (): void {
$user = User::factory()->create(['timezone' => 'Europe/London']);
Auth::login($user);

$backupTask = BackupTask::factory()->create(['time_to_run_at' => '04:15']);

Config::set('app.timezone', 'UTC');

Carbon::setTestNow(Carbon::create(2024, 7, 16, 4, 15, 0, 'UTC'));

$result = $backupTask->runTimeFormatted();

expect($result)->toBe('05:15')
->and($user->timezone)->toBe('Europe/London');
});

it('handles non-DST period correctly', function (): void {
$user = User::factory()->create(['timezone' => 'America/New_York']);
Auth::login($user);
$backupTask = BackupTask::factory()->create(['time_to_run_at' => '04:45']);

Carbon::setTestNow(Carbon::create(2023, 1, 15, 4, 45, 0, 'UTC'));

$result = $backupTask->runTimeFormatted();

expect($result)->toBe('23:45')
->and($user->timezone)->toBe('America/New_York');
});

it('handles null user correctly', function (): void {
Auth::logout();
$backupTask = BackupTask::factory()->create(['time_to_run_at' => '04:45']);

Config::set('app.timezone', 'UTC');

$result = $backupTask->runTimeFormatted();

expect($result)->toBe('04:45');
});

it('handles time around midnight correctly', function (): void {
$user = User::factory()->create(['timezone' => 'America/Los_Angeles']);
Auth::login($user);
$backupTask = BackupTask::factory()->create(['time_to_run_at' => '23:45']);

Carbon::setTestNow(Carbon::create(2023, 7, 15, 23, 45, 0, 'UTC'));

$result = $backupTask->runTimeFormatted();

expect($result)->toBe('16:45')
->and($user->timezone)->toBe('America/Los_Angeles');
});

it('handles explicit user parameter', function (): void {
$user1 = User::factory()->create(['timezone' => 'America/New_York']);
$user2 = User::factory()->create(['timezone' => 'Europe/London']);
Auth::login($user1);
$backupTask = BackupTask::factory()->create(['time_to_run_at' => '12:00']);

Carbon::setTestNow(Carbon::create(2023, 7, 15, 12, 0, 0, 'UTC'));

$result = $backupTask->runTimeFormatted($user2);

expect($result)->toBe('13:00')
->and($user2->timezone)->toBe('Europe/London');
});

0 comments on commit f095bec

Please sign in to comment.