summaryrefslogtreecommitdiff
path: root/app/Timestamp.php
blob: ae7c1b5b1ffeac315b56e8071047dd898441621b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2026 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees;

use Carbon\CarbonImmutable;
use Fisharebest\Webtrees\Contracts\TimestampInterface;

use function GregorianToJD;

/**
 * A localized date-time.
 */
class Timestamp implements TimestampInterface
{
    private CarbonImmutable $carbon;

    public function __construct(int $timestamp, string $timezone, string $locale)
    {
        $this->carbon = CarbonImmutable::createFromTimestamp($timestamp, $timezone);
        $this->carbon->locale($locale);
    }

    public function __clone()
    {
        $this->carbon = clone($this->carbon);
    }

    public function julianDay(): int
    {
        return GregorianToJD($this->carbon->month, $this->carbon->day, $this->carbon->year);
    }

    public function diffForHumans(): string
    {
        return $this->carbon->diffForHumans();
    }

    public function format(string $format): string
    {
        return $this->carbon->format($format);
    }

    public function isoFormat(string $format): string
    {
        return $this->carbon->isoFormat($format);
    }

    public function toDateString(): string
    {
        return $this->carbon->format('Y-m-d');
    }

    public function toDateTimeString(): string
    {
        return $this->carbon->format('Y-m-d H:i:s');
    }

    public function compare(TimestampInterface $timestamp): int
    {
        return $this->timestamp() <=> $timestamp->timestamp();
    }

    public function addSeconds(int $seconds): TimestampInterface
    {
        return new self(
            $this->carbon->addSeconds($seconds)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function addMinutes(int $minutes): TimestampInterface
    {
        return new self(
            $this->carbon->addMinutes($minutes)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function addHours(int $hours): TimestampInterface
    {
        return new self(
            $this->carbon->addHours($hours)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function addDays(int $days): TimestampInterface
    {
        return new self(
            $this->carbon->addDays($days)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function addMonths(int $months): TimestampInterface
    {
        return new self(
            $this->carbon->addMonths($months)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function addYears(int $years): TimestampInterface
    {
        return new self(
            $this->carbon->addYears($years)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function subtractSeconds(int $seconds): TimestampInterface
    {
        return new self(
            $this->carbon->subSeconds($seconds)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function subtractMinutes(int $minutes): TimestampInterface
    {
        return new self(
            $this->carbon->subMinutes($minutes)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function subtractHours(int $hours): TimestampInterface
    {
        return new self(
            $this->carbon->subHours($hours)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function subtractDays(int $days): TimestampInterface
    {
        return new self(
            $this->carbon->subDays($days)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function subtractMonths(int $months): TimestampInterface
    {
        return new self(
            $this->carbon->subMonths($months)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function subtractYears(int $years): TimestampInterface
    {
        return new self(
            $this->carbon->subYears($years)->getTimestamp(),
            $this->carbon->timezone->getName(),
            $this->carbon->locale
        );
    }

    public function timestamp(): int
    {
        return $this->carbon->getTimestamp();
    }
}