modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-10-15 17:09:19 +04:00
parent cf5ef38a0f
commit 7d332552ab
6 changed files with 48 additions and 11 deletions

View File

@ -91,7 +91,10 @@ class Delay {
$from = MutableDateTime::with($from)->clone(true); $from = MutableDateTime::with($from)->clone(true);
if ($delay === "INF") { if ($delay === "INF") {
$dest = $from; $dest = $from;
$dest->add(new DateInterval("P9999Y")); # rajouter 1000 ans pour ne pas dépasser la capacité
#XXX avant, c'était 9999 ans, mais getDest() provoque une exception parce
# que DateTime ne sait pas traiter une valeur flottante
$dest->add(new DateInterval("P1000Y"));
$repr = "INF"; $repr = "INF";
} elseif (is_int($delay)) { } elseif (is_int($delay)) {
[$dest, $repr] = self::compute_dest($delay, "s", null, $from); [$dest, $repr] = self::compute_dest($delay, "s", null, $from);
@ -119,10 +122,11 @@ class Delay {
} }
function __serialize(): array { function __serialize(): array {
return [$this->dest, $this->repr]; return [$this->dest->clone(), $this->repr];
} }
function __unserialize(array $data): void { function __unserialize(array $data): void {
[$this->dest, $this->repr] = $data; [$dest, $this->repr] = $data;
$this->dest = $dest->clone(true);
} }
/** @var MutableDateTime */ /** @var MutableDateTime */
@ -132,20 +136,22 @@ class Delay {
return $this->dest->clone(); return $this->dest->clone();
} }
function addDuration($duration) { function addDuration($duration): self {
if (is_numeric($duration) && $duration < 0) { if (is_numeric($duration) && $duration < 0) {
$this->dest->sub(DateInterval::with(-$duration)); $this->dest->sub(DateInterval::with(-$duration));
} else { } else {
$this->dest->add(DateInterval::with($duration)); $this->dest->add(DateInterval::with($duration));
} }
return $this;
} }
function subDuration($duration) { function subDuration($duration): self {
if (is_numeric($duration) && $duration < 0) { if (is_numeric($duration) && $duration < 0) {
$this->dest->add(DateInterval::with(-$duration)); $this->dest->add(DateInterval::with(-$duration));
} else { } else {
$this->dest->sub(DateInterval::with($duration)); $this->dest->sub(DateInterval::with($duration));
} }
return $this;
} }
/** @var string */ /** @var string */

View File

@ -1,10 +1,9 @@
# nulib\php\time # nulib\php\time
* Date, DateTime sont immutables par défaut. par exemple, add() retourne un nouvel objet. * refaire l'implémentation pour les délais INF. c'est un cas particulier qui
ajouter une version des méthodes qui modifie les données en place en les n'est jamais atteint. il faut donc implémenter un traitement spécifique dans
préfixant de `_` e.g `_add()` chaque méthode (i.e `if ($repr === "INF") { doSomething() }`)
* la destination est toujours 1000 ans dans le futur
en terme d'implémentation, dériver \DateTime pour supporter les modification * la différence est toujours de 1000 ans
en place, bien que ce ne soit pas le fonctionnement par défaut
-*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary

7
php/tbin/cachectl.php Executable file
View File

@ -0,0 +1,7 @@
#!/usr/bin/php
<?php
require __DIR__.'/../vendor/autoload.php';
use cli\CachectlApp;
CachectlApp::run();

View File

@ -7,6 +7,15 @@ use nulib\cache\CacheFile;
use nulib\ext\yaml; use nulib\ext\yaml;
use nulib\os\sh; use nulib\os\sh;
function Txx(...$parts) {
$line = [];
foreach ($parts as $part) {
if (is_scalar($part)) $line[] = strval($part);
else $line[] = var_export($part, true);
}
echo implode("", $line)."\n";
}
function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void { function show(string $prefix, CacheFile $cache, bool $dumpInfos=true): void {
Txx("$prefix=", $cache->get()); Txx("$prefix=", $cache->get());
if ($dumpInfos) { if ($dumpInfos) {

View File

@ -118,4 +118,12 @@ class DateTimeTest extends TestCase {
self::assertFalse($b >= $b2); self::assertFalse($b >= $b2);
self::assertFalse($b >= $b3); self::assertFalse($b >= $b3);
} }
function testSerialize() {
$date = new DateTime();
$serialized = serialize($date);
echo "serialized: $serialized\n";
$unserialized = unserialize($serialized);
self::assertEquals($date, $unserialized);
}
} }

View File

@ -76,4 +76,12 @@ class DelayTest extends TestCase {
sleep(5); sleep(5);
self::assertTrue($delay->isElapsed()); self::assertTrue($delay->isElapsed());
} }
function testSerialize() {
$delay = new Delay(5);
$serialized = serialize($delay);
echo "serialized: $serialized\n";
$unserialized = unserialize($serialized);
self::assertEquals($delay, $unserialized);
}
} }