Passed
Pull Request — main (#4931)
by Greg
15:47 queued 09:48
created

Timestamp::toUTCDateTimeString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees;
21
22
use Carbon\Carbon;
23
use Carbon\CarbonImmutable;
24
use Fisharebest\Webtrees\Contracts\TimestampInterface;
25
26
use function gregoriantojd;
27
28
/**
29
 * A localized date-time.
30
 */
31
class Timestamp implements TimestampInterface
32
{
33
    private CarbonImmutable $carbon;
34
35
    public function __construct(int $timestamp, string $timezone, string $locale)
36
    {
37
        $this->carbon = CarbonImmutable::createFromTimestamp($timestamp, $timezone);
38
        $this->carbon->locale($locale);
39
    }
40
41
    public function __clone()
42
    {
43
        $this->carbon = clone($this->carbon);
44
    }
45
46
    public function julianDay(): int
47
    {
48
        return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year);
49
    }
50
51
    public function diffForHumans(): string
52
    {
53
        return $this->carbon->diffForHumans();
54
    }
55
56
    public function format(string $format): string
57
    {
58
        return $this->carbon->format($format);
59
    }
60
61
    public function isoFormat(string $format): string
62
    {
63
        return $this->carbon->isoFormat($format);
64
    }
65
66
    public function toDateString(): string
67
    {
68
        return $this->carbon->format('Y-m-d');
69
    }
70
71
    public function toDateTimeString(): string
72
    {
73
        return $this->carbon->format('Y-m-d H:i:s');
74
    }
75
76
    /**
77
     * Use UTC instead of the saved timezone.
78
     *
79
     * @return string
80
     */
81
    public function toUTCDateTimeString(): string
82
    {
83
        return Carbon::createFromTimestampUTC($this->carbon->getTimestamp())->format('Y-m-d H:i:s');
84
    }
85
86
    /**
87
     * @param TimestampInterface $datetime
88
     *
89
     * @return int
90
     */
91
    public function compare(TimestampInterface $timestamp): int
92
    {
93
        return $this->timestamp() <=> $timestamp->timestamp();
94
    }
95
96
    public function addSeconds(int $seconds): TimestampInterface
97
    {
98
        return new self(
99
            $this->carbon->addSeconds($seconds)->getTimestamp(),
100
            $this->carbon->timezone->getName(),
101
            $this->carbon->locale
102
        );
103
    }
104
105
    public function addMinutes(int $minutes): TimestampInterface
106
    {
107
        return new self(
108
            $this->carbon->addMinutes($minutes)->getTimestamp(),
109
            $this->carbon->timezone->getName(),
110
            $this->carbon->locale
111
        );
112
    }
113
114
    public function addHours(int $hours): TimestampInterface
115
    {
116
        return new self(
117
            $this->carbon->addHours($hours)->getTimestamp(),
118
            $this->carbon->timezone->getName(),
119
            $this->carbon->locale
120
        );
121
    }
122
123
    public function addDays(int $days): TimestampInterface
124
    {
125
        return new self(
126
            $this->carbon->addDays($days)->getTimestamp(),
127
            $this->carbon->timezone->getName(),
128
            $this->carbon->locale
129
        );
130
    }
131
132
    public function addMonths(int $months): TimestampInterface
133
    {
134
        return new self(
135
            $this->carbon->addMonths($months)->getTimestamp(),
136
            $this->carbon->timezone->getName(),
137
            $this->carbon->locale
138
        );
139
    }
140
141
    public function addYears(int $years): TimestampInterface
142
    {
143
        return new self(
144
            $this->carbon->addYears($years)->getTimestamp(),
145
            $this->carbon->timezone->getName(),
146
            $this->carbon->locale
147
        );
148
    }
149
150
    public function subtractSeconds(int $seconds): TimestampInterface
151
    {
152
        return new self(
153
            $this->carbon->subSeconds($seconds)->getTimestamp(),
154
            $this->carbon->timezone->getName(),
155
            $this->carbon->locale
156
        );
157
    }
158
159
    public function subtractMinutes(int $minutes): TimestampInterface
160
    {
161
        return new self(
162
            $this->carbon->subMinutes($minutes)->getTimestamp(),
163
            $this->carbon->timezone->getName(),
164
            $this->carbon->locale
165
        );
166
    }
167
168
    public function subtractHours(int $hours): TimestampInterface
169
    {
170
        return new self(
171
            $this->carbon->subHours($hours)->getTimestamp(),
172
            $this->carbon->timezone->getName(),
173
            $this->carbon->locale
174
        );
175
    }
176
177
    public function subtractDays(int $days): TimestampInterface
178
    {
179
        return new self(
180
            $this->carbon->subDays($days)->getTimestamp(),
181
            $this->carbon->timezone->getName(),
182
            $this->carbon->locale
183
        );
184
    }
185
186
    public function subtractMonths(int $months): TimestampInterface
187
    {
188
        return new self(
189
            $this->carbon->subMonths($months)->getTimestamp(),
190
            $this->carbon->timezone->getName(),
191
            $this->carbon->locale
192
        );
193
    }
194
195
    public function subtractYears(int $years): TimestampInterface
196
    {
197
        return new self(
198
            $this->carbon->subYears($years)->getTimestamp(),
199
            $this->carbon->timezone->getName(),
200
            $this->carbon->locale
201
        );
202
    }
203
204
    public function timestamp(): int
205
    {
206
        return $this->carbon->getTimestamp();
207
    }
208
}
209