modifs.mineures sans commentaires
This commit is contained in:
parent
c3ef659bfe
commit
5b549cdaf3
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
namespace nur\sery\php\time;
|
namespace nur\sery\php\time;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
class DateInterval extends \DateInterval {
|
class DateInterval extends \DateInterval {
|
||||||
protected static function to_string(DateInterval $interval) {
|
protected static function to_string(DateInterval $interval) {
|
||||||
$string = "P";
|
$string = "P";
|
||||||
|
@ -22,7 +24,7 @@ class DateInterval extends \DateInterval {
|
||||||
}
|
}
|
||||||
|
|
||||||
function __construct($duration) {
|
function __construct($duration) {
|
||||||
if ($duration instanceof DateInterval) {
|
if ($duration instanceof \DateInterval) {
|
||||||
$this->y = $duration->y;
|
$this->y = $duration->y;
|
||||||
$this->m = $duration->m;
|
$this->m = $duration->m;
|
||||||
$this->d = $duration->d;
|
$this->d = $duration->d;
|
||||||
|
@ -31,6 +33,8 @@ class DateInterval extends \DateInterval {
|
||||||
$this->s = $duration->s;
|
$this->s = $duration->s;
|
||||||
$this->invert = $duration->invert;
|
$this->invert = $duration->invert;
|
||||||
$this->days = $duration->days;
|
$this->days = $duration->days;
|
||||||
|
} elseif (!is_string($duration)) {
|
||||||
|
throw new InvalidArgumentException("duration must be a string");
|
||||||
} else {
|
} else {
|
||||||
if (substr($duration, 0, 1) == "-") {
|
if (substr($duration, 0, 1) == "-") {
|
||||||
$duration = substr($duration, 1);
|
$duration = substr($duration, 1);
|
||||||
|
|
|
@ -8,16 +8,35 @@ use InvalidArgumentException;
|
||||||
/**
|
/**
|
||||||
* Class DateTime: une date et une heure
|
* Class DateTime: une date et une heure
|
||||||
*
|
*
|
||||||
* @property-read string $year
|
* @property-read int $year
|
||||||
* @property-read string $month
|
* @property-read int $month
|
||||||
* @property-read string $day
|
* @property-read int $day
|
||||||
* @property-read string $hour
|
* @property-read int $hour
|
||||||
* @property-read string $minute
|
* @property-read int $minute
|
||||||
* @property-read string $second
|
* @property-read int $second
|
||||||
|
* @property-read int $wday
|
||||||
|
* @property-read int $wnum
|
||||||
* @property-read string $timezone
|
* @property-read string $timezone
|
||||||
|
* @property-read string $datetime
|
||||||
|
* @property-read string $date
|
||||||
*/
|
*/
|
||||||
class DateTime extends \DateTime {
|
class DateTime extends \DateTime {
|
||||||
const DEFAULT_FORMAT = "d/m/Y H:i:s";
|
const DEFAULT_FORMAT = "d/m/Y H:i:s";
|
||||||
|
const INT_FORMATS = [
|
||||||
|
"year" => "Y",
|
||||||
|
"month" => "m",
|
||||||
|
"day" => "d",
|
||||||
|
"hour" => "H",
|
||||||
|
"minute" => "i",
|
||||||
|
"second" => "s",
|
||||||
|
"wday" => "N",
|
||||||
|
"wnum" => "W",
|
||||||
|
];
|
||||||
|
const STRING_FORMATS = [
|
||||||
|
"timezone" => "P",
|
||||||
|
"datetime" => "d/m/Y H:i:s",
|
||||||
|
"date" => "d/m/Y",
|
||||||
|
];
|
||||||
|
|
||||||
static function clone(DateTimeInterface $dateTime): self {
|
static function clone(DateTimeInterface $dateTime): self {
|
||||||
if ($dateTime instanceof static) return clone $dateTime;
|
if ($dateTime instanceof static) return clone $dateTime;
|
||||||
|
@ -33,6 +52,9 @@ class DateTime extends \DateTime {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->setTimestamp($datetime->getTimestamp());
|
$this->setTimestamp($datetime->getTimestamp());
|
||||||
$this->setTimezone($timezone);
|
$this->setTimezone($timezone);
|
||||||
|
} elseif (is_int($datetime)) {
|
||||||
|
parent::__construct();
|
||||||
|
$this->setTimestamp($datetime);
|
||||||
} else {
|
} else {
|
||||||
parent::__construct($datetime, $timezone);
|
parent::__construct($datetime, $timezone);
|
||||||
}
|
}
|
||||||
|
@ -51,15 +73,11 @@ class DateTime extends \DateTime {
|
||||||
}
|
}
|
||||||
|
|
||||||
function __get($name) {
|
function __get($name) {
|
||||||
switch ($name) {
|
if (array_key_exists($name, self::INT_FORMATS)) {
|
||||||
case "year": return $this->format("Y");
|
return intval($this->format(self::INT_FORMATS[$name]));
|
||||||
case "month": return $this->format("m");
|
} elseif (array_key_exists($name, self::STRING_FORMATS)) {
|
||||||
case "day": return $this->format("d");
|
return $this->format(self::STRING_FORMATS[$name]);
|
||||||
case "hour": return $this->format("H");
|
|
||||||
case "minute": return $this->format("i");
|
|
||||||
case "second": return $this->format("s");
|
|
||||||
case "timezone": return $this->format("P");
|
|
||||||
default: throw new InvalidArgumentException("Unknown property $name");
|
|
||||||
}
|
}
|
||||||
|
throw new InvalidArgumentException("Unknown property $name");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,15 +12,19 @@ class DateTest extends TestCase {
|
||||||
function testDate() {
|
function testDate() {
|
||||||
$date = self::dt("2024-04-05 09:15:23");
|
$date = self::dt("2024-04-05 09:15:23");
|
||||||
|
|
||||||
self::assertEquals("05/04/2024", $date->format());
|
self::assertSame("05/04/2024", $date->format());
|
||||||
self::assertEquals("05/04/2024", strval($date));
|
self::assertSame("05/04/2024", strval($date));
|
||||||
self::assertEquals("2024", $date->year);
|
self::assertSame(2024, $date->year);
|
||||||
self::assertEquals("04", $date->month);
|
self::assertSame(4, $date->month);
|
||||||
self::assertEquals("05", $date->day);
|
self::assertSame(5, $date->day);
|
||||||
self::assertEquals("00", $date->hour);
|
self::assertSame(0, $date->hour);
|
||||||
self::assertEquals("00", $date->minute);
|
self::assertSame(0, $date->minute);
|
||||||
self::assertEquals("00", $date->second);
|
self::assertSame(0, $date->second);
|
||||||
self::assertEquals("+04:00", $date->timezone);
|
self::assertSame(5, $date->wday);
|
||||||
|
self::assertSame(14, $date->wnum);
|
||||||
|
self::assertSame("+04:00", $date->timezone);
|
||||||
|
self::assertSame("05/04/2024 00:00:00", $date->datetime);
|
||||||
|
self::assertSame("05/04/2024", $date->date);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testClone() {
|
function testClone() {
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
namespace nur\sery\php\time;
|
namespace nur\sery\php\time;
|
||||||
|
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
use nur\sery\php\time\Date;
|
|
||||||
use nur\sery\php\time\DateTime;
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class DateTimeTest extends TestCase {
|
class DateTimeTest extends TestCase {
|
||||||
|
@ -16,13 +14,17 @@ class DateTimeTest extends TestCase {
|
||||||
|
|
||||||
self::assertEquals("05/04/2024 09:15:23", $date->format());
|
self::assertEquals("05/04/2024 09:15:23", $date->format());
|
||||||
self::assertEquals("05/04/2024 09:15:23", strval($date));
|
self::assertEquals("05/04/2024 09:15:23", strval($date));
|
||||||
self::assertEquals("2024", $date->year);
|
self::assertSame(2024, $date->year);
|
||||||
self::assertEquals("04", $date->month);
|
self::assertSame(4, $date->month);
|
||||||
self::assertEquals("05", $date->day);
|
self::assertSame(5, $date->day);
|
||||||
self::assertEquals("09", $date->hour);
|
self::assertSame(9, $date->hour);
|
||||||
self::assertEquals("15", $date->minute);
|
self::assertSame(15, $date->minute);
|
||||||
self::assertEquals("23", $date->second);
|
self::assertSame(23, $date->second);
|
||||||
|
self::assertSame(5, $date->wday);
|
||||||
|
self::assertSame(14, $date->wnum);
|
||||||
self::assertEquals("+04:00", $date->timezone);
|
self::assertEquals("+04:00", $date->timezone);
|
||||||
|
self::assertSame("05/04/2024 09:15:23", $date->datetime);
|
||||||
|
self::assertSame("05/04/2024", $date->date);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testClone() {
|
function testClone() {
|
||||||
|
|
Loading…
Reference in New Issue