Files
firefly-iii/app/Console/Commands/Integrity/ConvertDatesToUTC.php

110 lines
3.6 KiB
PHP
Raw Normal View History

2024-11-09 12:19:01 +01:00
<?php
declare(strict_types=1);
2024-11-09 12:19:01 +01:00
/*
* ConvertDatesToUTC.php
* Copyright (c) 2024 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
namespace FireflyIII\Console\Commands\Integrity;
use Carbon\Carbon;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2024-11-22 06:03:29 +01:00
use FireflyIII\Support\Facades\FireflyConfig;
2024-11-09 12:19:01 +01:00
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
class ConvertDatesToUTC extends Command
{
use ShowsFriendlyMessages;
2024-11-09 12:19:01 +01:00
/**
* The name and signature of the console command.
*
* @var string
*/
2024-11-22 06:03:29 +01:00
protected $signature = 'firefly-iii:migrate-to-utc';
2024-11-09 12:19:01 +01:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Convert stored dates to UTC.';
/**
* Execute the console command.
*/
public function handle(): int
{
/**
* @var string $model
* @var array $fields
*/
foreach (AddTimezonesToDates::$models as $model => $fields) {
$this->ConvertModeltoUTC($model, $fields);
}
2024-11-22 06:03:29 +01:00
// tell the system we are now in UTC mode.
FireflyConfig::set('utc', true);
2024-11-09 12:19:01 +01:00
return Command::SUCCESS;
}
private function ConvertModeltoUTC(string $model, array $fields): void
{
/** @var string $field */
foreach ($fields as $field) {
$this->convertFieldtoUTC($model, $field);
}
}
private function convertFieldtoUTC(string $model, string $field): void
{
2024-11-09 12:19:01 +01:00
$this->info(sprintf('Converting %s.%s to UTC', $model, $field));
$shortModel = str_replace('FireflyIII\Models\\', '', $model);
$timezoneField = sprintf('%s_tz', $field);
$items = new Collection();
$timeZone = config('app.timezone');
2024-11-09 12:19:01 +01:00
try {
$items = $model::where($timezoneField, $timeZone)->get();
} catch (QueryException $e) {
$this->friendlyError(sprintf('Cannot find timezone information to field "%s" of model "%s". Field does not exist.', $field, $shortModel));
Log::error($e->getMessage());
}
if (0 === $items->count()) {
$this->friendlyPositive(sprintf('All timezone information is UTC in field "%s" of model "%s".', $field, $shortModel));
return;
}
$this->friendlyInfo(sprintf('Converting field "%s" of model "%s" to UTC.', $field, $shortModel));
$items->each(
function ($item) use ($field, $timezoneField): void {
2024-11-09 12:19:01 +01:00
/** @var Carbon $date */
$date = Carbon::parse($item->{$field}, $item->{$timezoneField});
2024-11-09 12:19:01 +01:00
$date->setTimezone('UTC');
$item->{$field} = $date->format('Y-m-d H:i:s');
$item->{$timezoneField} = 'UTC';
2024-11-09 12:19:01 +01:00
$item->save();
}
);
}
}