Last active
May 10, 2017 15:26
-
-
Save xphere/c213198c03b502733407c003d55108c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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