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