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 Carbon\Carbon; 23use Carbon\CarbonImmutable; 24use Fisharebest\Webtrees\Contracts\TimestampInterface; 25 26use function gregoriantojd; 27 28/** 29 * A localized date-time. 30 */ 31class Timestamp implements TimestampInterface 32{ 33 private CarbonImmutable $carbon; 34 35 public function __construct(int $timestamp, string $timezone, string $locale) 36 { 37 $this->carbon = CarbonImmutable::createFromTimestamp($timestamp, $timezone); 38 $this->carbon->locale($locale); 39 } 40 41 public function __clone() 42 { 43 $this->carbon = clone($this->carbon); 44 } 45 46 public function julianDay(): int 47 { 48 return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year); 49 } 50 51 public function diffForHumans(): string 52 { 53 return $this->carbon->diffForHumans(); 54 } 55 56 public function format(string $format): string 57 { 58 return $this->carbon->format($format); 59 } 60 61 public function isoFormat(string $format): string 62 { 63 return $this->carbon->isoFormat($format); 64 } 65 66 public function toDateString(): string 67 { 68 return $this->carbon->format('Y-m-d'); 69 } 70 71 public function toDateTimeString(): string 72 { 73 return $this->carbon->format('Y-m-d H:i:s'); 74 } 75 76 public function compare(TimestampInterface $timestamp): int 77 { 78 return $this->timestamp() <=> $timestamp->timestamp(); 79 } 80 81 public function addSeconds(int $seconds): TimestampInterface 82 { 83 return new self( 84 $this->carbon->addSeconds($seconds)->getTimestamp(), 85 $this->carbon->timezone->getName(), 86 $this->carbon->locale 87 ); 88 } 89 90 public function addMinutes(int $minutes): TimestampInterface 91 { 92 return new self( 93 $this->carbon->addMinutes($minutes)->getTimestamp(), 94 $this->carbon->timezone->getName(), 95 $this->carbon->locale 96 ); 97 } 98 99 public function addHours(int $hours): TimestampInterface 100 { 101 return new self( 102 $this->carbon->addHours($hours)->getTimestamp(), 103 $this->carbon->timezone->getName(), 104 $this->carbon->locale 105 ); 106 } 107 108 public function addDays(int $days): TimestampInterface 109 { 110 return new self( 111 $this->carbon->addDays($days)->getTimestamp(), 112 $this->carbon->timezone->getName(), 113 $this->carbon->locale 114 ); 115 } 116 117 public function addMonths(int $months): TimestampInterface 118 { 119 return new self( 120 $this->carbon->addMonth($months)->getTimestamp(), 121 $this->carbon->timezone->getName(), 122 $this->carbon->locale 123 ); 124 } 125 126 public function addYears(int $years): TimestampInterface 127 { 128 return new self( 129 $this->carbon->addYears($years)->getTimestamp(), 130 $this->carbon->timezone->getName(), 131 $this->carbon->locale 132 ); 133 } 134 135 public function subtractSeconds(int $seconds): TimestampInterface 136 { 137 return new self( 138 $this->carbon->subSeconds($seconds)->getTimestamp(), 139 $this->carbon->timezone->getName(), 140 $this->carbon->locale 141 ); 142 } 143 144 public function subtractMinutes(int $minutes): TimestampInterface 145 { 146 return new self( 147 $this->carbon->subMinutes($minutes)->getTimestamp(), 148 $this->carbon->timezone->getName(), 149 $this->carbon->locale 150 ); 151 } 152 153 public function subtractHours(int $hours): TimestampInterface 154 { 155 return new self( 156 $this->carbon->subHours($hours)->getTimestamp(), 157 $this->carbon->timezone->getName(), 158 $this->carbon->locale 159 ); 160 } 161 162 public function subtractDays(int $days): TimestampInterface 163 { 164 return new self( 165 $this->carbon->subDays($days)->getTimestamp(), 166 $this->carbon->timezone->getName(), 167 $this->carbon->locale 168 ); 169 } 170 171 public function subtractMonths(int $months): TimestampInterface 172 { 173 return new self( 174 $this->carbon->subMonths($months)->getTimestamp(), 175 $this->carbon->timezone->getName(), 176 $this->carbon->locale 177 ); 178 } 179 180 public function subtractYears(int $years): TimestampInterface 181 { 182 return new self( 183 $this->carbon->subYears($years)->getTimestamp(), 184 $this->carbon->timezone->getName(), 185 $this->carbon->locale 186 ); 187 } 188 189 public function timestamp(): int 190 { 191 return $this->carbon->getTimestamp(); 192 } 193} 194