From 7d332552ab4fd318be81519920d870ce7dc767cc Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Wed, 15 Oct 2025 17:09:19 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- php/src/php/time/Delay.php | 16 +++++++++++----- php/src/php/time/TODO.md | 11 +++++------ php/tbin/cachectl.php | 7 +++++++ php/tbin/test-cache.php | 9 +++++++++ php/tests/php/time/DateTimeTest.php | 8 ++++++++ php/tests/php/time/DelayTest.php | 8 ++++++++ 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100755 php/tbin/cachectl.php diff --git a/php/src/php/time/Delay.php b/php/src/php/time/Delay.php index 1d11b72..f1008b2 100644 --- a/php/src/php/time/Delay.php +++ b/php/src/php/time/Delay.php @@ -91,7 +91,10 @@ class Delay { $from = MutableDateTime::with($from)->clone(true); if ($delay === "INF") { $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"; } elseif (is_int($delay)) { [$dest, $repr] = self::compute_dest($delay, "s", null, $from); @@ -119,10 +122,11 @@ class Delay { } function __serialize(): array { - return [$this->dest, $this->repr]; + return [$this->dest->clone(), $this->repr]; } function __unserialize(array $data): void { - [$this->dest, $this->repr] = $data; + [$dest, $this->repr] = $data; + $this->dest = $dest->clone(true); } /** @var MutableDateTime */ @@ -132,20 +136,22 @@ class Delay { return $this->dest->clone(); } - function addDuration($duration) { + function addDuration($duration): self { if (is_numeric($duration) && $duration < 0) { $this->dest->sub(DateInterval::with(-$duration)); } else { $this->dest->add(DateInterval::with($duration)); } + return $this; } - function subDuration($duration) { + function subDuration($duration): self { if (is_numeric($duration) && $duration < 0) { $this->dest->add(DateInterval::with(-$duration)); } else { $this->dest->sub(DateInterval::with($duration)); } + return $this; } /** @var string */ diff --git a/php/src/php/time/TODO.md b/php/src/php/time/TODO.md index edf17b0..f5ff33b 100644 --- a/php/src/php/time/TODO.md +++ b/php/src/php/time/TODO.md @@ -1,10 +1,9 @@ # nulib\php\time -* Date, DateTime sont immutables par défaut. par exemple, add() retourne un nouvel objet. - ajouter une version des méthodes qui modifie les données en place en les - préfixant de `_` e.g `_add()` - - en terme d'implémentation, dériver \DateTime pour supporter les modification - en place, bien que ce ne soit pas le fonctionnement par défaut +* refaire l'implémentation pour les délais INF. c'est un cas particulier qui + n'est jamais atteint. il faut donc implémenter un traitement spécifique dans + chaque méthode (i.e `if ($repr === "INF") { doSomething() }`) + * la destination est toujours 1000 ans dans le futur + * la différence est toujours de 1000 ans -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary \ No newline at end of file diff --git a/php/tbin/cachectl.php b/php/tbin/cachectl.php new file mode 100755 index 0000000..4afa7a5 --- /dev/null +++ b/php/tbin/cachectl.php @@ -0,0 +1,7 @@ +#!/usr/bin/php +get()); if ($dumpInfos) { diff --git a/php/tests/php/time/DateTimeTest.php b/php/tests/php/time/DateTimeTest.php index 0880c67..41ce15e 100644 --- a/php/tests/php/time/DateTimeTest.php +++ b/php/tests/php/time/DateTimeTest.php @@ -118,4 +118,12 @@ class DateTimeTest extends TestCase { self::assertFalse($b >= $b2); self::assertFalse($b >= $b3); } + + function testSerialize() { + $date = new DateTime(); + $serialized = serialize($date); + echo "serialized: $serialized\n"; + $unserialized = unserialize($serialized); + self::assertEquals($date, $unserialized); + } } diff --git a/php/tests/php/time/DelayTest.php b/php/tests/php/time/DelayTest.php index b86f6af..dca3321 100644 --- a/php/tests/php/time/DelayTest.php +++ b/php/tests/php/time/DelayTest.php @@ -76,4 +76,12 @@ class DelayTest extends TestCase { sleep(5); self::assertTrue($delay->isElapsed()); } + + function testSerialize() { + $delay = new Delay(5); + $serialized = serialize($delay); + echo "serialized: $serialized\n"; + $unserialized = unserialize($serialized); + self::assertEquals($delay, $unserialized); + } }