Skip to content

Commit

Permalink
Merge pull request #13 from PhantPHP/date-interface
Browse files Browse the repository at this point in the history
Date interface
  • Loading branch information
lennyrouanet authored Sep 21, 2022
2 parents f9d3691 + 2dad829 commit 2b43610
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
16 changes: 7 additions & 9 deletions component/Time/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ class Date extends \Phant\DataStructure\Abstract\Value\Varchar
{
protected int $time;

public function __construct(string $date, string $format = 'Y-m-d')
public function __construct(int|string $date, string $format = 'Y-m-d')
{
if (strtolower($date) == 'now') {
$date = 'today midnight';
if (is_string($date) && strtolower($date) == 'now') {
$date = strtotime('today midnight');
}

$time = strtotime($date);
$time = is_string($date) ? strtotime($date) : $date;

if ($time === false) {
throw new NotCompliant('Date: ' . $date);
}

$this->time = $time;

$date = date($format, $this->time);

parent::__construct($date);
Expand All @@ -29,9 +32,4 @@ public function getTime(): int
{
return $this->time;
}

public function getUtc(): string
{
return gmdate('Y-m-d\TH:i:s\Z', $this->time);
}
}
12 changes: 6 additions & 6 deletions component/Time/DateInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class DateInterval extends \Phant\DataStructure\Abstract\Aggregate
protected ?Duration $duration;

public function __construct(
null|string|Date $from,
null|string|Date $to
null|int|string|Date $from,
null|int|string|Date $to
)
{
if (!$from && !$to) {
throw new NotCompliant('Date intervals: from ' . $from . ' to' . $to);
}

if (is_string($from)) {
if (is_string($from) || is_int($from)) {
$from = new Date($from);
}

if (is_string($to)) {
if (is_string($to) || is_int($to)) {
$to = new Date($to);
}

Expand All @@ -53,9 +53,9 @@ public function getDuration(): ?Duration
return $this->duration;
}

public function isDuring(string|Date $date): bool
public function isDuring(string|int|Date $date): bool
{
if (is_string($date)) {
if (is_string($date) || is_int($date)) {
$date = new Date($date);
}

Expand Down
11 changes: 8 additions & 3 deletions component/Time/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

class DateTime extends \Phant\DataStructure\Time\Date
{
public function __construct(string $date, string $format = 'Y-m-d H:i:s')
public function __construct(int|string $date, string $format = 'Y-m-d H:i:s')
{
if (strtolower($date) == 'now') {
$date = 'now';
if (is_string($date) && strtolower($date) == 'now') {
$date = strtotime('now');
}

parent::__construct($date, $format);
}

public function getUtc(): string
{
return gmdate('Y-m-d\TH:i:s\Z', $this->time + date('Z', $this->time));
}
}
12 changes: 6 additions & 6 deletions component/Time/DateTimeInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class DateTimeInterval extends \Phant\DataStructure\Abstract\Aggregate
protected ?Duration $duration;

public function __construct(
null|string|DateTime $from,
null|string|DateTime $to
null|int|string|DateTime $from,
null|int|string|DateTime $to
)
{
if (!$from && !$to) {
throw new NotCompliant('Date time intervals: from ' . $from . ' to' . $to);
}

if (is_string($from)) {
if (is_string($from) || is_int($from)) {
$from = new DateTime($from);
}

if (is_string($to)) {
if (is_string($to) || is_int($to)) {
$to = new DateTime($to);
}

Expand All @@ -53,9 +53,9 @@ public function getDuration(): ?Duration
return $this->duration;
}

public function isDuring(string|DateTime $dateTime): bool
public function isDuring(int|string|DateTime $dateTime): bool
{
if (is_string($dateTime)) {
if (is_string($dateTime) || is_int($dateTime)) {
$dateTime = new DateTime($dateTime);
}

Expand Down
13 changes: 10 additions & 3 deletions test/Time/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public function testInterface(): void

$this->assertIsInt($date->getTime());
$this->assertEquals(-491356800, $date->getTime());

$this->assertIsString($date->getUtc());
$this->assertEquals('1954-06-07T00:00:00Z', $date->getUtc());
}

public function testBuild(): void
Expand All @@ -32,6 +29,16 @@ public function testBuild(): void
$this->assertIsObject($date);
}

public function testBuildFromTime(): void
{
$date = new Date(-491356800);

$this->assertIsObject($date);

$this->assertIsString($date->get());
$this->assertEquals('1954-06-07', $date->get());
}

public function testNotCompliant(): void
{
$this->expectException(NotCompliant::class);
Expand Down
10 changes: 10 additions & 0 deletions test/Time/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function testBuild(): void
$this->assertIsObject($dateTime);
}

public function testBuildFromTime(): void
{
$dateTime = new DateTime(-491311504);

$this->assertIsObject($dateTime);

$this->assertIsString($dateTime->get());
$this->assertEquals('1954-06-07 12:34:56', $dateTime->get());
}

public function testNotCompliant(): void
{
$this->expectException(NotCompliant::class);
Expand Down

0 comments on commit 2b43610

Please sign in to comment.