1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2023 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\Factories; |
21
|
|
|
|
22
|
|
|
use DateTimeZone; |
23
|
|
|
use Fisharebest\Webtrees\Auth; |
24
|
|
|
use Fisharebest\Webtrees\Contracts\TimestampFactoryInterface; |
25
|
|
|
use Fisharebest\Webtrees\Contracts\TimestampInterface; |
26
|
|
|
use Fisharebest\Webtrees\Contracts\UserInterface; |
27
|
|
|
use Fisharebest\Webtrees\I18N; |
28
|
|
|
use Fisharebest\Webtrees\Site; |
29
|
|
|
use Fisharebest\Webtrees\Timestamp; |
30
|
|
|
use InvalidArgumentException; |
31
|
|
|
|
32
|
|
|
use function date; |
33
|
|
|
use function date_create_from_format; |
34
|
|
|
use function time; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a timestamp object. |
38
|
|
|
*/ |
39
|
|
|
class TimestampFactory implements TimestampFactoryInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @param int $timestamp |
43
|
|
|
* @param UserInterface|null $user |
44
|
|
|
* |
45
|
|
|
* @return TimestampInterface |
46
|
|
|
*/ |
47
|
|
|
public function make(int $timestamp, UserInterface $user = null): TimestampInterface |
48
|
|
|
{ |
49
|
|
|
$user ??= Auth::user(); |
50
|
|
|
$timezone = $user->getPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE')); |
51
|
|
|
$locale = I18N::locale()->code(); |
52
|
|
|
|
53
|
|
|
return new Timestamp($timestamp, $timezone, $locale); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Constructs a Timestamp using UTC input only. |
58
|
|
|
* |
59
|
|
|
* @param string|null $string YYYY-MM-DD HH:MM:SS (as provided by SQL). |
60
|
|
|
* @param string $format |
61
|
|
|
* @param UserInterface|null $user |
62
|
|
|
* |
63
|
|
|
* @return TimestampInterface |
64
|
|
|
*/ |
65
|
|
|
public function fromString(?string $string, string $format = 'Y-m-d H:i:s', UserInterface $user = null): TimestampInterface |
66
|
|
|
{ |
67
|
|
|
$string ??= date($format); |
68
|
|
|
$utc = new DateTimeZone('UTC'); |
69
|
|
|
|
70
|
|
|
return $this->fromZoneString($utc, $string, $format, $user); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructs a Timestamp using local datetime input according to user's timezone setting. |
75
|
|
|
* |
76
|
|
|
* @param string $string YYYY-MM-DD HH:MM:SS (as provided by SQL). |
77
|
|
|
* @param string $format |
78
|
|
|
* @param UserInterface|null $user |
79
|
|
|
* |
80
|
|
|
* @return TimestampInterface |
81
|
|
|
*/ |
82
|
|
|
public function fromLocalString(string $string, string $format = 'Y-m-d H:i:s', UserInterface $user = null): TimestampInterface |
83
|
|
|
{ |
84
|
|
|
$user ??= Auth::user(); |
85
|
|
|
$timezone = new DateTimeZone($user->getPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'))); |
86
|
|
|
|
87
|
|
|
return $this->fromZoneString($timezone, $string, $format, $user); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Constructs a Timestamp using local datetime input according to the specified timezone. |
92
|
|
|
* |
93
|
|
|
* @param DateTimeZone $timezone |
94
|
|
|
* @param string $string YYYY-MM-DD HH:MM:SS (as provided by SQL). |
95
|
|
|
* @param string $format |
96
|
|
|
* @param UserInterface|null $user |
97
|
|
|
* |
98
|
|
|
* @return TimestampInterface |
99
|
|
|
*/ |
100
|
|
|
public function fromZoneString(DateTimeZone $timezone, string $string, string $format, UserInterface $user = null): TimestampInterface |
101
|
|
|
{ |
102
|
|
|
$datetime = date_create_from_format($format, $string, $timezone); |
103
|
|
|
|
104
|
|
|
if ($datetime === false) { |
105
|
|
|
throw new InvalidArgumentException('date/time "' . $string . '" does not match pattern "' . $format . '"'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->make($datetime->getTimestamp(), $user); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param UserInterface|null $user |
113
|
|
|
* |
114
|
|
|
* @return TimestampInterface |
115
|
|
|
*/ |
116
|
|
|
public function now(UserInterface $user = null): TimestampInterface |
117
|
|
|
{ |
118
|
|
|
return $this->make(time(), $user); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|