<?php
namespace nur\sery\php\time;

use DateTimeZone;
use nulib\tests\TestCase;

class DateTest extends TestCase {
  protected static function dt(string $datetime): Date {
    return new Date($datetime, new DateTimeZone("Indian/Reunion"));
  }

  function testDate() {
    $date = self::dt("2024-04-05 09:15:23");

    self::assertSame("05/04/2024", $date->format());
    self::assertSame("05/04/2024", strval($date));
    self::assertSame(2024, $date->year);
    self::assertSame(4, $date->month);
    self::assertSame(5, $date->day);
    self::assertSame(0, $date->hour);
    self::assertSame(0, $date->minute);
    self::assertSame(0, $date->second);
    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() {
    $date = self::dt("now");
    $clone = Date::clone($date);
    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")));
  }

  function testCompare() {
    $a = new Date("10/02/2024");
    $b = new Date("15/02/2024");
    $c = new Date("20/02/2024");
    $a2 = new Date("10/02/2024");
    $b2 = new Date("15/02/2024");
    $c2 = new Date("20/02/2024");

    self::assertTrue($a == $a2);
    self::assertFalse($a === $a2);
    self::assertTrue($b == $b2);
    self::assertTrue($c == $c2);

    self::assertFalse($a < $a);
    self::assertTrue($a < $b);
    self::assertTrue($a < $c);

    self::assertTrue($a <= $a);
    self::assertTrue($a <= $b);
    self::assertTrue($a <= $c);

    self::assertFalse($c > $c);
    self::assertTrue($c > $b);
    self::assertTrue($c > $a);

    self::assertTrue($c >= $c);
    self::assertTrue($c >= $b);
    self::assertTrue($c >= $a);
  }
}
