spaghettisolutions/xlsx-parser

View on GitHub
src/XLSXParser/Transformer/Date.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php declare(strict_types = 1);

namespace Spaghetti\XLSXParser\Transformer;

use DateTimeImmutable;

use function date_create_immutable_from_format;
use function floor;
use function gmdate;

/**
 * @internal
 */
final class Date
{
    private const DATETIME_FORMAT = 'd-m-Y H:i:s';

    public function transform(float|int $value): DateTimeImmutable
    {
        $value = (int) floor(num: $value);
        /** @noinspection SummerTimeUnsafeTimeManipulationInspection */
        $unix = ($value - 25569) * 86400;
        $date = gmdate(format: self::DATETIME_FORMAT, timestamp: $unix);

        return date_create_immutable_from_format(format: '!' . self::DATETIME_FORMAT, datetime: $date);
    }
}