-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from spatie/feature/precise-time-for-next-pre…
…vious Make comparison microsecond-precise
- Loading branch information
Showing
6 changed files
with
184 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Spatie\OpeningHours; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use DateTimeZone; | ||
|
||
class PreciseTime extends Time | ||
{ | ||
/** @var DateTimeInterface */ | ||
protected $dateTime; | ||
|
||
protected function __construct(DateTimeInterface $dateTime) | ||
{ | ||
$this->dateTime = $dateTime; | ||
} | ||
|
||
public static function fromString(string $string): parent | ||
{ | ||
return self::fromDateTime(new DateTimeImmutable($string)); | ||
} | ||
|
||
public function hours(): int | ||
{ | ||
return (int) $this->dateTime->format('G'); | ||
} | ||
|
||
public function minutes(): int | ||
{ | ||
return (int) $this->dateTime->format('i'); | ||
} | ||
|
||
public static function fromDateTime(DateTimeInterface $dateTime): parent | ||
{ | ||
return new self($dateTime); | ||
} | ||
|
||
public function isSame(parent $time): bool | ||
{ | ||
return $this->format('H:i:s.u') === $time->format('H:i:s.u'); | ||
} | ||
|
||
public function isAfter(parent $time): bool | ||
{ | ||
return $this->format('H:i:s.u') > $time->format('H:i:s.u'); | ||
} | ||
|
||
public function isBefore(parent $time): bool | ||
{ | ||
return $this->format('H:i:s.u') < $time->format('H:i:s.u'); | ||
} | ||
|
||
public function isSameOrAfter(parent $time): bool | ||
{ | ||
return $this->format('H:i:s.u') >= $time->format('H:i:s.u'); | ||
} | ||
|
||
public function diff(parent $time): \DateInterval | ||
{ | ||
return $this->toDateTime()->diff($time->toDateTime()); | ||
} | ||
|
||
public function toDateTime(DateTimeInterface $date = null): DateTimeInterface | ||
{ | ||
return $date | ||
? $this->copyDateTime($date)->modify($this->format('H:i:s.u')) | ||
: $this->copyDateTime($this->dateTime); | ||
} | ||
|
||
public function format(string $format = 'H:i', $timezone = null): string | ||
{ | ||
$date = $timezone | ||
? $this->copyDateTime($this->dateTime)->setTimezone($timezone instanceof DateTimeZone | ||
? $timezone | ||
: new DateTimeZone($timezone) | ||
) | ||
: $this->dateTime; | ||
|
||
return $date->format($format); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Spatie\OpeningHours\Test; | ||
|
||
use DateTimeImmutable; | ||
use DateTimeZone; | ||
use PHPUnit\Framework\TestCase; | ||
use Spatie\OpeningHours\PreciseTime; | ||
|
||
class PreciseTimeTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_can_be_formatted() | ||
{ | ||
$date = new DateTimeImmutable('2022-08-07 23:32:58.123456 America/Toronto'); | ||
$this->assertSame( | ||
'2022-08-08 05:32:58.123456 Europe/Berlin', | ||
PreciseTime::fromDateTime($date)->format('Y-m-d H:i:s.u e', 'Europe/Berlin') | ||
); | ||
$this->assertSame( | ||
'2022-08-08 12:32:58.123456 Asia/Tokyo', | ||
PreciseTime::fromDateTime($date)->format('Y-m-d H:i:s.u e', new DateTimeZone('Asia/Tokyo')) | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_return_original_datetime() | ||
{ | ||
$date = new DateTimeImmutable('2022-08-07 23:32:58.123456 America/Toronto'); | ||
$this->assertSame($date, PreciseTime::fromDateTime($date)->toDateTime()); | ||
$this->assertSame('2021-11-25 23:32:58'.(PHP_VERSION < 7.1 ? '' : '.123456').' Asia/Tokyo', PreciseTime::fromDateTime($date)->toDateTime( | ||
new DateTimeImmutable('2021-11-25 15:02:03.987654 Asia/Tokyo') | ||
)->format('Y-m-d H:i:s'.(PHP_VERSION < 7.1 ? '' : '.u').' e')); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_return_diff() | ||
{ | ||
$date = new DateTimeImmutable('2021-08-07 23:32:58.123456 America/Toronto'); | ||
$this->assertSame( | ||
'02 29 05 '.(PHP_VERSION < 7.1 ? '%F' : '864198'), | ||
PreciseTime::fromDateTime($date)->diff(PreciseTime::fromDateTime( | ||
new DateTimeImmutable('2022-11-25 15:02:03.987654 Asia/Tokyo') | ||
))->format('%H %I %S %F') | ||
); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_be_compared() | ||
{ | ||
$date = new DateTimeImmutable('2022-08-07 23:32:58.123456 America/Toronto'); | ||
$this->assertSame($date, PreciseTime::fromDateTime($date)->toDateTime()); | ||
$this->assertTrue(PreciseTime::fromDateTime( | ||
new DateTimeImmutable('2022-08-07 23:32:58.123456 America/Toronto') | ||
)->isSame(PreciseTime::fromDateTime( | ||
new DateTimeImmutable('2021-11-25 23:32:58.123456 Asia/Tokyo') | ||
))); | ||
$this->assertFalse(PreciseTime::fromDateTime( | ||
new DateTimeImmutable('2022-08-07 23:32:58.123456 America/Toronto') | ||
)->isSame(PreciseTime::fromDateTime( | ||
new DateTimeImmutable('2022-08-07 23:32:58.123457 America/Toronto') | ||
))); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_output_hours_and_minutes() | ||
{ | ||
$date = PreciseTime::fromString('2022-08-07 23:32:58.123456 America/Toronto'); | ||
$this->assertSame(23, $date->hours()); | ||
$this->assertSame(32, $date->minutes()); | ||
} | ||
} |