modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-04-06 11:32:25 +04:00
parent 5b549cdaf3
commit b28cbd7099
2 changed files with 78 additions and 1 deletions

View File

@ -46,6 +46,49 @@ class DateTime extends \DateTime {
return $clone; return $clone;
} }
/**
* corriger une année à deux chiffres qui est située dans le passé et
* retourner l'année à 4 chiffres.
*
* par exemple, si l'année courante est 2019, alors:
* - fix_past_year('18') === '2018'
* - fix_past_year('19') === '1919'
* - fix_past_year('20') === '1920'
*/
static function fix_past_year(int $year): int {
if ($year < 100) {
$y = getdate(); $y = $y["year"];
$r = $y % 100;
$c = $y - $r;
if ($year >= $r) $year += $c - 100;
else $year += $c;
}
return $year;
}
/**
* corriger une année à deux chiffres et retourner l'année à 4 chiffres.
* l'année charnière entre année passée et année future est 70
*
* par exemple, si l'année courante est 2019, alors:
* - fix_past_year('18') === '2018'
* - fix_past_year('19') === '2019'
* - fix_past_year('20') === '2020'
* - fix_past_year('69') === '2069'
* - fix_past_year('70') === '1970'
* - fix_past_year('71') === '1971'
*/
static function fix_any_year(int $year): int {
if ($year < 100) {
$y = intval(date("Y"));
$r = $y % 100;
$c = $y - $r;
if ($year >= 70) $year += $c - 100;
else $year += $c;
}
return $year;
}
function __construct($datetime="now", DateTimeZone $timezone=null) { function __construct($datetime="now", DateTimeZone $timezone=null) {
if ($datetime instanceof \DateTimeInterface) { if ($datetime instanceof \DateTimeInterface) {
if ($timezone === null) $timezone = $datetime->getTimezone(); if ($timezone === null) $timezone = $datetime->getTimezone();
@ -53,9 +96,23 @@ class DateTime extends \DateTime {
$this->setTimestamp($datetime->getTimestamp()); $this->setTimestamp($datetime->getTimestamp());
$this->setTimezone($timezone); $this->setTimezone($timezone);
} elseif (is_int($datetime)) { } elseif (is_int($datetime)) {
parent::__construct(); parent::__construct("now", $timezone);
$this->setTimestamp($datetime); $this->setTimestamp($datetime);
} elseif (!is_string($datetime)) {
throw new InvalidArgumentException("datetime must be a string or an instance of DateTimeInterface");
} else { } else {
if (preg_match('/^(\d+)\/(\d+)\/(\d+)$/', $datetime, $ms)) {
$y = self::fix_any_year(intval($ms[3]));
$datetime = sprintf("%04d-%02d-%02d", $y, intval($ms[2]), intval($ms[1]));
} elseif (preg_match('/^(\d+)\/(\d+)$/', $datetime, $ms)) {
$datetime = sprintf("%04d-%02d-%02d", intval(date("Y")), intval($ms[2]), intval($ms[1]));
} elseif (preg_match('/^(\d{4})(\d{2})(\d{2})$/', $datetime, $ms)) {
$datetime = sprintf("%04d-%02d-%02d", intval($ms[1]), intval($ms[2]), intval($ms[3]));
} elseif (preg_match('/^(\d{2})(\d{2})(\d{2})$/', $datetime, $ms)) {
$y = self::fix_any_year(intval($ms[1]));
$datetime = sprintf("%04d-%02d-%02d", $y, intval($ms[2]), intval($ms[3]));
}
#XXX ajouter datetime et YYYYmmddTHHMMSS[Z]
parent::__construct($datetime, $timezone); parent::__construct($datetime, $timezone);
} }
} }

View File

@ -32,4 +32,24 @@ class DateTest extends TestCase {
$clone = Date::clone($date); $clone = Date::clone($date);
self::assertInstanceOf(DateTime::class, $clone); self::assertInstanceOf(DateTime::class, $clone);
} }
function testConstruct() {
$y = date("Y");
self::assertSame("05/04/$y", strval(new Date("5/4")));
self::assertSame("05/04/2024", strval(new Date("5/4/24")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024")));
self::assertSame("05/04/2024", strval(new Date("05/04/2024")));
self::assertSame("05/04/2024", strval(new Date("20240405")));
self::assertSame("05/04/2024", strval(new Date("240405")));
self::assertSame("05/04/2024", strval(new Date("20240405T091523")));
self::assertSame("05/04/2024", strval(new Date("20240405T091523Z")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 9:15:23")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 9.15.23")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 9:15")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 9.15")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 9h15")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 09:15:23")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 09:15")));
self::assertSame("05/04/2024", strval(new Date("5/4/2024 09h15")));
}
} }