xref: /webtrees/app/Timestamp.php (revision ee551e0b6d330340f2c6e961359bd855b89571fb)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use Carbon\Carbon;
23use Fisharebest\Webtrees\Contracts\TimestampInterface;
24
25/**
26 * A localized date-time.
27 */
28class Timestamp implements TimestampInterface
29{
30    private Carbon $carbon;
31
32    /**
33     * @param int    $timestamp
34     * @param string $timezone
35     * @param string $locale
36     */
37    public function __construct(int $timestamp, string $timezone, string $locale)
38    {
39        $this->carbon = Carbon::createFromTimestamp($timestamp, $timezone);
40        $this->carbon->locale($locale);
41    }
42
43    public function __clone()
44    {
45        $this->carbon = clone($this->carbon);
46    }
47
48    /**
49     * Convert a datetime to the user's Julian day number.
50     *
51     * @return int
52     */
53    public function julianDay(): int
54    {
55        return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year);
56    }
57
58    /**
59     * @return string
60     */
61    public function diffForHumans(): string
62    {
63        return $this->carbon->diffForHumans();
64    }
65
66    /**
67     * @param string $format
68     *
69     * @return string
70     */
71    public function format(string $format): string
72    {
73        return $this->carbon->format($format);
74    }
75
76    /**
77     * @param string $format
78     *
79     * @return string
80     */
81    public function isoFormat(string $format): string
82    {
83        return $this->carbon->isoFormat($format);
84    }
85
86    /**
87     * @return string
88     */
89    public function toDateString(): string
90    {
91        return $this->carbon->format('Y-m-d');
92    }
93
94    /**
95     * @return string
96     */
97    public function toDateTimeString(): string
98    {
99        return $this->carbon->format('Y-m-d H:i:s');
100    }
101
102    /**
103     * @param TimestampInterface $datetime
104     *
105     * @return int
106     */
107    public function compare(TimestampInterface $datetime): int
108    {
109        if ($this->carbon->lt($datetime->carbon)) {
110            return -1;
111        }
112
113        if ($this->carbon->gt($datetime->carbon)) {
114            return 1;
115        }
116
117        return 0;
118    }
119
120    /**
121     * @param int $seconds
122     *
123     * @return self
124     */
125    public function addSeconds(int $seconds): TimestampInterface
126    {
127        $clone = clone($this);
128
129        $clone->carbon->addSeconds($seconds);
130
131        return $clone;
132    }
133
134    /**
135     * @param int $minutes
136     *
137     * @return self
138     */
139    public function addMinutes(int $minutes): TimestampInterface
140    {
141        $clone = clone($this);
142
143        $clone->carbon->addMinutes($minutes);
144
145        return $this;
146    }
147
148    /**
149     * @param int $hours
150     *
151     * @return self
152     */
153    public function addHours(int $hours): TimestampInterface
154    {
155        $clone = clone($this);
156
157        $clone->carbon->addHours($hours);
158
159        return $clone;
160    }
161
162    /**
163     * @param int $days
164     *
165     * @return self
166     */
167    public function addDays(int $days): TimestampInterface
168    {
169        $clone = clone($this);
170
171        $clone->carbon->addDays($days);
172
173        return $clone;
174    }
175
176    /**
177     * Add to the month portion of the date.
178     *
179     * Allows overflow, consistent with v2.1.0 ... 2023-10-31 plus 1 month = 2023-12-01.
180     *
181     * @param int $months
182     *
183     * @return self
184     */
185    public function addMonths(int $months): TimestampInterface
186    {
187        $clone = clone($this);
188
189        $clone->carbon->addMonths($months);
190
191        return $clone;
192    }
193
194    /**
195     * Add to the year portion of the date.
196     *
197     * Allows overflow, consistent with v2.1.0 ... 2024-02-29 plus 1 year = 2025-03-01.
198     *
199     * @param int $years
200     *
201     * @return self
202     */
203    public function addYears(int $years): TimestampInterface
204    {
205        $clone = clone($this);
206
207        $clone->carbon->addYears($years);
208
209        return $clone;
210    }
211
212    /**
213     * @param int $seconds
214     *
215     * @return self
216     */
217    public function subtractSeconds(int $seconds): TimestampInterface
218    {
219        $clone = clone($this);
220
221        $clone->carbon->subSeconds($seconds);
222
223        return $clone;
224    }
225
226    /**
227     * @param int $minutes
228     *
229     * @return self
230     */
231    public function subtractMinutes(int $minutes): TimestampInterface
232    {
233        $clone = clone($this);
234
235        $clone->carbon->subMinutes($minutes);
236
237        return $this;
238    }
239
240    /**
241     * @param int $hours
242     *
243     * @return self
244     */
245    public function subtractHours(int $hours): TimestampInterface
246    {
247        $clone = clone($this);
248
249        $clone->carbon->subHours($hours);
250
251        return $clone;
252    }
253
254    /**
255     * @param int $days
256     *
257     * @return self
258     */
259    public function subtractDays(int $days): TimestampInterface
260    {
261        $clone = clone($this);
262
263        $clone->carbon->subDays($days);
264
265        return $clone;
266    }
267
268    /**
269     * Subtract from the month portion of the date.
270     *
271     * Allows overflow, consistent with v2.1.0 ... 2023-10-31 minus 1 month = 2023-10-01.
272     *
273     * @param int $months
274     *
275     * @return self
276     */
277    public function subtractMonths(int $months): TimestampInterface
278    {
279        $clone = clone($this);
280
281        $clone->carbon->subMonths($months);
282
283        return $clone;
284    }
285
286    /**
287     * Subtract from the year portion of the date.
288     *
289     * Allows overflow, consistent with v2.1.0 ... 2024-02-29 minus 1 year = 2023-03-01.
290     *
291     * @param int $years
292     *
293     * @return self
294     */
295    public function subtractYears(int $years): TimestampInterface
296    {
297        $clone = clone($this);
298
299        $clone->carbon->subYears($years);
300
301        return $clone;
302    }
303
304    /**
305     * @return int
306     */
307    public function timestamp(): int
308    {
309        return $this->carbon->getTimestamp();
310    }
311}
312