Skip to content

Commit

Permalink
Added a format method to Time and TimeRange
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiandedeyne committed Apr 14, 2017
1 parent f4b1b26 commit f12cf1c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Time
{
/** @var int */
protected $hours;

/** @var int */
protected $minutes;

protected function __construct(int $hours, int $minutes)
Expand Down Expand Up @@ -68,11 +70,16 @@ public function isSameOrAfter(Time $time): bool

public function toDateTime(): DateTime
{
return new DateTime("1970-01-01 {$this}:00");
return (new DateTime('1970-01-01 00:00:00'))->setTime($this->hours, $this->minutes);
}

public function format(string $format = 'H:i'): string
{
return $this->toDateTime()->format($format);
}

public function __toString(): string
{
return str_pad($this->hours, 2, '0', STR_PAD_LEFT).':'.str_pad($this->minutes, 2, '0', STR_PAD_LEFT);
return $this->format();
}
}
11 changes: 9 additions & 2 deletions src/TimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class TimeRange
{
/** @var \Spatie\OpeningHours\Time */
protected $start;

/** @var \Spatie\OpeningHours\Time */
protected $end;

protected function __construct(Time $start, Time $end)
Expand Down Expand Up @@ -60,8 +62,13 @@ public function overlaps(TimeRange $timeRange): bool
return $this->containsTime($timeRange->start) || $this->containsTime($timeRange->end);
}

public function __toString()
public function format(string $timeFormat = 'H:i', string $rangeFormat = '%s-%s'): string
{
return sprintf($rangeFormat, $this->start->format($timeFormat), $this->end->format($timeFormat));
}

public function __toString(): string
{
return "{$this->start}-{$this->end}";
return $this->format();
}
}
8 changes: 8 additions & 0 deletions tests/TimeRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ public function it_can_determine_that_it_overlaps_another_time_range()
$this->assertFalse(TimeRange::fromString('16:00-18:00')->overlaps(TimeRange::fromString('14:00-15:00')));
$this->assertFalse(TimeRange::fromString('16:00-18:00')->overlaps(TimeRange::fromString('19:00-20:00')));
}

/** @test */
public function it_can_be_formatted()
{
$this->assertEquals('16:00-18:00', TimeRange::fromString('16:00-18:00')->format());
$this->assertEquals('16:00 - 18:00', TimeRange::fromString('16:00-18:00')->format('H:i', '%s - %s'));
$this->assertEquals('from 4 PM to 6 PM', TimeRange::fromString('16:00-18:00')->format('g A', 'from %s to %s'));
}
}
8 changes: 8 additions & 0 deletions tests/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ public function it_can_accept_any_date_format_with_the_date_time_interface()

$this->assertEquals('13:25', (string) Time::fromDateTime($dateTime));
}

/** @test */
public function it_can_be_formatted()
{
$this->assertEquals('09:00', Time::fromString('09:00')->format());
$this->assertEquals('09:00', Time::fromString('09:00')->format('H:i'));
$this->assertEquals('9 AM', Time::fromString('09:00')->format('g A'));
}
}

0 comments on commit f12cf1c

Please sign in to comment.