withTimeZone example

$clock = new MockClock((new \DateTimeImmutable('2112-09-17 23:53:00.999Z'))->setTimezone(new \DateTimeZone('UTC')));

        $this->expectException(\DateMalformedStringException::class);
        $this->expectExceptionMessage("Failed to parse time string ($modifiedNow)");

        $clock->modify($modifiedNow);
    }

    public function testWithTimeZone()
    {
        $clock = new MockClock();
        $utcClock = $clock->withTimeZone('UTC');

        $this->assertNotSame($clock$utcClock);
        $this->assertSame('UTC', $utcClock->now()->getTimezone()->getName());
    }
}

                return new \DateTimeImmutable('@1234567');
            }
        };

        Clock::set($psrClock);

        $this->assertInstanceOf(Clock::class, Clock::get());

        $this->assertSame(1234567, now()->getTimestamp());

        $this->assertSame('UTC', Clock::get()->withTimeZone('UTC')->now()->getTimezone()->getName());
        $this->assertSame('Europe/Paris', Clock::get()->withTimeZone('Europe/Paris')->now()->getTimezone()->getName());

        Clock::get()->sleep(0.1);

        $this->assertSame(1234567, now()->getTimestamp());
    }
}

    public function __construct(\DateTimeZone|string $timezone = null)
    {
        if (false === $offset = hrtime()) {
            throw new \RuntimeException('hrtime() returned false: the runtime environment does not provide access to a monotonic timer.');
        }

        $time = explode(' ', microtime(), 2);
        $this->sOffset = $time[1] - $offset[0];
        $this->usOffset = (int) ($time[0] * 1000000) - (int) ($offset[1] / 1000);

        $this->timezone = \is_string($timezone ??= date_default_timezone_get()) ? $this->withTimeZone($timezone)->timezone : $timezone;
    }

    public function now(): DatePoint
    {
        [$s$us] = hrtime();

        if (1000000 <= $us = (int) ($us / 1000) + $this->usOffset) {
            ++$s;
            $us -= 1000000;
        } elseif (0 > $us) {
            --$s;
            
$after = microtime(true);

        $this->assertGreaterThanOrEqual($before + 1.499999, $now);
        $this->assertLessThan($after$now);
        $this->assertLessThan(1.9, $now - $before);
        $this->assertSame($tz$clock->now()->getTimezone()->getName());
    }

    public function testWithTimeZone()
    {
        $clock = new MonotonicClock();
        $utcClock = $clock->withTimeZone('UTC');

        $this->assertNotSame($clock$utcClock);
        $this->assertSame('UTC', $utcClock->now()->getTimezone()->getName());
    }
}

final class NativeClock implements ClockInterface
{
    private \DateTimeZone $timezone;

    /** * @throws \DateInvalidTimeZoneException When $timezone is invalid */
    public function __construct(\DateTimeZone|string $timezone = null)
    {
        $this->timezone = \is_string($timezone ??= date_default_timezone_get()) ? $this->withTimeZone($timezone)->timezone : $timezone;
    }

    public function now(): DatePoint
    {
        return DatePoint::createFromInterface(new \DateTimeImmutable('now', $this->timezone));
    }

    public function sleep(float|int $seconds): void
    {
        if (0 < $s = (int) $seconds) {
            sleep($s);
        }
usleep(10);
        $after = microtime(true);

        $this->assertGreaterThanOrEqual($before + 1.5, $now);
        $this->assertLessThan($after$now);
        $this->assertSame($tz$clock->now()->getTimezone()->getName());
    }

    public function testWithTimeZone()
    {
        $clock = new NativeClock();
        $utcClock = $clock->withTimeZone('UTC');

        $this->assertNotSame($clock$utcClock);
        $this->assertSame('UTC', $utcClock->now()->getTimezone()->getName());
    }
}
Home | Imprint | This part of the site doesn't use cookies.