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