xref: /webtrees/tests/app/TimestampTest.php (revision cf27371fce1e8b3c01f7d6807a88eaeff5ba9f3f)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use PHPUnit\Framework\Attributes\CoversClass;
23
24use function gregoriantojd;
25use function mktime;
26
27#[CoversClass(Timestamp::class)]
28class TimestampTest extends TestCase
29{
30    public function testJulianDay(): void
31    {
32        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
33
34        $this->assertSame(2460296, gregoriantojd(12, 17, 2023));
35        $this->assertSame(2460296, $timestamp->julianDay());
36    }
37
38    public function testDiffForHumans(): void
39    {
40        $timestamp = new Timestamp(time(), 'UTC', 'en-US');
41
42        $this->assertSame('5 years ago', $timestamp->subtractYears(5)->diffForHumans());
43        $this->assertSame('5 months ago', $timestamp->subtractMonths(5)->diffForHumans());
44        $this->assertSame('5 days ago', $timestamp->subtractDays(5)->diffForHumans());
45        $this->assertSame('5 hours ago', $timestamp->subtractHours(5)->diffForHumans());
46        $this->assertSame('5 minutes ago', $timestamp->subtractMinutes(5)->diffForHumans());
47        $this->assertSame('5 seconds ago', $timestamp->subtractSeconds(5)->diffForHumans());
48
49        $timestamp = new Timestamp(time(), 'UTC', 'fr_FR');
50
51        $this->assertSame('il y a 5 ans', $timestamp->subtractYears(5)->diffForHumans());
52        $this->assertSame('il y a 5 mois', $timestamp->subtractMonths(5)->diffForHumans());
53        $this->assertSame('il y a 5 jours', $timestamp->subtractDays(5)->diffForHumans());
54        $this->assertSame('il y a 5 heures', $timestamp->subtractHours(5)->diffForHumans());
55        $this->assertSame('il y a 5 minutes', $timestamp->subtractMinutes(5)->diffForHumans());
56        $this->assertSame('il y a 5 secondes', $timestamp->subtractSeconds(5)->diffForHumans());
57    }
58
59    public function testFormat(): void
60    {
61        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
62
63        $this->assertSame('17', $timestamp->format('d'));
64        $this->assertSame('Sun', $timestamp->format('D'));
65        $this->assertSame('17', $timestamp->format('j'));
66        $this->assertSame('Sunday', $timestamp->format('l'));
67        $this->assertSame('7', $timestamp->format('N'));
68        $this->assertSame('th', $timestamp->format('S'));
69        $this->assertSame('0', $timestamp->format('w'));
70        $this->assertSame('350', $timestamp->format('z'));
71        $this->assertSame('50', $timestamp->format('W'));
72        $this->assertSame('December', $timestamp->format('F'));
73        $this->assertSame('12', $timestamp->format('m'));
74        $this->assertSame('Dec', $timestamp->format('M'));
75        $this->assertSame('12', $timestamp->format('n'));
76        $this->assertSame('31', $timestamp->format('t'));
77        $this->assertSame('0', $timestamp->format('L'));
78        $this->assertSame('2023', $timestamp->format('o'));
79        $this->assertSame('2023', $timestamp->format('Y'));
80        $this->assertSame('23', $timestamp->format('y'));
81        $this->assertSame('pm', $timestamp->format('a'));
82        $this->assertSame('PM', $timestamp->format('A'));
83        $this->assertSame('723', $timestamp->format('B'));
84        $this->assertSame('4', $timestamp->format('g'));
85        $this->assertSame('16', $timestamp->format('G'));
86        $this->assertSame('04', $timestamp->format('h'));
87        $this->assertSame('16', $timestamp->format('H'));
88        $this->assertSame('21', $timestamp->format('i'));
89        $this->assertSame('19', $timestamp->format('s'));
90        $this->assertSame('000000', $timestamp->format('u'));
91        $this->assertSame('UTC', $timestamp->format('e'));
92        $this->assertSame('0', $timestamp->format('I'));
93        $this->assertSame('+0000', $timestamp->format('O'));
94        $this->assertSame('+00:00', $timestamp->format('P'));
95        $this->assertSame('UTC', $timestamp->format('T'));
96        $this->assertSame('0', $timestamp->format('Z'));
97        $this->assertSame('2023-12-17T16:21:19+00:00', $timestamp->format('c'));
98        $this->assertSame('Sun, 17 Dec 2023 16:21:19 +0000', $timestamp->format('r'));
99        $this->assertSame('1702830079', $timestamp->format('U'));
100    }
101
102    public function testIsoFormat(): void
103    {
104        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
105
106        $this->assertSame('12/17/2023', $timestamp->isoFormat('l'));
107        $this->assertSame('Dec 17, 2023', $timestamp->isoFormat('ll'));
108        $this->assertSame('Dec 17, 2023 4:21 PM', $timestamp->isoFormat('lll'));
109        $this->assertSame('Sun, Dec 17, 2023 4:21 PM', $timestamp->isoFormat('llll'));
110
111        $this->assertSame('12/17/2023', $timestamp->isoFormat('L'));
112        $this->assertSame('December 17, 2023', $timestamp->isoFormat('LL'));
113        $this->assertSame('December 17, 2023 4:21 PM', $timestamp->isoFormat('LLL'));
114        $this->assertSame('Sunday, December 17, 2023 4:21 PM', $timestamp->isoFormat('LLLL'));
115    }
116
117    public function testToDateTimeString(): void
118    {
119        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
120
121        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
122    }
123
124    public function testCompare(): void
125    {
126        $timestamp1 = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
127        $timestamp2 = new Timestamp(mktime(16, 21, 20, 12, 17, 2023), 'UTC', 'en-US');
128
129        $this->assertSame(-1, $timestamp1->compare($timestamp2));
130        $this->assertSame(0, $timestamp1->compare($timestamp1));
131        $this->assertSame(1, $timestamp2->compare($timestamp1));
132    }
133
134    public function testAddSeconds(): void
135    {
136        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
137
138        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
139        $this->assertSame('2023-12-17 16:21:20', $timestamp->addSeconds(1)->toDateTimeString());
140        $this->assertSame('2023-12-17 16:21:18', $timestamp->addSeconds(-1)->toDateTimeString());
141    }
142
143    public function testAddMinutes(): void
144    {
145        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
146
147        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
148        $this->assertSame('2023-12-17 16:22:19', $timestamp->addMinutes(1)->toDateTimeString());
149        $this->assertSame('2023-12-17 16:20:19', $timestamp->addMinutes(-1)->toDateTimeString());
150    }
151
152    public function testAddHours(): void
153    {
154        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
155
156        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
157        $this->assertSame('2023-12-17 17:21:19', $timestamp->addHours(1)->toDateTimeString());
158        $this->assertSame('2023-12-17 15:21:19', $timestamp->addHours(-1)->toDateTimeString());
159    }
160
161    public function testAddDays(): void
162    {
163        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
164
165        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
166        $this->assertSame('2023-12-18 16:21:19', $timestamp->addDays(1)->toDateTimeString());
167    }
168
169    public function testAddMonths(): void
170    {
171        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
172
173        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
174        $this->assertSame('2024-01-17 16:21:19', $timestamp->addMonths(1)->toDateTimeString());
175    }
176
177    public function testAddYears(): void
178    {
179        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
180
181        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
182        $this->assertSame('2024-12-17 16:21:19', $timestamp->addYears(1)->toDateTimeString());
183    }
184
185    public function testSubtractSeconds(): void
186    {
187        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
188
189        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
190        $this->assertSame('2023-12-17 16:21:18', $timestamp->subtractSeconds(1)->toDateTimeString());
191        $this->assertSame('2023-12-17 16:21:20', $timestamp->subtractSeconds(-1)->toDateTimeString());
192    }
193
194    public function testSubtractMinutes(): void
195    {
196        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
197
198        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
199        $this->assertSame('2023-12-17 16:20:19', $timestamp->subtractMinutes(1)->toDateTimeString());
200        $this->assertSame('2023-12-17 16:22:19', $timestamp->subtractMinutes(-1)->toDateTimeString());
201    }
202
203    public function testSubtractHours(): void
204    {
205        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
206
207        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
208        $this->assertSame('2023-12-17 15:21:19', $timestamp->subtractHours(1)->toDateTimeString());
209        $this->assertSame('2023-12-17 17:21:19', $timestamp->subtractHours(-1)->toDateTimeString());
210    }
211
212    public function testSubtractDays(): void
213    {
214        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
215
216        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
217        $this->assertSame('2023-12-16 16:21:19', $timestamp->subtractDays(1)->toDateTimeString());
218        $this->assertSame('2023-12-18 16:21:19', $timestamp->subtractDays(-1)->toDateTimeString());
219    }
220
221    public function testSubtractMonths(): void
222    {
223        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
224
225        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
226        $this->assertSame('2023-11-17 16:21:19', $timestamp->subtractMonths(1)->toDateTimeString());
227        $this->assertSame('2024-01-17 16:21:19', $timestamp->subtractMonths(-1)->toDateTimeString());
228    }
229
230    public function testSubtractYears(): void
231    {
232        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
233
234        $this->assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
235        $this->assertSame('2022-12-17 16:21:19', $timestamp->subtractYears(1)->toDateTimeString());
236        $this->assertSame('2024-12-17 16:21:19', $timestamp->subtractYears(-1)->toDateTimeString());
237    }
238
239    public function testTimestamp(): void
240    {
241        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
242
243        $this->assertSame(1702830079, $timestamp->timestamp());
244    }
245}
246