From 218c097cc428e04514f294f658e61dab5ac16f21 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Mon, 17 Jun 2024 21:38:40 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/php/time/Elapsed.php | 171 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 src/php/time/Elapsed.php diff --git a/src/php/time/Elapsed.php b/src/php/time/Elapsed.php new file mode 100644 index 0000000..9d7ed6f --- /dev/null +++ b/src/php/time/Elapsed.php @@ -0,0 +1,171 @@ + 0) { + if ($text !== "") $text .= " "; + $text .= sprintf("%d jour", $d); + if ($d > 1) $text .= "s"; + } + if ($h > 0) { + if ($text !== "") $text .= " "; + $text .= sprintf("%d heure", $h); + if ($h > 1) $text .= "s"; + } + if ($m > 0) { + if ($text !== "") $text .= " "; + $text .= sprintf("%d minute", $m); + if ($m > 1) $text .= "s"; + } + return $text; + } + + private static function format_seconds(int $seconds, string $prefix, ?string $zero): string { + $seconds = abs($seconds); + + if ($zero === null) $zero = "maintenant"; + if ($seconds == 0) return $zero; + + if ($seconds <= 3) { + if ($prefix !== "") $prefix .= " "; + return "${prefix}quelques secondes"; + } + + if ($seconds < 60) { + if ($prefix !== "") $prefix .= " "; + return sprintf("${prefix}%d secondes", $seconds); + } + + $oneDay = 60 * 60 * 24; + $oneHour = 60 * 60; + $oneMinute = 60; + $rs = $seconds; + $d = intdiv($rs, $oneDay); $rs = $rs % $oneDay; + $h = intdiv($rs, $oneHour); $rs = $rs % $oneHour; + $m = intdiv($rs, $oneMinute); + return self::format_generic($prefix, $d, $h, $m); + } + + private static function format_minutes(int $seconds, string $prefix, ?string $zero): string { + $minutes = intdiv(abs($seconds), 60); + + if ($zero === null) $zero = "maintenant"; + if ($minutes == 0) return $zero; + + if ($minutes <= 3) { + if ($prefix !== "") $prefix .= " "; + return "${prefix}quelques minutes"; + } + + $oneDay = 60 * 24; + $oneHour = 60; + $rs = $minutes; + $d = intdiv($rs, $oneDay); $rs = $rs % $oneDay; + $h = intdiv($rs, $oneHour); $rs = $rs % $oneHour; + $m = $rs; + return self::format_generic($prefix, $d, $h, $m); + } + + private static function format_hours(int $seconds, string $prefix, ?string $zero): string { + $minutes = intdiv(abs($seconds), 60); + + if ($zero === null) $zero = "maintenant"; + if ($minutes == 0) return $zero; + + if ($minutes < 60) { + if ($prefix !== "") $prefix .= " "; + return "${prefix}moins d'une heure"; + } elseif ($minutes < 120) { + if ($prefix !== "") $prefix .= " "; + return "${prefix}moins de deux heures"; + } + + $oneDay = 60 * 24; + $oneHour = 60; + $rs = $minutes; + $d = intdiv($rs, $oneDay); $rs = $rs % $oneDay; + $h = intdiv($rs, $oneHour); + return self::format_generic($prefix, $d, $h, 0); + } + + private static function format_days(int $seconds, string $prefix, ?string $zero): string { + $hours = intdiv(abs($seconds), 60 * 60); + + if ($zero === null) $zero = "aujourd'hui"; + if ($hours < 24) return $zero; + + $oneDay = 24; + $rs = $hours; + $d = intdiv($rs, $oneDay); + return self::format_generic($prefix, $d, 0, 0); + } + + static function format_at(DateTime $start, ?DateTime $now=null): string { + $now ??= new DateTime(); + $seconds = $now->getTimestamp() - $start->getTimestamp(); + return (new self($seconds))->formatAt(); + } + + static function format_since(DateTime $start, ?DateTime $now=null): string { + $now ??= new DateTime(); + $seconds = $now->getTimestamp() - $start->getTimestamp(); + return (new self($seconds))->formatSince(); + } + + static function format_delay(DateTime $start, ?DateTime $now=null): string { + $now ??= new DateTime(); + $seconds = $now->getTimestamp() - $start->getTimestamp(); + return (new self($seconds))->formatDelay(); + } + + function __construct(int $seconds, int $resolution=self::RES_SECONDS) { + if ($resolution < self::RES_SECONDS) $resolution = self::RES_SECONDS; + elseif ($resolution > self::RES_DAYS) $resolution = self::RES_DAYS; + $this->seconds = $seconds; + $this->resolution = $resolution; + } + + /** @var int */ + private $seconds; + + /** @var int */ + private $resolution; + + function formatAt(): string { + $seconds = $this->seconds; + if ($seconds < 0) return self::format($seconds, $this->resolution, "dans"); + else return self::format($seconds, $this->resolution, "il y a"); + } + + function formatSince(): string { + $seconds = $this->seconds; + if ($seconds < 0) return self::format(-$seconds, $this->resolution, "dans"); + else return self::format($seconds, $this->resolution, "depuis"); + } + + function formatDelay(): string { + return self::format($this->seconds, $this->resolution, "", "immédiat"); + } +}