modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-17 21:38:40 +04:00
parent 60c9c4a2fb
commit 218c097cc4
1 changed files with 171 additions and 0 deletions

171
src/php/time/Elapsed.php Normal file
View File

@ -0,0 +1,171 @@
<?php
namespace nur\sery\php\time;
/**
* Class Elapsed: durée entre deux événements
*/
class Elapsed {
const MINUTE = 60;
const HOUR = 60 * 60;
const DAY = 60 * 60 * 24;
/** @var int résolution */
const RES_SECONDS = 0, RES_MINUTES = 1, RES_HOURS = 2, RES_DAYS = 3;
private static function format(int $seconds, int $resolution, ?string $prefix=null, ?string $zero=null): string {
if ($prefix === null) $prefix = "depuis";
switch ($resolution) {
case self::RES_DAYS: return self::format_days($seconds, $prefix, $zero);
case self::RES_HOURS: return self::format_hours($seconds, $prefix, $zero);
case self::RES_MINUTES: return self::format_minutes($seconds, $prefix, $zero);
default: return self::format_seconds($seconds, $prefix, $zero);
}
}
private static function format_generic(string $prefix, int $d, int $h, int $m): string {
$text = $prefix;
if ($d > 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");
}
}