Passed
Pull Request — main (#4931)
by
unknown
06:16 queued 12s
created

Timestamp   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 25
eloc 54
c 2
b 0
f 0
dl 0
loc 292
rs 10

23 Methods

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