Skip to content

Commit

Permalink
fix(DateTime): use php81_bc/strftime (#1063)
Browse files Browse the repository at this point in the history
Native strftime was deprecated in PHP 8.1 so I replaced it with an
alternative library that uses ICU so that you don't have to set up
locales on your server anymore.
Also this commit removes AM/PM labels as timestamps were in 24h format
anyways, regardless of locale.
  • Loading branch information
WerySkok authored Jan 2, 2024
1 parent fabf9ff commit 44531fc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Web/Models/Entities/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function getSendTimeHumanized(): string
$dateTime = new DateTime($this->getRecord()->created);

if($dateTime->format("%d.%m.%y") == ovk_strftime_safe("%d.%m.%y", time())) {
return $dateTime->format("%T %p");
return $dateTime->format("%T");
} else {
return $dateTime->format("%d.%m.%y");
}
Expand Down
10 changes: 5 additions & 5 deletions Web/Util/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ protected function zmdate(): string
$now = date_create();
$diff = date_diff($now, $then);
if($diff->invert === 0)
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R %p", $this->timestamp);
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);

if($this->timestamp >= strtotime("midnight")) { # Today
if($diff->h >= 1)
return tr("time_today") . tr("time_at_sp") . ovk_strftime_safe(" %R %p", $this->timestamp);
return tr("time_today") . tr("time_at_sp") . ovk_strftime_safe(" %R", $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") . ovk_strftime_safe(" %R %p", $this->timestamp);
return tr("time_yesterday") . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
} else if(ovk_strftime_safe("%Y", $this->timestamp) === ovk_strftime_safe("%Y", time())) { # In this year
return ovk_strftime_safe("%e %h ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R %p", $this->timestamp);
return ovk_strftime_safe("%e %h ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
} else {
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R %p", $this->timestamp);
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
}
}

Expand Down
3 changes: 2 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Chandler\Session\Session;
use openvk\Web\Util\Localizator;
use openvk\Web\Util\Bitmask;
use function PHP81_BC\strftime;

function _ovk_check_environment(): void
{
Expand Down Expand Up @@ -198,7 +199,7 @@ function ovk_proc_strtrim(string $string, int $length = 0): string
function ovk_strftime_safe(string $format, ?int $timestamp = NULL): string
{
$sessionOffset = intval(Session::i()->get("_timezoneOffset"));
$str = strftime($format, $timestamp + ($sessionOffset * MINUTE) * -1 ?? time() + ($sessionOffset * MINUTE) * -1);
$str = strftime($format, $timestamp + ($sessionOffset * MINUTE) * -1 ?? time() + ($sessionOffset * MINUTE) * -1, tr("__locale") !== '@__locale' ? tr("__locale") : NULL);
if(PHP_SHLIB_SUFFIX === "dll") {
$enc = tr("__WinEncoding");
if($enc === "@__WinEncoding")
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"ext-simplexml": "*",
"symfony/console": "5.4.x-dev",
"wapmorgan/morphos": "dev-master",
"ext-sodium": "*"
"ext-sodium": "*",
"php81_bc/strftime": "^0.0.2"
},
"minimum-stability": "dev"
}
42 changes: 41 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 44531fc

Please sign in to comment.