Make DateTime compatible with Windows

This commit is contained in:
Alma Armas 2020-09-29 12:59:59 -07:00
parent 7be8b25a21
commit b2f069f75f
3 changed files with 23 additions and 6 deletions

View file

@ -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);
}

View file

@ -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";

View file

@ -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 %}