<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\X509\Feature; use DateTimeImmutable; use DateTimeZone; use Exception; use RuntimeException; use UnexpectedValueException; trait DateTimeHelper { private static function createDateTime(?string $time = null, ?string $tz = null): DateTimeImmutable { if (! isset($time)) { $time = 'now'; } if (! isset($tz)) { $tz = date_default_timezone_get(); } try { $dt = new DateTimeImmutable($time, self::createTimeZone($tz)); return self::roundDownFractionalSeconds($dt); } catch (Exception $e) { throw new RuntimeException('Failed to create DateTime:', 0, $e); } } private static function roundDownFractionalSeconds(DateTimeImmutable $dt): DateTimeImmutable { return DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $dt->format('Y-m-d H:i:s'), $dt->getTimezone()); } private static function createTimeZone(string $tz): DateTimeZone { try { return new DateTimeZone($tz); } catch (Exception $e) { throw new UnexpectedValueException('Invalid timezone.', 0, $e); } } } 