1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright (c) 2022, Joas Schilling <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
8
|
|
|
* |
9
|
|
|
* @author Bernhard Posselt <[email protected]> |
10
|
|
|
* @author Joas Schilling <[email protected]> |
11
|
|
|
* @author Morris Jobke <[email protected]> |
12
|
|
|
* @author Thomas Müller <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @license AGPL-3.0 |
15
|
|
|
* |
16
|
|
|
* This code is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
18
|
|
|
* as published by the Free Software Foundation. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU Affero General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
namespace OC\AppFramework\Utility; |
30
|
|
|
|
31
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Use this to get a timestamp or DateTime object in code to remain testable |
35
|
|
|
* |
36
|
|
|
* @since 8.0.0 |
37
|
|
|
* @since 26.0.0 Extends the \Psr\Clock\ClockInterface interface |
38
|
|
|
* @ref https://www.php-fig.org/psr/psr-20/#21-clockinterface |
39
|
|
|
*/ |
40
|
|
|
class TimeFactory implements ITimeFactory { |
41
|
|
|
protected \DateTimeZone $timezone; |
42
|
|
|
|
43
|
|
|
public function __construct() { |
44
|
|
|
$this->timezone = new \DateTimeZone('UTC'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int the result of a call to time() |
49
|
|
|
* @since 8.0.0 |
50
|
|
|
* @deprecated 26.0.0 {@see ITimeFactory::now()} |
51
|
|
|
*/ |
52
|
|
|
public function getTime(): int { |
53
|
|
|
return time(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $time |
58
|
|
|
* @param \DateTimeZone $timezone |
59
|
|
|
* @return \DateTime |
60
|
|
|
* @since 15.0.0 |
61
|
|
|
* @deprecated 26.0.0 {@see ITimeFactory::now()} |
62
|
|
|
*/ |
63
|
|
|
public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null): \DateTime { |
64
|
|
|
return new \DateTime($time, $timezone); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function now(): \DateTimeImmutable { |
68
|
|
|
return new \DateTimeImmutable('now', $this->timezone); |
69
|
|
|
} |
70
|
|
|
public function withTimeZone(\DateTimeZone $timezone): static { |
71
|
|
|
$clone = clone $this; |
72
|
|
|
$clone->timezone = $timezone; |
73
|
|
|
|
74
|
|
|
return $clone; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|