Proyectos de Subversion Moodle

Rev

| 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
 * Frozen 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 \DateTimeImmutable $time The current time of the clock
24
 */
25
class frozen_clock implements \core\clock {
26
    /** @var DateTimeImmutable The next time of the clock */
27
    public DateTimeImmutable $time;
28
 
29
    /**
30
     * Create a new instance of the frozen clock.
31
     *
32
     * @param null|int $time The initial time to use. If not specified, the current time is used.
33
     */
34
    public function __construct(
35
        ?int $time = null,
36
    ) {
37
        if ($time) {
38
            $this->time = new \DateTimeImmutable("@{$time}");
39
        } else {
40
            $this->time = new \DateTimeImmutable();
41
        }
42
    }
43
 
44
    public function now(): \DateTimeImmutable {
45
        return $this->time;
46
    }
47
 
48
    public function time(): int {
49
        return $this->time->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 = new \DateTimeImmutable("@{$time}");
59
    }
60
 
61
    /**
62
     * Bump the time by a number of seconds.
63
     *
64
     * @param int $seconds
65
     */
66
    public function bump(int $seconds = 1): void {
67
        $this->time = $this->time->modify("+{$seconds} seconds");
68
    }
69
}