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 public function addMinutes(int $minutes): TimestampInterface 90 { 91 return new self( 92 $this->carbon->addMinutes($minutes)->getTimestamp(), 93 $this->carbon->timezone->getName(), 94 $this->carbon->locale); 95 } 96 97 public function addHours(int $hours): TimestampInterface 98 { 99 return new self( 100 $this->carbon->addHours($hours)->getTimestamp(), 101 $this->carbon->timezone->getName(), 102 $this->carbon->locale); 103 } 104 105 public function addDays(int $days): TimestampInterface 106 { 107 return new self( 108 $this->carbon->addDays($days)->getTimestamp(), 109 $this->carbon->timezone->getName(), 110 $this->carbon->locale); 111 } 112 113 public function addMonths(int $months): TimestampInterface 114 { 115 return new self( 116 $this->carbon->addMonth($months)->getTimestamp(), 117 $this->carbon->timezone->getName(), 118 $this->carbon->locale); 119 } 120 121 public function addYears(int $years): TimestampInterface 122 { 123 return new self( 124 $this->carbon->addYears($years)->getTimestamp(), 125 $this->carbon->timezone->getName(), 126 $this->carbon->locale); 127 } 128 129 public function subtractSeconds(int $seconds): TimestampInterface 130 { 131 return new self( 132 $this->carbon->subSeconds($seconds)->getTimestamp(), 133 $this->carbon->timezone->getName(), 134 $this->carbon->locale); 135 } 136 137 public function subtractMinutes(int $minutes): TimestampInterface 138 { 139 return new self( 140 $this->carbon->subMinutes($minutes)->getTimestamp(), 141 $this->carbon->timezone->getName(), 142 $this->carbon->locale); 143 } 144 145 public function subtractHours(int $hours): TimestampInterface 146 { 147 return new self( 148 $this->carbon->subHours($hours)->getTimestamp(), 149 $this->carbon->timezone->getName(), 150 $this->carbon->locale); 151 } 152 153 public function subtractDays(int $days): TimestampInterface 154 { 155 return new self( 156 $this->carbon->subDays($days)->getTimestamp(), 157 $this->carbon->timezone->getName(), 158 $this->carbon->locale); 159 } 160 161 public function subtractMonths(int $months): TimestampInterface 162 { 163 return new self( 164 $this->carbon->subMonths($months)->getTimestamp(), 165 $this->carbon->timezone->getName(), 166 $this->carbon->locale); 167 } 168 169 public function subtractYears(int $years): TimestampInterface 170 { 171 return new self( 172 $this->carbon->subYears($years)->getTimestamp(), 173 $this->carbon->timezone->getName(), 174 $this->carbon->locale); 175 } 176 177 public function timestamp(): int 178 { 179 return $this->carbon->getTimestamp(); 180 } 181} 182