Skip to content

Commit

Permalink
allow to switch the clock timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Nov 21, 2024
1 parent e4ece47 commit c0811f1
Show file tree
Hide file tree
Showing 20 changed files with 2,867 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

## Added

- `Innmind\TimeContinuum\Clock::switch()`

### Changed

- `Innmind\TimeContinuum\Format` is now a `final class`
Expand Down
12 changes: 10 additions & 2 deletions src/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ private function __construct(

public static function live(): self
{
return new self(new Live);
return new self(new Live(Offset::utc()));
}

public static function frozen(PointInTime $point): self
{
return new self(new Frozen($point));
return new self(new Frozen($point, new Live(Offset::utc())));
}

public static function logger(self $clock, LoggerInterface $logger): self
Expand All @@ -38,6 +38,14 @@ public function now(): PointInTime
return $this->implementation->now();
}

/**
* @param callable(Timezones): Timezone $changeTimezone
*/
public function switch(callable $changeTimezone): self
{
return new self($this->implementation->switch($changeTimezone));
}

/**
* @psalm-pure
*
Expand Down
17 changes: 15 additions & 2 deletions src/Clock/Frozen.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Innmind\TimeContinuum\{
PointInTime,
Format,
Timezones,
Timezone,
};
use Innmind\Immutable\Maybe;

Expand All @@ -17,10 +19,21 @@ final class Frozen
private PointInTime $now;
private Live $concrete;

public function __construct(PointInTime $now)
public function __construct(PointInTime $now, Live $concrete)
{
$this->now = $now;
$this->concrete = new Live;
$this->concrete = $concrete;
}

/**
* @param callable(Timezones): Timezone $changeTimezone
*/
public function switch(callable $changeTimezone): self
{
return new self(
$this->now,
$this->concrete->switch($changeTimezone),
);
}

public function now(): PointInTime
Expand Down
27 changes: 25 additions & 2 deletions src/Clock/Live.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Offset,
PointInTime,
Format,
Timezones,
Timezone,
};
use Innmind\Immutable\Maybe;

Expand All @@ -17,9 +19,30 @@ final class Live
{
private Offset $offset;

public function __construct()
public function __construct(Offset $offset)
{
$this->offset = Offset::utc();
$this->offset = $offset;
}

/**
* @param callable(Timezones): Timezone $changeTimezone
*/
public function switch(callable $changeTimezone): self
{
/** @var callable(non-empty-string): Timezone */
$of = static function(string $zone): Timezone {
/** @var non-empty-string $zone */
$now = (new \DateTimeImmutable('now'))->setTimezone(new \DateTimeZone($zone));

return Timezone::of(
Offset::from($now->format('P')),
(bool) (int) $now->format('I'),
);
};

return new self(
$changeTimezone(Timezones::new($of))->offset(),
);
}

public function now(): PointInTime
Expand Down
13 changes: 13 additions & 0 deletions src/Clock/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Clock,
PointInTime,
Format,
Timezones,
Timezone,
};
use Innmind\Immutable\Maybe;
use Psr\Log\LoggerInterface;
Expand All @@ -25,6 +27,17 @@ public function __construct(Clock $clock, LoggerInterface $logger)
$this->logger = $logger;
}

/**
* @param callable(Timezones): Timezone $changeTimezone
*/
public function switch(callable $changeTimezone): self
{
return new self(
$this->clock->switch($changeTimezone),
$this->logger,
);
}

public function now(): PointInTime
{
$now = $this->clock->now();
Expand Down
31 changes: 31 additions & 0 deletions src/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types = 1);

namespace Innmind\TimeContinuum;

final class Timezone
{
private function __construct(
private Offset $offset,
private bool $dst,
) {
}

/**
* @internal
*/
public static function of(Offset $offset, bool $daylightSavingTime): self
{
return new self($offset, $daylightSavingTime);
}

public function offset(): Offset
{
return $this->offset;
}

public function daylightSavingTimeApplied(): bool
{
return $this->dst;
}
}
Loading

0 comments on commit c0811f1

Please sign in to comment.