Skip to content

Instantly share code, notes, and snippets.

@xphere
Last active May 10, 2017 15:26
Show Gist options
  • Save xphere/c213198c03b502733407c003d55108c6 to your computer and use it in GitHub Desktop.
Save xphere/c213198c03b502733407c003d55108c6 to your computer and use it in GitHub Desktop.
<?php // Full license: https://gist.github.com/xphere/710839fba51cdd582ebd4ddf635cc0c4
use DateTimeImmutable as Time;
final class Watch
{
/** @var Time|null */
private static $frozenTime = null;
public static function fromFormat($dateString, $format = 'Y-m-d H:i:s'): Time
{
return Time::createFromFormat($format, $dateString);
}
public static function now(): Time
{
return self::$frozenTime ?: new Time();
}
public static function at($time): Time
{
return self::$frozenTime
? self::$frozenTime->modify($time)
: new Time($time);
}
public static function freeze(Time $time = null)
{
assert(null === self::$frozenTime);
self::$frozenTime = $time ?: new Time();
}
public static function unfreeze()
{
self::$frozenTime = null;
}
// Avoid construction by making this private
private function __construct()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment