Uso de zona horaria en output de comandos

This commit is contained in:
Juan Pablo Vial
2025-05-16 14:44:48 -04:00
parent 8ce7d2570d
commit 2bdb2a0ed0
4 changed files with 17 additions and 9 deletions

View File

@ -1,6 +1,7 @@
<?php
namespace Incoviba\Command;
use DateTimeZone;
use Throwable;
use DateTimeInterface;
use DateTimeImmutable;
@ -17,7 +18,9 @@ class BaseLoop extends Console\Command\Command
public function __construct(protected LoggerInterface $logger, protected Schedule $scheduleService, ?string $name = null)
{
parent::__construct($name);
$this->timezone = new DateTimeZone($_ENV['TZ'] ?? 'America/Santiago');
}
protected DateTimeZone $timezone;
public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
{
@ -28,7 +31,7 @@ class BaseLoop extends Console\Command\Command
$this->write($output, 'Starting loop...');
while (true) {
$commands = $this->scheduleService->getPending();
$start = new DateTimeImmutable();
$start = new DateTimeImmutable('now', $this->timezone);
foreach ($commands as $command) {
$this->runCommand($input, $output, $command);
}
@ -40,9 +43,9 @@ class BaseLoop extends Console\Command\Command
{
// wait for next minute
if ($start === null) {
$start = new DateTimeImmutable();
$start = new DateTimeImmutable('now', $this->timezone);
}
$nextMinute = new DateTimeImmutable($start->format('Y-m-d H:i:00'));
$nextMinute = new DateTimeImmutable($start->format('Y-m-d H:i:00'), $this->timezone);
$nextMinute = $nextMinute->add(new \DateInterval('PT1M'));
$diff = $nextMinute->getTimestamp() - $start->getTimestamp();
if ($diff > 0) {
@ -71,7 +74,7 @@ class BaseLoop extends Console\Command\Command
}
protected function write(Console\Output\OutputInterface $output, string $message): void
{
$now = new DateTimeImmutable();
$now = new DateTimeImmutable('now', $this->timezone);
$output->writeln("[{$now->format('Y-m-d H:i:s e')}] {$message}");
}
}