<?php

namespace Fifa\ConnectId\Sdk\Util\DateUtils;

use DateTime;
use InvalidArgumentException;

/**
 * Class TimezoneChecker
 * @package Fifa\ConnectId\Sdk\Util\DateUtils
 */
class TimezoneChecker
{
    /**
     * Checks whether given DateTime object is UTC
     *
     * @param DateTime|null $date
     * @param null $message
     */
    public static function checkIsUTC(DateTime $date = null, $message = null): void
    {
        if (!$date instanceof DateTime) {
            throw new InvalidArgumentException($message ?: 'Date is not in UTC');
        }
        $dateParsed = @date_parse($date->format(DateTime::ATOM));
        if ($dateParsed === false) {
            throw new InvalidArgumentException('Date is not properly formatted.');
        }

        if (!isset($dateParsed['zone']) || $dateParsed['zone'] != 0) {
            throw new InvalidArgumentException(
                $message ?: sprintf('Date is not in UTC (timezone: %s)', $date->getTimezone()->getName())
            );
        }
    }
}