1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Incrementing clock for testing purposes.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
* @property-read int $time The current time of the clock
|
|
|
24 |
*/
|
|
|
25 |
class incrementing_clock implements \core\clock {
|
|
|
26 |
/** @var int The next time of the clock */
|
|
|
27 |
public int $time;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Create a new instance of the incrementing clock.
|
|
|
31 |
*
|
|
|
32 |
* @param null|int $starttime The initial time to use. If not specified, the current time is used.
|
|
|
33 |
*/
|
|
|
34 |
public function __construct(
|
|
|
35 |
?int $starttime = null,
|
|
|
36 |
) {
|
|
|
37 |
$this->time = $starttime ?? time();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
public function now(): \DateTimeImmutable {
|
|
|
41 |
return new \DateTimeImmutable('@' . $this->time++);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public function time(): int {
|
|
|
45 |
return $this->now()->getTimestamp();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Set the time of the clock.
|
|
|
50 |
*
|
|
|
51 |
* @param int $time
|
|
|
52 |
*/
|
|
|
53 |
public function set_to(int $time): void {
|
|
|
54 |
$this->time = $time;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Bump the time by a number of seconds.
|
|
|
59 |
*
|
|
|
60 |
* Note: The act of fetching the time will also bump the time by one second.
|
|
|
61 |
*
|
|
|
62 |
* @param int $seconds
|
|
|
63 |
*/
|
|
|
64 |
public function bump(int $seconds = 1): void {
|
|
|
65 |
$this->time += $seconds;
|
|
|
66 |
}
|
|
|
67 |
}
|