Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
1441 ariadna 29
    /** @var DateTimeZone The system timezone. */
30
    protected DateTimeZone $timezone;
31
 
1 efrain 32
    /**
33
     * Create a new instance of the incrementing clock.
34
     *
35
     * @param null|int $starttime The initial time to use. If not specified, the current time is used.
36
     */
37
    public function __construct(
38
        ?int $starttime = null,
39
    ) {
40
        $this->time = $starttime ?? time();
1441 ariadna 41
        $this->timezone = \core_date::get_server_timezone_object();
1 efrain 42
    }
43
 
44
    public function now(): \DateTimeImmutable {
1441 ariadna 45
        return (new \DateTimeImmutable('@' . $this->time++))->setTimezone($this->timezone);
1 efrain 46
    }
47
 
48
    public function time(): int {
49
        return $this->now()->getTimestamp();
50
    }
51
 
52
    /**
53
     * Set the time of the clock.
54
     *
55
     * @param int $time
56
     */
57
    public function set_to(int $time): void {
58
        $this->time = $time;
59
    }
60
 
61
    /**
62
     * Bump the time by a number of seconds.
63
     *
64
     * Note: The act of fetching the time will also bump the time by one second.
65
     *
66
     * @param int $seconds
67
     */
68
    public function bump(int $seconds = 1): void {
69
        $this->time += $seconds;
70
    }
71
}