<?php
/*
 * Software Name: Eorisis Pixels
 * Software URI: https://eorisis.com/shop/software/joomla/pixels
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace com_eorisis_pixels\framework\install_package\lib\core\model;
\defined('_JEXEC') or die;

use DateTime, DateTimeZone, IntlDateFormatter;

class geotime
{
	public function __construct() {}

	// --------------------------------------------------

	public static function date($time, DateTimeZone $datetimezone)
	{
		// $time can be 'now', 'today', etc, or a date
		return new DateTime($time, $datetimezone);
	}

	// --------------------------------------------------

	public static function date_by_timezone($time = 'now', string $timezone = '')
	{
		// $time can be 'now', 'today', etc, or a date
		// $timezone can be UTC or a timezone like Europe/Athens
		$datetimezone = self::get_timezone($timezone);
		return self::date($time, $datetimezone);
	}

	// --------------------------------------------------

	public static function get_timezone(string $string = '')
	{
		if (!$string)
		{
			$string = \com_eorisis_pixels\framework\install_package\lib\core\cms\config::offset();
		}

		return new DateTimeZone($string);
	}

	// --------------------------------------------------

	public static function to_local_date($time = 'now', string $local_format = 'd MMMM y', string $format = 'F j, Y', string $timezone = '') : string
	{
		// $time can be 'now' or a date

		// Some systems are not well prepared by the host
		if (\class_exists('IntlDateFormatter', false))
		{
			$locale_code = \com_eorisis_pixels\framework\install_package\lib\core\cms\language::current_sef();
			if ($locale_code)
			{
				if ($locale_code != 'en')
				{
					$date = self::date_by_timezone($time, $timezone);
					return self::translate_date($date, $locale_code, $local_format);
				}
			}
		}

		return self::date_by_timezone($time, $timezone)->format($format);
	}

	// --------------------------------------------------

	public static function translate_date($date, string $locale_code, string $format = 'd MMMM y')
	{
		// $locale_code e.g: en,el
		return IntlDateFormatter::formatObject($date, $format, $locale_code);
	}
}
