xref: /webtrees/app/Timestamp.php (revision 0a011512f43fd01c11d93af156778968f114afb9)
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;
238e989043SGreg Roachuse Carbon\CarbonImmutable;
24d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface;
25d97083feSGreg Roach
268e989043SGreg Roachuse function gregoriantojd;
278e989043SGreg Roach
28d97083feSGreg Roach/**
29d97083feSGreg Roach * A localized date-time.
30d97083feSGreg Roach */
31d97083feSGreg Roachclass Timestamp implements TimestampInterface
32d97083feSGreg Roach{
338e989043SGreg Roach    private CarbonImmutable $carbon;
34d97083feSGreg Roach
35d97083feSGreg Roach    public function __construct(int $timestamp, string $timezone, string $locale)
36d97083feSGreg Roach    {
378e989043SGreg Roach        $this->carbon = CarbonImmutable::createFromTimestamp($timestamp, $timezone);
38d97083feSGreg Roach        $this->carbon->locale($locale);
39d97083feSGreg Roach    }
40d97083feSGreg Roach
41ee551e0bSGreg Roach    public function __clone()
42ee551e0bSGreg Roach    {
43ee551e0bSGreg Roach        $this->carbon = clone($this->carbon);
44ee551e0bSGreg Roach    }
45ee551e0bSGreg Roach
46d97083feSGreg Roach    public function julianDay(): int
47d97083feSGreg Roach    {
48d97083feSGreg Roach        return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year);
49d97083feSGreg Roach    }
50d97083feSGreg Roach
51d97083feSGreg Roach    public function diffForHumans(): string
52d97083feSGreg Roach    {
53d97083feSGreg Roach        return $this->carbon->diffForHumans();
54d97083feSGreg Roach    }
55d97083feSGreg Roach
56d97083feSGreg Roach    public function format(string $format): string
57d97083feSGreg Roach    {
58d97083feSGreg Roach        return $this->carbon->format($format);
59d97083feSGreg Roach    }
60d97083feSGreg Roach
61d97083feSGreg Roach    public function isoFormat(string $format): string
62d97083feSGreg Roach    {
63d97083feSGreg Roach        return $this->carbon->isoFormat($format);
64d97083feSGreg Roach    }
65d97083feSGreg Roach
66d97083feSGreg Roach    public function toDateString(): string
67d97083feSGreg Roach    {
68d97083feSGreg Roach        return $this->carbon->format('Y-m-d');
69d97083feSGreg Roach    }
70d97083feSGreg Roach
71d97083feSGreg Roach    public function toDateTimeString(): string
72d97083feSGreg Roach    {
73d97083feSGreg Roach        return $this->carbon->format('Y-m-d H:i:s');
74d97083feSGreg Roach    }
75d97083feSGreg Roach
768e989043SGreg Roach    public function compare(TimestampInterface $timestamp): int
77d97083feSGreg Roach    {
788e989043SGreg Roach        return $this->timestamp() <=> $timestamp->timestamp();
79d97083feSGreg Roach    }
80d97083feSGreg Roach
817761cf05SGreg Roach    public function addSeconds(int $seconds): TimestampInterface
82d97083feSGreg Roach    {
838e989043SGreg Roach        return new self(
848e989043SGreg Roach            $this->carbon->addSeconds($seconds)->getTimestamp(),
858e989043SGreg Roach            $this->carbon->timezone->getName(),
86*0a011512SGreg Roach            $this->carbon->locale
87*0a011512SGreg Roach        );
88d97083feSGreg Roach    }
89d97083feSGreg Roach
907761cf05SGreg Roach    public function addMinutes(int $minutes): TimestampInterface
91d97083feSGreg Roach    {
928e989043SGreg Roach        return new self(
938e989043SGreg Roach            $this->carbon->addMinutes($minutes)->getTimestamp(),
948e989043SGreg Roach            $this->carbon->timezone->getName(),
95*0a011512SGreg Roach            $this->carbon->locale
96*0a011512SGreg Roach        );
97d97083feSGreg Roach    }
98d97083feSGreg Roach
997761cf05SGreg Roach    public function addHours(int $hours): TimestampInterface
100d97083feSGreg Roach    {
1018e989043SGreg Roach        return new self(
1028e989043SGreg Roach            $this->carbon->addHours($hours)->getTimestamp(),
1038e989043SGreg Roach            $this->carbon->timezone->getName(),
104*0a011512SGreg Roach            $this->carbon->locale
105*0a011512SGreg Roach        );
106d97083feSGreg Roach    }
107d97083feSGreg Roach
1087761cf05SGreg Roach    public function addDays(int $days): TimestampInterface
109d97083feSGreg Roach    {
1108e989043SGreg Roach        return new self(
1118e989043SGreg Roach            $this->carbon->addDays($days)->getTimestamp(),
1128e989043SGreg Roach            $this->carbon->timezone->getName(),
113*0a011512SGreg Roach            $this->carbon->locale
114*0a011512SGreg Roach        );
115d97083feSGreg Roach    }
116d97083feSGreg Roach
1177761cf05SGreg Roach    public function addMonths(int $months): TimestampInterface
118d97083feSGreg Roach    {
1198e989043SGreg Roach        return new self(
1208e989043SGreg Roach            $this->carbon->addMonth($months)->getTimestamp(),
1218e989043SGreg Roach            $this->carbon->timezone->getName(),
122*0a011512SGreg Roach            $this->carbon->locale
123*0a011512SGreg Roach        );
124d97083feSGreg Roach    }
125d97083feSGreg Roach
1267761cf05SGreg Roach    public function addYears(int $years): TimestampInterface
127d97083feSGreg Roach    {
1288e989043SGreg Roach        return new self(
1298e989043SGreg Roach            $this->carbon->addYears($years)->getTimestamp(),
1308e989043SGreg Roach            $this->carbon->timezone->getName(),
131*0a011512SGreg Roach            $this->carbon->locale
132*0a011512SGreg Roach        );
133d97083feSGreg Roach    }
134d97083feSGreg Roach
1357761cf05SGreg Roach    public function subtractSeconds(int $seconds): TimestampInterface
136d97083feSGreg Roach    {
1378e989043SGreg Roach        return new self(
1388e989043SGreg Roach            $this->carbon->subSeconds($seconds)->getTimestamp(),
1398e989043SGreg Roach            $this->carbon->timezone->getName(),
140*0a011512SGreg Roach            $this->carbon->locale
141*0a011512SGreg Roach        );
142d97083feSGreg Roach    }
143d97083feSGreg Roach
1447761cf05SGreg Roach    public function subtractMinutes(int $minutes): TimestampInterface
145d97083feSGreg Roach    {
1468e989043SGreg Roach        return new self(
1478e989043SGreg Roach            $this->carbon->subMinutes($minutes)->getTimestamp(),
1488e989043SGreg Roach            $this->carbon->timezone->getName(),
149*0a011512SGreg Roach            $this->carbon->locale
150*0a011512SGreg Roach        );
151d97083feSGreg Roach    }
152d97083feSGreg Roach
1537761cf05SGreg Roach    public function subtractHours(int $hours): TimestampInterface
154d97083feSGreg Roach    {
1558e989043SGreg Roach        return new self(
1568e989043SGreg Roach            $this->carbon->subHours($hours)->getTimestamp(),
1578e989043SGreg Roach            $this->carbon->timezone->getName(),
158*0a011512SGreg Roach            $this->carbon->locale
159*0a011512SGreg Roach        );
160d97083feSGreg Roach    }
161d97083feSGreg Roach
1627761cf05SGreg Roach    public function subtractDays(int $days): TimestampInterface
163d97083feSGreg Roach    {
1648e989043SGreg Roach        return new self(
1658e989043SGreg Roach            $this->carbon->subDays($days)->getTimestamp(),
1668e989043SGreg Roach            $this->carbon->timezone->getName(),
167*0a011512SGreg Roach            $this->carbon->locale
168*0a011512SGreg Roach        );
169d97083feSGreg Roach    }
170d97083feSGreg Roach
1717761cf05SGreg Roach    public function subtractMonths(int $months): TimestampInterface
172d97083feSGreg Roach    {
1738e989043SGreg Roach        return new self(
1748e989043SGreg Roach            $this->carbon->subMonths($months)->getTimestamp(),
1758e989043SGreg Roach            $this->carbon->timezone->getName(),
176*0a011512SGreg Roach            $this->carbon->locale
177*0a011512SGreg Roach        );
178d97083feSGreg Roach    }
179d97083feSGreg Roach
1807761cf05SGreg Roach    public function subtractYears(int $years): TimestampInterface
181d97083feSGreg Roach    {
1828e989043SGreg Roach        return new self(
1838e989043SGreg Roach            $this->carbon->subYears($years)->getTimestamp(),
1848e989043SGreg Roach            $this->carbon->timezone->getName(),
185*0a011512SGreg Roach            $this->carbon->locale
186*0a011512SGreg Roach        );
187d97083feSGreg Roach    }
188d97083feSGreg Roach
189d97083feSGreg Roach    public function timestamp(): int
190d97083feSGreg Roach    {
191d97083feSGreg Roach        return $this->carbon->getTimestamp();
192d97083feSGreg Roach    }
193d97083feSGreg Roach}
194