2024-04-06 05:36:48 +04:00
|
|
|
<?php
|
|
|
|
namespace nur\sery\php\time;
|
|
|
|
|
|
|
|
use DateTimeZone;
|
2024-04-25 17:46:14 +04:00
|
|
|
use nulib\tests\TestCase;
|
2024-04-06 05:36:48 +04:00
|
|
|
|
|
|
|
class DateTimeTest extends TestCase {
|
|
|
|
protected static function dt(string $datetime): DateTime {
|
|
|
|
return new DateTime($datetime, new DateTimeZone("Indian/Reunion"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDateTime() {
|
|
|
|
$date = self::dt("2024-04-05 09:15:23");
|
|
|
|
|
|
|
|
self::assertEquals("05/04/2024 09:15:23", $date->format());
|
|
|
|
self::assertEquals("05/04/2024 09:15:23", strval($date));
|
2024-04-06 06:05:48 +04:00
|
|
|
self::assertSame(2024, $date->year);
|
|
|
|
self::assertSame(4, $date->month);
|
|
|
|
self::assertSame(5, $date->day);
|
|
|
|
self::assertSame(9, $date->hour);
|
|
|
|
self::assertSame(15, $date->minute);
|
|
|
|
self::assertSame(23, $date->second);
|
|
|
|
self::assertSame(5, $date->wday);
|
|
|
|
self::assertSame(14, $date->wnum);
|
2024-04-06 05:36:48 +04:00
|
|
|
self::assertEquals("+04:00", $date->timezone);
|
2024-04-06 06:05:48 +04:00
|
|
|
self::assertSame("05/04/2024 09:15:23", $date->datetime);
|
|
|
|
self::assertSame("05/04/2024", $date->date);
|
2024-04-22 23:41:19 +04:00
|
|
|
self::assertSame("20240405", $date->Ymd);
|
|
|
|
self::assertSame("20240405T091523", $date->YmdHMS);
|
|
|
|
self::assertSame("20240405T091523+04:00", $date->YmdHMSZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDateTimeZ() {
|
|
|
|
$date = new DateTime("20240405T091523Z");
|
|
|
|
self::assertSame("20240405T091523", $date->YmdHMS);
|
|
|
|
self::assertSame("20240405T091523Z", $date->YmdHMSZ);
|
2024-04-06 05:36:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testClone() {
|
|
|
|
$date = self::dt("now");
|
|
|
|
$clone = DateTime::clone($date);
|
|
|
|
self::assertInstanceOf(DateTime::class, $clone);
|
|
|
|
}
|
2024-04-22 23:41:19 +04:00
|
|
|
|
|
|
|
function testConstruct() {
|
|
|
|
$y = date("Y");
|
|
|
|
self::assertSame("05/04/$y 00:00:00", strval(new DateTime("5/4")));
|
|
|
|
self::assertSame("05/04/2024 00:00:00", strval(new DateTime("5/4/24")));
|
|
|
|
self::assertSame("05/04/2024 00:00:00", strval(new DateTime("5/4/2024")));
|
|
|
|
self::assertSame("05/04/2024 00:00:00", strval(new DateTime("05/04/2024")));
|
|
|
|
self::assertSame("05/04/2024 00:00:00", strval(new DateTime("20240405")));
|
|
|
|
self::assertSame("05/04/2024 00:00:00", strval(new DateTime("240405")));
|
|
|
|
self::assertSame("05/04/2024 09:15:23", strval(new DateTime("20240405T091523")));
|
|
|
|
self::assertSame("05/04/2024 09:15:23", strval(new DateTime("20240405T091523Z")));
|
|
|
|
self::assertSame("05/04/2024 09:15:23", strval(new DateTime("5/4/2024 9:15:23")));
|
|
|
|
self::assertSame("05/04/2024 09:15:23", strval(new DateTime("5/4/2024 9.15.23")));
|
|
|
|
self::assertSame("05/04/2024 09:15:00", strval(new DateTime("5/4/2024 9:15")));
|
|
|
|
self::assertSame("05/04/2024 09:15:00", strval(new DateTime("5/4/2024 9.15")));
|
|
|
|
self::assertSame("05/04/2024 09:15:00", strval(new DateTime("5/4/2024 9h15")));
|
|
|
|
self::assertSame("05/04/2024 09:15:23", strval(new DateTime("5/4/2024 09:15:23")));
|
|
|
|
self::assertSame("05/04/2024 09:15:00", strval(new DateTime("5/4/2024 09:15")));
|
|
|
|
self::assertSame("05/04/2024 09:15:00", strval(new DateTime("5/4/2024 09h15")));
|
|
|
|
}
|
2024-06-25 23:01:48 +04:00
|
|
|
|
|
|
|
function testCompare() {
|
|
|
|
$a = new DateTime("10/02/2024");
|
|
|
|
$a2 = new DateTime("10/02/2024 8:30");
|
|
|
|
$a3 = new DateTime("10/02/2024 15:45");
|
|
|
|
$b = new DateTime("15/02/2024");
|
|
|
|
$b2 = new DateTime("15/02/2024 8:30");
|
|
|
|
$b3 = new DateTime("15/02/2024 15:45");
|
|
|
|
$x = new DateTime("10/02/2024");
|
|
|
|
$x2 = new DateTime("10/02/2024 8:30");
|
|
|
|
$x3 = new DateTime("10/02/2024 15:45");
|
|
|
|
|
|
|
|
self::assertTrue($a == $x);
|
|
|
|
self::assertFalse($a === $x);
|
|
|
|
self::assertTrue($a2 == $x2);
|
|
|
|
self::assertTrue($a3 == $x3);
|
|
|
|
|
|
|
|
self::assertFalse($a < $a);
|
|
|
|
self::assertTrue($a < $a2);
|
|
|
|
self::assertTrue($a < $a3);
|
|
|
|
self::assertTrue($a < $b);
|
|
|
|
self::assertTrue($a < $b2);
|
|
|
|
self::assertTrue($a < $b3);
|
|
|
|
|
|
|
|
self::assertTrue($a <= $a);
|
|
|
|
self::assertTrue($a <= $a2);
|
|
|
|
self::assertTrue($a <= $a3);
|
|
|
|
self::assertTrue($a <= $b);
|
|
|
|
self::assertTrue($a <= $b2);
|
|
|
|
self::assertTrue($a <= $b3);
|
|
|
|
|
|
|
|
self::assertTrue($b > $a);
|
|
|
|
self::assertTrue($b > $a2);
|
|
|
|
self::assertTrue($b > $a3);
|
|
|
|
self::assertFalse($b > $b);
|
|
|
|
self::assertFalse($b > $b2);
|
|
|
|
self::assertFalse($b > $b3);
|
|
|
|
|
|
|
|
self::assertTrue($b >= $a);
|
|
|
|
self::assertTrue($b >= $a2);
|
|
|
|
self::assertTrue($b >= $a3);
|
|
|
|
self::assertTrue($b >= $b);
|
|
|
|
self::assertFalse($b >= $b2);
|
|
|
|
self::assertFalse($b >= $b3);
|
|
|
|
}
|
2024-04-06 05:36:48 +04:00
|
|
|
}
|