xref: /webtrees/tests/app/TimestampTest.php (revision a6d4916949516e3eeaad5077f18f158b867e0927)
1a8139624SGreg Roach<?php
2a8139624SGreg Roach
3a8139624SGreg Roach/**
4a8139624SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a8139624SGreg Roach * This program is free software: you can redistribute it and/or modify
7a8139624SGreg Roach * it under the terms of the GNU General Public License as published by
8a8139624SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a8139624SGreg Roach * (at your option) any later version.
10a8139624SGreg Roach * This program is distributed in the hope that it will be useful,
11a8139624SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a8139624SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a8139624SGreg Roach * GNU General Public License for more details.
14a8139624SGreg Roach * You should have received a copy of the GNU General Public License
15a8139624SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a8139624SGreg Roach */
17a8139624SGreg Roach
18a8139624SGreg Roachdeclare(strict_types=1);
19a8139624SGreg Roach
20a8139624SGreg Roachnamespace Fisharebest\Webtrees;
21a8139624SGreg Roach
22202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
23202c018bSGreg Roach
24ee551e0bSGreg Roachuse function gregoriantojd;
25ee551e0bSGreg Roachuse function mktime;
26a8139624SGreg Roach
27202c018bSGreg Roach#[CoversClass(Timestamp::class)]
28a8139624SGreg Roachclass TimestampTest extends TestCase
29a8139624SGreg Roach{
30ee551e0bSGreg Roach    public function testJulianDay(): void
31a8139624SGreg Roach    {
32ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
33ee551e0bSGreg Roach
34*a6d49169SGreg Roach        self::assertSame(2460296, gregoriantojd(12, 17, 2023));
35*a6d49169SGreg Roach        self::assertSame(2460296, $timestamp->julianDay());
36ee551e0bSGreg Roach    }
37ee551e0bSGreg Roach
38ee551e0bSGreg Roach    public function testDiffForHumans(): void
39ee551e0bSGreg Roach    {
40ee551e0bSGreg Roach        $timestamp = new Timestamp(time(), 'UTC', 'en-US');
41ee551e0bSGreg Roach
42*a6d49169SGreg Roach        self::assertSame('5 years ago', $timestamp->subtractYears(5)->diffForHumans());
43*a6d49169SGreg Roach        self::assertSame('5 months ago', $timestamp->subtractMonths(5)->diffForHumans());
44*a6d49169SGreg Roach        self::assertSame('5 days ago', $timestamp->subtractDays(5)->diffForHumans());
45*a6d49169SGreg Roach        self::assertSame('5 hours ago', $timestamp->subtractHours(5)->diffForHumans());
46*a6d49169SGreg Roach        self::assertSame('5 minutes ago', $timestamp->subtractMinutes(5)->diffForHumans());
47*a6d49169SGreg Roach        self::assertSame('5 seconds ago', $timestamp->subtractSeconds(5)->diffForHumans());
48ee551e0bSGreg Roach
49ee551e0bSGreg Roach        $timestamp = new Timestamp(time(), 'UTC', 'fr_FR');
50ee551e0bSGreg Roach
51*a6d49169SGreg Roach        self::assertSame('il y a 5 ans', $timestamp->subtractYears(5)->diffForHumans());
52*a6d49169SGreg Roach        self::assertSame('il y a 5 mois', $timestamp->subtractMonths(5)->diffForHumans());
53*a6d49169SGreg Roach        self::assertSame('il y a 5 jours', $timestamp->subtractDays(5)->diffForHumans());
54*a6d49169SGreg Roach        self::assertSame('il y a 5 heures', $timestamp->subtractHours(5)->diffForHumans());
55*a6d49169SGreg Roach        self::assertSame('il y a 5 minutes', $timestamp->subtractMinutes(5)->diffForHumans());
56*a6d49169SGreg Roach        self::assertSame('il y a 5 secondes', $timestamp->subtractSeconds(5)->diffForHumans());
57ee551e0bSGreg Roach    }
58ee551e0bSGreg Roach
59ee551e0bSGreg Roach    public function testFormat(): void
60ee551e0bSGreg Roach    {
61ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
62ee551e0bSGreg Roach
63*a6d49169SGreg Roach        self::assertSame('17', $timestamp->format('d'));
64*a6d49169SGreg Roach        self::assertSame('Sun', $timestamp->format('D'));
65*a6d49169SGreg Roach        self::assertSame('17', $timestamp->format('j'));
66*a6d49169SGreg Roach        self::assertSame('Sunday', $timestamp->format('l'));
67*a6d49169SGreg Roach        self::assertSame('7', $timestamp->format('N'));
68*a6d49169SGreg Roach        self::assertSame('th', $timestamp->format('S'));
69*a6d49169SGreg Roach        self::assertSame('0', $timestamp->format('w'));
70*a6d49169SGreg Roach        self::assertSame('350', $timestamp->format('z'));
71*a6d49169SGreg Roach        self::assertSame('50', $timestamp->format('W'));
72*a6d49169SGreg Roach        self::assertSame('December', $timestamp->format('F'));
73*a6d49169SGreg Roach        self::assertSame('12', $timestamp->format('m'));
74*a6d49169SGreg Roach        self::assertSame('Dec', $timestamp->format('M'));
75*a6d49169SGreg Roach        self::assertSame('12', $timestamp->format('n'));
76*a6d49169SGreg Roach        self::assertSame('31', $timestamp->format('t'));
77*a6d49169SGreg Roach        self::assertSame('0', $timestamp->format('L'));
78*a6d49169SGreg Roach        self::assertSame('2023', $timestamp->format('o'));
79*a6d49169SGreg Roach        self::assertSame('2023', $timestamp->format('Y'));
80*a6d49169SGreg Roach        self::assertSame('23', $timestamp->format('y'));
81*a6d49169SGreg Roach        self::assertSame('pm', $timestamp->format('a'));
82*a6d49169SGreg Roach        self::assertSame('PM', $timestamp->format('A'));
83*a6d49169SGreg Roach        self::assertSame('723', $timestamp->format('B'));
84*a6d49169SGreg Roach        self::assertSame('4', $timestamp->format('g'));
85*a6d49169SGreg Roach        self::assertSame('16', $timestamp->format('G'));
86*a6d49169SGreg Roach        self::assertSame('04', $timestamp->format('h'));
87*a6d49169SGreg Roach        self::assertSame('16', $timestamp->format('H'));
88*a6d49169SGreg Roach        self::assertSame('21', $timestamp->format('i'));
89*a6d49169SGreg Roach        self::assertSame('19', $timestamp->format('s'));
90*a6d49169SGreg Roach        self::assertSame('000000', $timestamp->format('u'));
91*a6d49169SGreg Roach        self::assertSame('UTC', $timestamp->format('e'));
92*a6d49169SGreg Roach        self::assertSame('0', $timestamp->format('I'));
93*a6d49169SGreg Roach        self::assertSame('+0000', $timestamp->format('O'));
94*a6d49169SGreg Roach        self::assertSame('+00:00', $timestamp->format('P'));
95*a6d49169SGreg Roach        self::assertSame('UTC', $timestamp->format('T'));
96*a6d49169SGreg Roach        self::assertSame('0', $timestamp->format('Z'));
97*a6d49169SGreg Roach        self::assertSame('2023-12-17T16:21:19+00:00', $timestamp->format('c'));
98*a6d49169SGreg Roach        self::assertSame('Sun, 17 Dec 2023 16:21:19 +0000', $timestamp->format('r'));
99*a6d49169SGreg Roach        self::assertSame('1702830079', $timestamp->format('U'));
100ee551e0bSGreg Roach    }
101ee551e0bSGreg Roach
102ee551e0bSGreg Roach    public function testIsoFormat(): void
103ee551e0bSGreg Roach    {
104ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
105ee551e0bSGreg Roach
106*a6d49169SGreg Roach        self::assertSame('12/17/2023', $timestamp->isoFormat('l'));
107*a6d49169SGreg Roach        self::assertSame('Dec 17, 2023', $timestamp->isoFormat('ll'));
108*a6d49169SGreg Roach        self::assertSame('Dec 17, 2023 4:21 PM', $timestamp->isoFormat('lll'));
109*a6d49169SGreg Roach        self::assertSame('Sun, Dec 17, 2023 4:21 PM', $timestamp->isoFormat('llll'));
110ee551e0bSGreg Roach
111*a6d49169SGreg Roach        self::assertSame('12/17/2023', $timestamp->isoFormat('L'));
112*a6d49169SGreg Roach        self::assertSame('December 17, 2023', $timestamp->isoFormat('LL'));
113*a6d49169SGreg Roach        self::assertSame('December 17, 2023 4:21 PM', $timestamp->isoFormat('LLL'));
114*a6d49169SGreg Roach        self::assertSame('Sunday, December 17, 2023 4:21 PM', $timestamp->isoFormat('LLLL'));
115ee551e0bSGreg Roach    }
116ee551e0bSGreg Roach
117ee551e0bSGreg Roach    public function testToDateTimeString(): void
118ee551e0bSGreg Roach    {
119ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
120ee551e0bSGreg Roach
121*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
122ee551e0bSGreg Roach    }
123ee551e0bSGreg Roach
124ee551e0bSGreg Roach    public function testCompare(): void
125ee551e0bSGreg Roach    {
126ee551e0bSGreg Roach        $timestamp1 = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
127ee551e0bSGreg Roach        $timestamp2 = new Timestamp(mktime(16, 21, 20, 12, 17, 2023), 'UTC', 'en-US');
128ee551e0bSGreg Roach
129*a6d49169SGreg Roach        self::assertSame(-1, $timestamp1->compare($timestamp2));
130*a6d49169SGreg Roach        self::assertSame(0, $timestamp1->compare($timestamp1));
131*a6d49169SGreg Roach        self::assertSame(1, $timestamp2->compare($timestamp1));
132ee551e0bSGreg Roach    }
133ee551e0bSGreg Roach
134ee551e0bSGreg Roach    public function testAddSeconds(): void
135ee551e0bSGreg Roach    {
136ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
137ee551e0bSGreg Roach
138*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
139*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:20', $timestamp->addSeconds(1)->toDateTimeString());
140*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:18', $timestamp->addSeconds(-1)->toDateTimeString());
141ee551e0bSGreg Roach    }
142ee551e0bSGreg Roach
143ee551e0bSGreg Roach    public function testAddMinutes(): void
144ee551e0bSGreg Roach    {
145ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
146ee551e0bSGreg Roach
147*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
148*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:22:19', $timestamp->addMinutes(1)->toDateTimeString());
149*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:20:19', $timestamp->addMinutes(-1)->toDateTimeString());
150ee551e0bSGreg Roach    }
151ee551e0bSGreg Roach
152ee551e0bSGreg Roach    public function testAddHours(): void
153ee551e0bSGreg Roach    {
154ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
155ee551e0bSGreg Roach
156*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
157*a6d49169SGreg Roach        self::assertSame('2023-12-17 17:21:19', $timestamp->addHours(1)->toDateTimeString());
158*a6d49169SGreg Roach        self::assertSame('2023-12-17 15:21:19', $timestamp->addHours(-1)->toDateTimeString());
159ee551e0bSGreg Roach    }
160ee551e0bSGreg Roach
161ee551e0bSGreg Roach    public function testAddDays(): void
162ee551e0bSGreg Roach    {
163ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
164ee551e0bSGreg Roach
165*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
166*a6d49169SGreg Roach        self::assertSame('2023-12-18 16:21:19', $timestamp->addDays(1)->toDateTimeString());
167ee551e0bSGreg Roach    }
168ee551e0bSGreg Roach
169ee551e0bSGreg Roach    public function testAddMonths(): void
170ee551e0bSGreg Roach    {
171ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
172ee551e0bSGreg Roach
173*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
174*a6d49169SGreg Roach        self::assertSame('2024-01-17 16:21:19', $timestamp->addMonths(1)->toDateTimeString());
175ee551e0bSGreg Roach    }
176ee551e0bSGreg Roach
177ee551e0bSGreg Roach    public function testAddYears(): void
178ee551e0bSGreg Roach    {
179ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
180ee551e0bSGreg Roach
181*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
182*a6d49169SGreg Roach        self::assertSame('2024-12-17 16:21:19', $timestamp->addYears(1)->toDateTimeString());
183ee551e0bSGreg Roach    }
184ee551e0bSGreg Roach
185ee551e0bSGreg Roach    public function testSubtractSeconds(): void
186ee551e0bSGreg Roach    {
187ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
188ee551e0bSGreg Roach
189*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
190*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:18', $timestamp->subtractSeconds(1)->toDateTimeString());
191*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:20', $timestamp->subtractSeconds(-1)->toDateTimeString());
192ee551e0bSGreg Roach    }
193ee551e0bSGreg Roach
194ee551e0bSGreg Roach    public function testSubtractMinutes(): void
195ee551e0bSGreg Roach    {
196ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
197ee551e0bSGreg Roach
198*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
199*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:20:19', $timestamp->subtractMinutes(1)->toDateTimeString());
200*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:22:19', $timestamp->subtractMinutes(-1)->toDateTimeString());
201ee551e0bSGreg Roach    }
202ee551e0bSGreg Roach
203ee551e0bSGreg Roach    public function testSubtractHours(): void
204ee551e0bSGreg Roach    {
205ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
206ee551e0bSGreg Roach
207*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
208*a6d49169SGreg Roach        self::assertSame('2023-12-17 15:21:19', $timestamp->subtractHours(1)->toDateTimeString());
209*a6d49169SGreg Roach        self::assertSame('2023-12-17 17:21:19', $timestamp->subtractHours(-1)->toDateTimeString());
210ee551e0bSGreg Roach    }
211ee551e0bSGreg Roach
212ee551e0bSGreg Roach    public function testSubtractDays(): void
213ee551e0bSGreg Roach    {
214ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
215ee551e0bSGreg Roach
216*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
217*a6d49169SGreg Roach        self::assertSame('2023-12-16 16:21:19', $timestamp->subtractDays(1)->toDateTimeString());
218*a6d49169SGreg Roach        self::assertSame('2023-12-18 16:21:19', $timestamp->subtractDays(-1)->toDateTimeString());
219ee551e0bSGreg Roach    }
220ee551e0bSGreg Roach
221ee551e0bSGreg Roach    public function testSubtractMonths(): void
222ee551e0bSGreg Roach    {
223ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
224ee551e0bSGreg Roach
225*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
226*a6d49169SGreg Roach        self::assertSame('2023-11-17 16:21:19', $timestamp->subtractMonths(1)->toDateTimeString());
227*a6d49169SGreg Roach        self::assertSame('2024-01-17 16:21:19', $timestamp->subtractMonths(-1)->toDateTimeString());
228ee551e0bSGreg Roach    }
229ee551e0bSGreg Roach
230ee551e0bSGreg Roach    public function testSubtractYears(): void
231ee551e0bSGreg Roach    {
232ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
233ee551e0bSGreg Roach
234*a6d49169SGreg Roach        self::assertSame('2023-12-17 16:21:19', $timestamp->toDateTimeString());
235*a6d49169SGreg Roach        self::assertSame('2022-12-17 16:21:19', $timestamp->subtractYears(1)->toDateTimeString());
236*a6d49169SGreg Roach        self::assertSame('2024-12-17 16:21:19', $timestamp->subtractYears(-1)->toDateTimeString());
237ee551e0bSGreg Roach    }
238ee551e0bSGreg Roach
239ee551e0bSGreg Roach    public function testTimestamp(): void
240ee551e0bSGreg Roach    {
241ee551e0bSGreg Roach        $timestamp = new Timestamp(mktime(16, 21, 19, 12, 17, 2023), 'UTC', 'en-US');
242ee551e0bSGreg Roach
243*a6d49169SGreg Roach        self::assertSame(1702830079, $timestamp->timestamp());
244a8139624SGreg Roach    }
245a8139624SGreg Roach}
246