xref: /webtrees/tests/app/TimestampTest.php (revision e873f434551745f888937263ff89e80db3b0f785)
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        self::assertSame(2460296, gregoriantojd(12, 17, 2023));
35        self::assertSame(2460296, $timestamp->julianDay());
36    }
37
38    public function testDiffForHumans(): void
39    {
40        $timestamp = new Timestamp(time(), 'UTC', 'en-US');
41
42        self::assertSame('5 years ago', $timestamp->subtractYears(5)->diffForHumans());
43        self::assertSame('5 months ago', $timestamp->subtractMonths(5)->diffForHumans());
44        self::assertSame('5 days ago', $timestamp->subtractDays(5)->diffForHumans());
45        self::assertSame('5 hours ago', $timestamp->subtractHours(5)->diffForHumans());
46        self::assertSame('5 minutes ago', $timestamp->subtractMinutes(5)->diffForHumans());
47        self::assertSame('5 seconds ago', $timestamp->subtractSeconds(5)->diffForHumans());
48
49        $timestamp = new Timestamp(time(), 'UTC', 'fr_FR');
50
51        self::assertSame('il y a 5 ans', $timestamp->subtractYears(5)->diffForHumans());
52        self::assertSame('il y a 5 mois', $timestamp->subtractMonths(5)->diffForHumans());
53        self::assertSame('il y a 5 jours', $timestamp->subtractDays(5)->diffForHumans());
54        self::assertSame('il y a 5 heures', $timestamp->subtractHours(5)->diffForHumans());
55        self::assertSame('il y a 5 minutes', $timestamp->subtractMinutes(5)->diffForHumans());
56        self::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        self::assertSame('17', $timestamp->format('d'));
64        self::assertSame('Sun', $timestamp->format('D'));
65        self::assertSame('17', $timestamp->format('j'));
66        self::assertSame('Sunday', $timestamp->format('l'));
67        self::assertSame('7', $timestamp->format('N'));
68        self::assertSame('th', $timestamp->format('S'));
69        self::assertSame('0', $timestamp->format('w'));
70        self::assertSame('350', $timestamp->format('z'));
71        self::assertSame('50', $timestamp->format('W'));
72        self::assertSame('December', $timestamp->format('F'));
73        self::assertSame('12', $timestamp->format('m'));
74        self::assertSame('Dec', $timestamp->format('M'));
75        self::assertSame('12', $timestamp->format('n'));
76        self::assertSame('31', $timestamp->format('t'));
77        self::assertSame('0', $timestamp->format('L'));
78        self::assertSame('2023', $timestamp->format('o'));
79        self::assertSame('2023', $timestamp->format('Y'));
80        self::assertSame('23', $timestamp->format('y'));
81        self::assertSame('pm', $timestamp->format('a'));
82        self::assertSame('PM', $timestamp->format('A'));
83        self::assertSame('723', $timestamp->format('B'));
84        self::assertSame('4', $timestamp->format('g'));
85        self::assertSame('16', $timestamp->format('G'));
86        self::assertSame('04', $timestamp->format('h'));
87        self::assertSame('16', $timestamp->format('H'));
88        self::assertSame('21', $timestamp->format('i'));
89        self::assertSame('19', $timestamp->format('s'));
90        self::assertSame('000000', $timestamp->format('u'));
91        self::assertSame('UTC', $timestamp->format('e'));
92        self::assertSame('0', $timestamp->format('I'));
93        self::assertSame('+0000', $timestamp->format('O'));
94        self::assertSame('+00:00', $timestamp->format('P'));
95        self::assertSame('UTC', $timestamp->format('T'));
96        self::assertSame('0', $timestamp->format('Z'));
97        self::assertSame('2023-12-17T16:21:19+00:00', $timestamp->format('c'));
98        self::assertSame('Sun, 17 Dec 2023 16:21:19 +0000', $timestamp->format('r'));
99        self::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        self::assertSame('12/17/2023', $timestamp->isoFormat('l'));
107        self::assertSame('Dec 17, 2023', $timestamp->isoFormat('ll'));
108        self::assertSame('Dec 17, 2023 4:21 PM', $timestamp->isoFormat('lll'));
109        self::assertSame('Sun, Dec 17, 2023 4:21 PM', $timestamp->isoFormat('llll'));
110
111        self::assertSame('12/17/2023', $timestamp->isoFormat('L'));
112        self::assertSame('December 17, 2023', $timestamp->isoFormat('LL'));
113        self::assertSame('December 17, 2023 4:21 PM', $timestamp->isoFormat('LLL'));
114        self::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        self::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        self::assertSame(-1, $timestamp1->compare($timestamp2));
130        self::assertSame(0, $timestamp1->compare($timestamp1));
131        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
139        self::assertSame('2023-12-17 16:21:20', $timestamp->addSeconds(1)->toDateTimeString());
140        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
148        self::assertSame('2023-12-17 16:22:19', $timestamp->addMinutes(1)->toDateTimeString());
149        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
157        self::assertSame('2023-12-17 17:21:19', $timestamp->addHours(1)->toDateTimeString());
158        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
166        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
174        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
182        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
190        self::assertSame('2023-12-17 16:21:18', $timestamp->subtractSeconds(1)->toDateTimeString());
191        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
199        self::assertSame('2023-12-17 16:20:19', $timestamp->subtractMinutes(1)->toDateTimeString());
200        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
208        self::assertSame('2023-12-17 15:21:19', $timestamp->subtractHours(1)->toDateTimeString());
209        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
217        self::assertSame('2023-12-16 16:21:19', $timestamp->subtractDays(1)->toDateTimeString());
218        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
226        self::assertSame('2023-11-17 16:21:19', $timestamp->subtractMonths(1)->toDateTimeString());
227        self::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        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
235        self::assertSame('2022-12-17 16:21:19', $timestamp->subtractYears(1)->toDateTimeString());
236        self::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        self::assertSame(1702830079, $timestamp->timestamp());
244    }
245}
246