Skip to content

Commit

Permalink
Merge pull request #381 from cakephp/get-native
Browse files Browse the repository at this point in the history
Add toNative() helpers
  • Loading branch information
markstory authored Mar 14, 2023
2 parents 74daa9a + 2f8c591 commit 7813680
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Chronos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,16 @@ public function diffForHumans(?Chronos $other = null, bool $absolute = false): s
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}

/**
* Returns the time as a DateTimeImmutable instance.
*
* @return \DateTimeImmutable
*/
public function toNative(): DateTimeImmutable
{
return $this->native;
}

/**
* Get a part of the object
*
Expand Down
10 changes: 10 additions & 0 deletions src/ChronosDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,16 @@ public function diffForHumans(?ChronosDate $other = null, bool $absolute = false
return static::diffFormatter()->diffForHumans($this, $other, $absolute);
}

/**
* Returns the date as a DateTimeImmutable instance.
*
* @return \DateTimeImmutable
*/
public function toNative(): DateTimeImmutable
{
return $this->native;
}

/**
* Get a part of the object
*
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/Date/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

class StringsTest extends TestCase
{
/**
* @var string
*/
protected $tz;

/**
* Setup
*
Expand Down Expand Up @@ -167,4 +172,10 @@ public function testToW3cString()
$d = ChronosDate::create(1975, 12, 25, 14, 15, 16);
$this->assertSame('1975-12-25T00:00:00+00:00', $d->toW3cString());
}

public function testToNative(): void
{
$d = ChronosDate::now();
$this->assertSame($d->format('Y-m-d'), $d->toNative()->format('Y-m-d'));
}
}
6 changes: 6 additions & 0 deletions tests/TestCase/DateTime/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,10 @@ public function testToWeek($date, $expected)
{
$this->assertSame($expected, (new Chronos($date))->toWeek());
}

public function testToNative(): void
{
$c = Chronos::now();
$this->assertSame($c->format(DATE_ATOM), $c->toNative()->format(DATE_ATOM));
}
}

0 comments on commit 7813680

Please sign in to comment.