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