From b2f069f75fd1b9953db03c07dfb99fbe3a77824a Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Tue, 29 Sep 2020 12:59:59 -0700 Subject: [PATCH] Make DateTime compatible with Windows --- Web/Util/DateTime.php | 12 ++++++------ bootstrap.php | 16 ++++++++++++++++ locales/ru.strings | 1 + 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Web/Util/DateTime.php b/Web/Util/DateTime.php index a8c1e316..39d67d0c 100644 --- a/Web/Util/DateTime.php +++ b/Web/Util/DateTime.php @@ -25,24 +25,24 @@ class DateTime if($this->timestamp >= strtotime("midnight")) { # Today if($diff->h >= 1) - return tr("time_today") . tr("time_at_sp") . strftime("%X", $this->timestamp); + return tr("time_today") . tr("time_at_sp") . ovk_strftime_safe("%X", $this->timestamp); else if($diff->i < 2) return tr("time_just_now"); else return $diff->i === 5 ? tr("time_exactly_five_minutes_ago") : tr("time_minutes_ago", $diff->i); } else if($this->timestamp >= strtotime("-1day midnight")) { # Yesterday - return tr("time_yesterday") . tr("time_at_sp") . strftime("%X", $this->timestamp); - } else if(strftime("%G", $this->timestamp) === strftime("%G")) { # In this year - return strftime("%e %h ", $this->timestamp) . tr("time_at_sp") . strftime(" %R %p", $this->timestamp); + return tr("time_yesterday") . tr("time_at_sp") . ovk_strftime_safe("%X", $this->timestamp); + } else if(ovk_strftime_safe("%G", $this->timestamp) === ovk_strftime_safe("%G")) { # In this year + return ovk_strftime_safe("%e %h ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R %p", $this->timestamp); } else { - return strftime("%e %B %G ", $this->timestamp) . tr("time_at_sp") . strftime(" %X", $this->timestamp); + return ovk_strftime_safe("%e %B %G ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %X", $this->timestamp); } } function format(string $format, bool $useDate = false): string { if(!$useDate) - return strftime($format, $this->timestamp); + return ovk_strftime_safe($format, $this->timestamp); else return date($format, $this->timestamp); } diff --git a/bootstrap.php b/bootstrap.php index b5b4bbec..5b6e67c7 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -121,6 +121,22 @@ function ovk_proc_strtrim(string $string, int $length = 0): string return ovk_proc_strtr($string, $length); } +function ovk_strftime_safe(string $format, ?int $timestamp = NULL): string +{ + $str = strftime($format, $timestamp ?? time()); + if(PHP_SHLIB_SUFFIX === "dll") { + $enc = tr("__WinEncoding"); + if($enc === "@__WinEncoding") + $enc = "Windows-1251"; + + $nStr = iconv($enc, "UTF-8", $str); + if(!is_null($nStr)) + $str = $nStr; + } + + return $str; +} + return (function() { _ovk_check_environment(); require __DIR__ . "/vendor/autoload.php"; diff --git a/locales/ru.strings b/locales/ru.strings index 2d6ba276..4c63b0b3 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -1,4 +1,5 @@ "__locale" = "ru_UA.utf8;ru_RU.UTF-8;Rus"; +"__WinEncoding" = "Windows-1251"; %{ Check for https://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html %}