3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 class CRM_Utils_Time
{
24 * A function which determines the current time.
25 * Only used during testing (with mocked time).
27 * The normal value, NULL, indicates the use of real time.
31 static private $callback = NULL;
36 * @param string $returnFormat
37 * Format in which date is to be retrieved.
41 public static function getTime($returnFormat = 'YmdHis') {
42 return date($returnFormat, self
::getTimeRaw());
51 public static function getTimeRaw() {
52 return self
::$callback === NULL ?
time() : call_user_func(self
::$callback);
58 * @param string $newDateTime
59 * A date formatted with strtotime.
60 * @param string $returnFormat
61 * Format in which date is to be retrieved.
63 * Note: The progression of time will be influenced by TIME_FUNC, which may be:
64 * - 'frozen' (time does not move)
65 * - 'natural' (time moves naturally)
66 * - 'linear:XXX' (time moves in increments of XXX milliseconds - with every lookup)
67 * - 'prng:XXX' (time moves by random increments, between 0 and XXX milliseconds)
70 public static function setTime($newDateTime, $returnFormat = 'YmdHis') {
71 $mode = getenv('TIME_FUNC') ?
getenv('TIME_FUNC') : 'natural';
73 list ($modeName, $modeNum) = explode(":", "$mode:");
77 // Every getTime() will produce the same value (ie $newDateTime).
78 $now = strtotime($newDateTime);
79 self
::$callback = function () use ($now) {
85 // Time changes to $newDateTime and then proceeds naturally.
86 $delta = strtotime($newDateTime) - time();
87 self
::$callback = function () use ($delta) {
88 return time() +
$delta;
93 // Time changes to $newDateTime and then proceeds in fixed increments ($modeNum milliseconds).
94 $incr = ($modeNum / 1000.0);
95 $now = (float) strtotime($newDateTime) - $incr;
96 self
::$callback = function () use (&$now, $incr) {
103 // Time changes to $newDateTime and then proceeds using deterministic pseudorandom increments (of up to $modeNum milliseconds).
104 $seed = md5($newDateTime . chr(0) . $mode, TRUE);
105 $now = (float) strtotime($newDateTime);
106 self
::$callback = function () use (&$seed, &$now, $modeNum) {
107 $mod = gmp_strval(gmp_mod(gmp_import($seed), "$modeNum"));
108 $seed = md5($seed . $now, TRUE);
109 $now = $now +
($mod / 1000.0);
115 throw new \
RuntimeException("Unrecognized TIME_FUNC ($mode)");
118 return self
::getTime($returnFormat);
122 * Remove any time overrides.
124 public static function resetTime() {
125 self
::$callback = NULL;
129 * Approximate time-comparison. $a and $b are considered equal if they
130 * are within $threshold seconds of each other.
133 * Time which can be parsed by strtotime.
135 * Time which can be parsed by strtotime.
136 * @param int $threshold
137 * Maximum allowed difference (in seconds).
140 public static function isEqual($a, $b, $threshold = 0) {
141 $diff = strtotime($b) - strtotime($a);
142 return (abs($diff) <= $threshold);