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}");
}
}

View File

@ -3,6 +3,7 @@ namespace Incoviba\Command\Money;
use DateTimeImmutable;
use DateInterval;
use DateTimeZone;
use Symfony\Component\Console;
use Incoviba\Common\Alias\Command;
@ -15,10 +16,12 @@ class IPC extends Command
public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
{
$this->logger->debug("Running {$this->getName()}");
$lastMonth = (new DateTimeImmutable())->sub(new DateInterval('P1M'));
$endLastYear = (new DateTimeImmutable())->sub(new DateInterval('P1Y'));
$timezone = new DateTimeZone($_ENV['TZ'] ?? 'America/Santiago');
$now = new DateTimeImmutable('now', $timezone);
$lastMonth = $now->sub(new DateInterval('P1M'));
$endLastYear = $now->sub(new DateInterval('P1Y'));
$uri = '/api/money/ipc';
$current = new DateTimeImmutable($lastMonth->format('Y-m-d'));
$current = new DateTimeImmutable($lastMonth->format('Y-m-d'), $timezone);
while ($current->diff($endLastYear)->days > 30) {
$data = [
'start' => $endLastYear->format('Y-12-1'),

View File

@ -2,6 +2,7 @@
namespace Incoviba\Command\Money;
use DateTimeImmutable;
use DateTimeZone;
use Symfony\Component\Console;
use Incoviba\Common\Alias\Command;
@ -14,7 +15,7 @@ class UF extends Command
public function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
{
$this->logger->debug("Running {$this->getName()}");
$now = new DateTimeImmutable();
$now = new DateTimeImmutable('now', new DateTimeZone($_ENV['TZ'] ?? 'America/Santiago'));
$uri = '/api/money/uf';
$data = [
'fecha' => $now->format('Y-m-d')

View File

@ -2,6 +2,7 @@
namespace Incoviba\Command;
use DateTimeImmutable;
use DateTimeZone;
use Psr\Http\Client\ClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console;
@ -24,7 +25,7 @@ class Queue extends Command
{
$this->logger->debug("Running {$this->getName()}");
$io = new Console\Style\SymfonyStyle($input, $output);
$now = new DateTimeImmutable();
$now = new DateTimeImmutable('now', new DateTimeZone($_ENV['TZ'] ?? 'America/Santiago'));
$io->title("[{$now->format('Y-m-d H:i:s e')}] Running Queue...");
$jobs = $this->getJobs($output);