diff --git a/modules/boersenchecker/bootstrap.php b/modules/boersenchecker/bootstrap.php index ac6b419..1a4a607 100644 --- a/modules/boersenchecker/bootstrap.php +++ b/modules/boersenchecker/bootstrap.php @@ -399,6 +399,69 @@ $mm->registerFunction($moduleName, 'bavest_request', static function ( ]; }); +$mm->registerFunction($moduleName, 'display_timezone', static function (): \DateTimeZone { + return new \DateTimeZone('Europe/Berlin'); +}); + +$mm->registerFunction($moduleName, 'normalize_bavest_timestamp_utc', static function (mixed $value): string { + if (is_numeric($value)) { + return gmdate('Y-m-d H:i:s', (int) $value); + } + + $raw = trim((string) $value); + if ($raw === '') { + return gmdate('Y-m-d H:i:s'); + } + + try { + $date = new \DateTimeImmutable($raw, new \DateTimeZone('UTC')); + return $date->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s'); + } catch (\Throwable) { + $timestamp = strtotime($raw); + return $timestamp !== false ? gmdate('Y-m-d H:i:s', $timestamp) : gmdate('Y-m-d H:i:s'); + } +}); + +$mm->registerFunction($moduleName, 'format_datetime_for_display', static function ( + ?string $value, + ?string $source = null, + string $format = 'Y-m-d H:i:s' +): string { + $raw = trim((string) $value); + if ($raw === '') { + return ''; + } + + $displayTimezone = new \DateTimeZone('Europe/Berlin'); + $source = trim((string) $source); + + if (str_starts_with($source, 'bavest:')) { + $date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $raw, new \DateTimeZone('UTC')); + if (!$date instanceof \DateTimeImmutable) { + try { + $date = new \DateTimeImmutable($raw, new \DateTimeZone('UTC')); + } catch (\Throwable) { + return $raw; + } + } + return $date->setTimezone($displayTimezone)->format($format); + } + + if (preg_match('/(Z|[+\-]\d{2}:\d{2})$/', $raw) === 1) { + try { + return (new \DateTimeImmutable($raw))->setTimezone($displayTimezone)->format($format); + } catch (\Throwable) { + return $raw; + } + } + + return str_replace('T', ' ', $raw); +}); + +$mm->registerFunction($moduleName, 'local_now_input_value', static function (): string { + return (new \DateTimeImmutable('now', new \DateTimeZone('Europe/Berlin')))->format('Y-m-d\TH:i'); +}); + $mm->registerFunction($moduleName, 'bavest_extract_quote', static function (array $entry): ?array { $candidates = [$entry]; foreach (['quote', 'data', 'result', 'security'] as $nestedKey) { @@ -419,8 +482,11 @@ $mm->registerFunction($moduleName, 'bavest_extract_quote', static function (arra continue; } - $timestamp = trim((string) ($candidate['timestamp'] ?? $candidate['time'] ?? $candidate['date'] ?? '')); - $timestamp = $timestamp !== '' ? date('Y-m-d H:i:s', strtotime($timestamp) ?: time()) : date('Y-m-d H:i:s'); + $timestamp = module_fn( + 'boersenchecker', + 'normalize_bavest_timestamp_utc', + $candidate['timestamp'] ?? $candidate['time'] ?? $candidate['date'] ?? null + ); return [ 'symbol' => trim((string) ($candidate['symbol'] ?? $candidate['ticker'] ?? $entry['symbol'] ?? '')), diff --git a/modules/boersenchecker/partials/dashboard.php b/modules/boersenchecker/partials/dashboard.php index 73bdd18..f19882e 100644 --- a/modules/boersenchecker/partials/dashboard.php +++ b/modules/boersenchecker/partials/dashboard.php @@ -287,7 +287,7 @@