CoreFiles by mySites.guru
DateTimeHelper.php59 lines, 1,661 bytes, from Joomla_5.2.1-Stable-Full_Package.zip
Raw History Compare with 5.2.0
md5
cb6e7199946e9a126ffe16a8ccbd8ca3
sha1
6449f4c33928fc6b6cdde614fca55646f636864c
sha256
39ec14cf49020ed3fe84ffb37736ea842e2f4ff496794e53432cff4833777e66
<?php

declare(strict_types=1);

namespace SpomkyLabs\Pki\X509\Feature;

use DateTimeImmutable;
use DateTimeZone;
use Exception;
use RuntimeException;
use UnexpectedValueException;

/**
 * Helper trait for classes employing date and time handling.
 */
trait DateTimeHelper
{
    /**
     * Create DateTime object from time string and timezone.
     *
     * @param null|string $time Time string, default to 'now'
     * @param null|string $tz Timezone, default if omitted
     */
    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);
        }
    }

    /**
     * Rounds a \DateTimeImmutable value such that fractional seconds are removed.
     */
    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());
    }

    /**
     * Create DateTimeZone object from string.
     */
    private static function createTimeZone(string $tz): DateTimeZone
    {
        try {
            return new DateTimeZone($tz);
        } catch (Exception $e) {
            throw new UnexpectedValueException('Invalid timezone.', 0, $e);
        }
    }
}

The same content is in 158 releases, Joomla 5.0.0-alpha1 to 6.3.0-alpha1. See them all, or every version of this file.