<?php
/**
 * A class of utilities for dealing with dates and times.
 *
 * @since 3.0.0
 */

declare(strict_types=1);
namespace FeData\FastEvents\Utilities;

use DateTime;
use DateTimeZone;

/**
 * A class of utilities for dealing with dates and times.
 */
class TimeUtil {

	/**
	 * Get the local time (including timezone offset)
	 *
	 * @since 1.0
	 *
	 * @return int Timestamp.
	 */
	public static function local_time(): int {
		$datetime = date_create( 'now', wp_timezone() );
		return $datetime->getTimestamp() + $datetime->getOffset();
	}


	/**
	 * Get the UTC time
	 *
	 * @since 1.0
	 *
	 * @return int Timestamp.
	 */
	public static function utc_time(): int {
		$datetime = date_create( 'now', new DateTimeZone( 'UTC' ) );
		return $datetime->getTimestamp();
	}


	/**
	 * Get the local time based on a date-string (including timezone offset)
	 *
	 * @since 1.0
	 *
	 * @param string $date Date string.
	 * @return int Timestamp.
	 */
	public static function date_local_time( string $date ): int {
		$datetime = date_create( $date, wp_timezone() );
		return $datetime->getTimestamp() + $datetime->getOffset();
	}


	/**
	 * Validate date/time formats.
	 *
	 * @since 2.2.0
	 *
	 * @param string $date   Date and/or time string.
	 * @param string $format Date and/or time format string.
	 *
	 * @return bool Valid date.
	 */
	public static function validate_date( string $date, string $format = 'Y-m-d H:i:s' ): bool {
		$d = DateTime::createFromFormat( $format, $date );
		return $d && $d->format( $format ) === $date;
	}
}
