Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2642,6 +2642,25 @@ public function toNative(): DateTimeImmutable
return new DateTimeImmutable($this->format('Y-m-d H:i:s.u'), $this->getTimezone());
}

/**
* Returns the date and time as an associative array.
*
* @return array{year: int, month: int, day: int, hour: int, minute: int, second: int, microsecond: int, timezone: string}
*/
public function toArray(): array
{
return [
'year' => $this->year,
'month' => $this->month,
'day' => $this->day,
'hour' => $this->hour,
'minute' => $this->minute,
'second' => $this->second,
'microsecond' => $this->microsecond,
'timezone' => $this->timezone->getName(),
];
}

/**
* Get a part of the object
*
Expand Down
14 changes: 14 additions & 0 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,20 @@ public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImm
return $this->toDateTimeImmutable($timezone);
}

/**
* Returns the date as an associative array.
*
* @return array{year: int, month: int, day: int}
*/
public function toArray(): array
{
return [
'year' => $this->year,
'month' => $this->month,
'day' => $this->day,
];
}

/**
* Get a part of the object
*
Expand Down
15 changes: 15 additions & 0 deletions src/ChronosTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,19 @@ public function toNative(DateTimeZone|string|null $timezone = null): DateTimeImm
{
return $this->toDateTimeImmutable($timezone);
}

/**
* Returns the time as an associative array.
*
* @return array{hour: int, minute: int, second: int, microsecond: int}
*/
public function toArray(): array
{
return [
'hour' => $this->getHours(),
'minute' => $this->getMinutes(),
'second' => $this->getSeconds(),
'microsecond' => $this->getMicroseconds(),
];
}
}
12 changes: 12 additions & 0 deletions tests/TestCase/ChronosTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,16 @@ public function testToString(): void
ChronosTime::resetToStringFormat();
$this->assertSame('12:13:14', (string)$t);
}

public function testToArray(): void
{
$t = new ChronosTime('12:30:45.123456');
$array = $t->toArray();

$this->assertSame(12, $array['hour']);
$this->assertSame(30, $array['minute']);
$this->assertSame(45, $array['second']);
$this->assertSame(123456, $array['microsecond']);
$this->assertCount(4, $array);
}
}
11 changes: 11 additions & 0 deletions tests/TestCase/Date/GettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public function testHalfOfYear(int $month, int $expectedHalfOfYear): void
$d = ChronosDate::create(year: 2012, month: $month, day: 1);
$this->assertSame($expectedHalfOfYear, $d->half);
}

public function testToArray(): void
{
$d = ChronosDate::create(2024, 1, 15);
$array = $d->toArray();

$this->assertSame(2024, $array['year']);
$this->assertSame(1, $array['month']);
$this->assertSame(15, $array['day']);
$this->assertCount(3, $array);
}
}
15 changes: 15 additions & 0 deletions tests/TestCase/DateTime/GettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,19 @@ public function testInvalidGetter()
$d = Chronos::now();
$d->doesNotExit;
}

public function testToArray(): void
{
$d = Chronos::create(2024, 1, 15, 12, 30, 45, 123456, 'America/Toronto');
$array = $d->toArray();

$this->assertSame(2024, $array['year']);
$this->assertSame(1, $array['month']);
$this->assertSame(15, $array['day']);
$this->assertSame(12, $array['hour']);
$this->assertSame(30, $array['minute']);
$this->assertSame(45, $array['second']);
$this->assertSame(123456, $array['microsecond']);
$this->assertSame('America/Toronto', $array['timezone']);
}
}
Loading