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