xref: /webtrees/app/Timestamp.php (revision 7796152954434c105cb4c6abb89f1c5511dc53f3)
1d97083feSGreg Roach<?php
2d97083feSGreg Roach
3d97083feSGreg Roach/**
4d97083feSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6d97083feSGreg Roach * This program is free software: you can redistribute it and/or modify
7d97083feSGreg Roach * it under the terms of the GNU General Public License as published by
8d97083feSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9d97083feSGreg Roach * (at your option) any later version.
10d97083feSGreg Roach * This program is distributed in the hope that it will be useful,
11d97083feSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12d97083feSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13d97083feSGreg Roach * GNU General Public License for more details.
14d97083feSGreg Roach * You should have received a copy of the GNU General Public License
15d97083feSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16d97083feSGreg Roach */
17d97083feSGreg Roach
18d97083feSGreg Roachdeclare(strict_types=1);
19d97083feSGreg Roach
20d97083feSGreg Roachnamespace Fisharebest\Webtrees;
21d97083feSGreg Roach
22d97083feSGreg Roachuse Carbon\Carbon;
23d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface;
24d97083feSGreg Roach
25d97083feSGreg Roach/**
26d97083feSGreg Roach * A localized date-time.
27d97083feSGreg Roach */
28d97083feSGreg Roachclass Timestamp implements TimestampInterface
29d97083feSGreg Roach{
30d97083feSGreg Roach    private Carbon $carbon;
31d97083feSGreg Roach
32d97083feSGreg Roach    /**
33d97083feSGreg Roach     * @param int    $timestamp
34d97083feSGreg Roach     * @param string $timezone
35d97083feSGreg Roach     * @param string $locale
36d97083feSGreg Roach     */
37d97083feSGreg Roach    public function __construct(int $timestamp, string $timezone, string $locale)
38d97083feSGreg Roach    {
39d97083feSGreg Roach        $this->carbon = Carbon::createFromTimestamp($timestamp, $timezone);
40d97083feSGreg Roach        $this->carbon->locale($locale);
41d97083feSGreg Roach    }
42d97083feSGreg Roach
43d97083feSGreg Roach    /**
44d97083feSGreg Roach     * Convert a datetime to the user's Julian day number.
45d97083feSGreg Roach     *
46d97083feSGreg Roach     * @return int
47d97083feSGreg Roach     */
48d97083feSGreg Roach    public function julianDay(): int
49d97083feSGreg Roach    {
50d97083feSGreg Roach        return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year);
51d97083feSGreg Roach    }
52d97083feSGreg Roach
53d97083feSGreg Roach    /**
54d97083feSGreg Roach     * @return string
55d97083feSGreg Roach     */
56d97083feSGreg Roach    public function diffForHumans(): string
57d97083feSGreg Roach    {
58d97083feSGreg Roach        return $this->carbon->diffForHumans();
59d97083feSGreg Roach    }
60d97083feSGreg Roach
61d97083feSGreg Roach    /**
62d97083feSGreg Roach     * @param string $format
63d97083feSGreg Roach     *
64d97083feSGreg Roach     * @return string
65d97083feSGreg Roach     */
66d97083feSGreg Roach    public function format(string $format): string
67d97083feSGreg Roach    {
68d97083feSGreg Roach        return $this->carbon->format($format);
69d97083feSGreg Roach    }
70d97083feSGreg Roach
71d97083feSGreg Roach    /**
72d97083feSGreg Roach     * @param string $format
73d97083feSGreg Roach     *
74d97083feSGreg Roach     * @return string
75d97083feSGreg Roach     */
76d97083feSGreg Roach    public function isoFormat(string $format): string
77d97083feSGreg Roach    {
78d97083feSGreg Roach        return $this->carbon->isoFormat($format);
79d97083feSGreg Roach    }
80d97083feSGreg Roach
81d97083feSGreg Roach    /**
82d97083feSGreg Roach     * @return string
83d97083feSGreg Roach     */
84d97083feSGreg Roach    public function toDateString(): string
85d97083feSGreg Roach    {
86d97083feSGreg Roach        return $this->carbon->format('Y-m-d');
87d97083feSGreg Roach    }
88d97083feSGreg Roach
89d97083feSGreg Roach    /**
90d97083feSGreg Roach     * @return string
91d97083feSGreg Roach     */
92d97083feSGreg Roach    public function toDateTimeString(): string
93d97083feSGreg Roach    {
94d97083feSGreg Roach        return $this->carbon->format('Y-m-d H:i:s');
95d97083feSGreg Roach    }
96d97083feSGreg Roach
97d97083feSGreg Roach    /**
98d97083feSGreg Roach     * @param TimestampInterface $datetime
99d97083feSGreg Roach     *
100d97083feSGreg Roach     * @return int
101d97083feSGreg Roach     */
102d97083feSGreg Roach    public function compare(TimestampInterface $datetime): int
103d97083feSGreg Roach    {
104d97083feSGreg Roach        if ($this->carbon->lt($datetime)) {
105d97083feSGreg Roach            return -1;
106d97083feSGreg Roach        }
107d97083feSGreg Roach
108d97083feSGreg Roach        if ($this->carbon->gt($datetime)) {
109d97083feSGreg Roach            return 1;
110d97083feSGreg Roach        }
111d97083feSGreg Roach
112d97083feSGreg Roach        return 0;
113d97083feSGreg Roach    }
114d97083feSGreg Roach
115d97083feSGreg Roach    /**
116d97083feSGreg Roach     * @param int $seconds
117d97083feSGreg Roach     *
1186612c384SGreg Roach     * @return self
119d97083feSGreg Roach     */
1207761cf05SGreg Roach    public function addSeconds(int $seconds): TimestampInterface
121d97083feSGreg Roach    {
122d97083feSGreg Roach        $clone = clone($this);
123d97083feSGreg Roach
124*77961529Smiqrogroove        $clone->carbon->addSeconds($seconds);
125d97083feSGreg Roach
126d97083feSGreg Roach        return $clone;
127d97083feSGreg Roach    }
128d97083feSGreg Roach
129d97083feSGreg Roach    /**
130d97083feSGreg Roach     * @param int $minutes
131d97083feSGreg Roach     *
1326612c384SGreg Roach     * @return self
133d97083feSGreg Roach     */
1347761cf05SGreg Roach    public function addMinutes(int $minutes): TimestampInterface
135d97083feSGreg Roach    {
136d97083feSGreg Roach        $clone = clone($this);
137d97083feSGreg Roach
138*77961529Smiqrogroove        $clone->carbon->addMinutes($minutes);
139d97083feSGreg Roach
140d97083feSGreg Roach        return $this;
141d97083feSGreg Roach    }
142d97083feSGreg Roach
143d97083feSGreg Roach    /**
144d97083feSGreg Roach     * @param int $hours
145d97083feSGreg Roach     *
1466612c384SGreg Roach     * @return self
147d97083feSGreg Roach     */
1487761cf05SGreg Roach    public function addHours(int $hours): TimestampInterface
149d97083feSGreg Roach    {
150d97083feSGreg Roach        $clone = clone($this);
151d97083feSGreg Roach
152*77961529Smiqrogroove        $clone->carbon->addHours($hours);
153d97083feSGreg Roach
154d97083feSGreg Roach        return $clone;
155d97083feSGreg Roach    }
156d97083feSGreg Roach
157d97083feSGreg Roach    /**
158d97083feSGreg Roach     * @param int $days
159d97083feSGreg Roach     *
1606612c384SGreg Roach     * @return self
161d97083feSGreg Roach     */
1627761cf05SGreg Roach    public function addDays(int $days): TimestampInterface
163d97083feSGreg Roach    {
164d97083feSGreg Roach        $clone = clone($this);
165d97083feSGreg Roach
166*77961529Smiqrogroove        $clone->carbon->addDays($days);
167d97083feSGreg Roach
168d97083feSGreg Roach        return $clone;
169d97083feSGreg Roach    }
170d97083feSGreg Roach
171d97083feSGreg Roach    /**
172*77961529Smiqrogroove     * Add to the month portion of the date.
173*77961529Smiqrogroove     *
174*77961529Smiqrogroove     * Allows overflow, consistent with v2.1.0 ... 2023-10-31 plus 1 month = 2023-12-01.
175*77961529Smiqrogroove     *
176d97083feSGreg Roach     * @param int $months
177d97083feSGreg Roach     *
1786612c384SGreg Roach     * @return self
179d97083feSGreg Roach     */
1807761cf05SGreg Roach    public function addMonths(int $months): TimestampInterface
181d97083feSGreg Roach    {
182d97083feSGreg Roach        $clone = clone($this);
183d97083feSGreg Roach
184*77961529Smiqrogroove        $clone->carbon->addMonths($months);
185d97083feSGreg Roach
186d97083feSGreg Roach        return $clone;
187d97083feSGreg Roach    }
188d97083feSGreg Roach
189d97083feSGreg Roach    /**
190*77961529Smiqrogroove     * Add to the year portion of the date.
191*77961529Smiqrogroove     *
192*77961529Smiqrogroove     * Allows overflow, consistent with v2.1.0 ... 2024-02-29 plus 1 year = 2025-03-01.
193*77961529Smiqrogroove     *
194d97083feSGreg Roach     * @param int $years
195d97083feSGreg Roach     *
1966612c384SGreg Roach     * @return self
197d97083feSGreg Roach     */
1987761cf05SGreg Roach    public function addYears(int $years): TimestampInterface
199d97083feSGreg Roach    {
200d97083feSGreg Roach        $clone = clone($this);
201d97083feSGreg Roach
202*77961529Smiqrogroove        $clone->carbon->addYears($years);
203d97083feSGreg Roach
204d97083feSGreg Roach        return $clone;
205d97083feSGreg Roach    }
206d97083feSGreg Roach
207d97083feSGreg Roach    /**
208d97083feSGreg Roach     * @param int $seconds
209d97083feSGreg Roach     *
2106612c384SGreg Roach     * @return self
211d97083feSGreg Roach     */
2127761cf05SGreg Roach    public function subtractSeconds(int $seconds): TimestampInterface
213d97083feSGreg Roach    {
214d97083feSGreg Roach        $clone = clone($this);
215d97083feSGreg Roach
216*77961529Smiqrogroove        $clone->carbon->subSeconds($seconds);
217d97083feSGreg Roach
218d97083feSGreg Roach        return $clone;
219d97083feSGreg Roach    }
220d97083feSGreg Roach
221d97083feSGreg Roach    /**
222d97083feSGreg Roach     * @param int $minutes
223d97083feSGreg Roach     *
2246612c384SGreg Roach     * @return self
225d97083feSGreg Roach     */
2267761cf05SGreg Roach    public function subtractMinutes(int $minutes): TimestampInterface
227d97083feSGreg Roach    {
228d97083feSGreg Roach        $clone = clone($this);
229d97083feSGreg Roach
230*77961529Smiqrogroove        $clone->carbon->subMinutes($minutes);
231d97083feSGreg Roach
232*77961529Smiqrogroove        return $this;
233d97083feSGreg Roach    }
234d97083feSGreg Roach
235d97083feSGreg Roach    /**
236d97083feSGreg Roach     * @param int $hours
237d97083feSGreg Roach     *
2386612c384SGreg Roach     * @return self
239d97083feSGreg Roach     */
2407761cf05SGreg Roach    public function subtractHours(int $hours): TimestampInterface
241d97083feSGreg Roach    {
242d97083feSGreg Roach        $clone = clone($this);
243d97083feSGreg Roach
244*77961529Smiqrogroove        $clone->carbon->subHours($hours);
245d97083feSGreg Roach
246d97083feSGreg Roach        return $clone;
247d97083feSGreg Roach    }
248d97083feSGreg Roach
249d97083feSGreg Roach    /**
250d97083feSGreg Roach     * @param int $days
251d97083feSGreg Roach     *
2526612c384SGreg Roach     * @return self
253d97083feSGreg Roach     */
2547761cf05SGreg Roach    public function subtractDays(int $days): TimestampInterface
255d97083feSGreg Roach    {
256d97083feSGreg Roach        $clone = clone($this);
257d97083feSGreg Roach
258*77961529Smiqrogroove        $clone->carbon->subDays($days);
259d97083feSGreg Roach
260d97083feSGreg Roach        return $clone;
261d97083feSGreg Roach    }
262d97083feSGreg Roach
263d97083feSGreg Roach    /**
264*77961529Smiqrogroove     * Subtract from the month portion of the date.
265*77961529Smiqrogroove     *
266*77961529Smiqrogroove     * Allows overflow, consistent with v2.1.0 ... 2023-10-31 minus 1 month = 2023-10-01.
267*77961529Smiqrogroove     *
268d97083feSGreg Roach     * @param int $months
269d97083feSGreg Roach     *
2706612c384SGreg Roach     * @return self
271d97083feSGreg Roach     */
2727761cf05SGreg Roach    public function subtractMonths(int $months): TimestampInterface
273d97083feSGreg Roach    {
274d97083feSGreg Roach        $clone = clone($this);
275d97083feSGreg Roach
276*77961529Smiqrogroove        $clone->carbon->subMonths($months);
277d97083feSGreg Roach
278d97083feSGreg Roach        return $clone;
279d97083feSGreg Roach    }
280d97083feSGreg Roach
281d97083feSGreg Roach    /**
282*77961529Smiqrogroove     * Subtract from the year portion of the date.
283*77961529Smiqrogroove     *
284*77961529Smiqrogroove     * Allows overflow, consistent with v2.1.0 ... 2024-02-29 minus 1 year = 2023-03-01.
285*77961529Smiqrogroove     *
286d97083feSGreg Roach     * @param int $years
287d97083feSGreg Roach     *
2886612c384SGreg Roach     * @return self
289d97083feSGreg Roach     */
2907761cf05SGreg Roach    public function subtractYears(int $years): TimestampInterface
291d97083feSGreg Roach    {
292d97083feSGreg Roach        $clone = clone($this);
293d97083feSGreg Roach
294*77961529Smiqrogroove        $clone->carbon->subYears($years);
295d97083feSGreg Roach
296d97083feSGreg Roach        return $clone;
297d97083feSGreg Roach    }
298d97083feSGreg Roach
299d97083feSGreg Roach    /**
300d97083feSGreg Roach     * @return int
301d97083feSGreg Roach     */
302d97083feSGreg Roach    public function timestamp(): int
303d97083feSGreg Roach    {
304d97083feSGreg Roach        return $this->carbon->getTimestamp();
305d97083feSGreg Roach    }
306d97083feSGreg Roach}
307