<?php

/**
 * Smarty {format_date_locale} modifier.
 *
 * @param      $date   DateTime|string Date to format
 * @param null $format Format to use - in ICU style http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details
 * @param null $locale string Locale to use. Ef empty, use current locale
 *
 * @return string
 */
function smarty_modifier_format_date_locale($date, $format = null, $locale = null)
{
    if (!$date) {
        return '';
    }

    if (empty($locale)) {
        global $cfg;
        $locale = $cfg['Lang']['language'];
    }

    if ($locale == 'czech') {
        $locale = 'cs';
    }

    if (!($date instanceof DateTime)) {
        $new_date = DateTime::createFromFormat('Y-m-d H:i:s', $date);
        if (!$new_date) {
            $php_format = Settings::getDateFormat().' '.Settings::getTimeFormat();
            $new_date = DateTime::createFromFormat($php_format, $date);
        }
        $date = $new_date;
    }

    return IntlDateFormatter::formatObject($date, $format, $locale);
}
