1d97083feSGreg Roach<?php 2d97083feSGreg Roach 3d97083feSGreg Roach/** 4d97083feSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6d97083feSGreg Roach * This program is free software: you can redistribute it and/or modify 7d97083feSGreg Roach * it under the terms of the GNU General Public License as published by 8d97083feSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d97083feSGreg Roach * (at your option) any later version. 10d97083feSGreg Roach * This program is distributed in the hope that it will be useful, 11d97083feSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d97083feSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d97083feSGreg Roach * GNU General Public License for more details. 14d97083feSGreg Roach * You should have received a copy of the GNU General Public License 15d97083feSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16d97083feSGreg Roach */ 17d97083feSGreg Roach 18d97083feSGreg Roachdeclare(strict_types=1); 19d97083feSGreg Roach 20d97083feSGreg Roachnamespace Fisharebest\Webtrees; 21d97083feSGreg Roach 22d97083feSGreg Roachuse Carbon\Carbon; 23*8e989043SGreg Roachuse Carbon\CarbonImmutable; 24d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface; 25d97083feSGreg Roach 26*8e989043SGreg Roachuse function gregoriantojd; 27*8e989043SGreg Roach 28d97083feSGreg Roach/** 29d97083feSGreg Roach * A localized date-time. 30d97083feSGreg Roach */ 31d97083feSGreg Roachclass Timestamp implements TimestampInterface 32d97083feSGreg Roach{ 33*8e989043SGreg Roach private CarbonImmutable $carbon; 34d97083feSGreg Roach 35d97083feSGreg Roach public function __construct(int $timestamp, string $timezone, string $locale) 36d97083feSGreg Roach { 37*8e989043SGreg Roach $this->carbon = CarbonImmutable::createFromTimestamp($timestamp, $timezone); 38d97083feSGreg Roach $this->carbon->locale($locale); 39d97083feSGreg Roach } 40d97083feSGreg Roach 41ee551e0bSGreg Roach public function __clone() 42ee551e0bSGreg Roach { 43ee551e0bSGreg Roach $this->carbon = clone($this->carbon); 44ee551e0bSGreg Roach } 45ee551e0bSGreg Roach 46d97083feSGreg Roach public function julianDay(): int 47d97083feSGreg Roach { 48d97083feSGreg Roach return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year); 49d97083feSGreg Roach } 50d97083feSGreg Roach 51d97083feSGreg Roach public function diffForHumans(): string 52d97083feSGreg Roach { 53d97083feSGreg Roach return $this->carbon->diffForHumans(); 54d97083feSGreg Roach } 55d97083feSGreg Roach 56d97083feSGreg Roach public function format(string $format): string 57d97083feSGreg Roach { 58d97083feSGreg Roach return $this->carbon->format($format); 59d97083feSGreg Roach } 60d97083feSGreg Roach 61d97083feSGreg Roach public function isoFormat(string $format): string 62d97083feSGreg Roach { 63d97083feSGreg Roach return $this->carbon->isoFormat($format); 64d97083feSGreg Roach } 65d97083feSGreg Roach 66d97083feSGreg Roach public function toDateString(): string 67d97083feSGreg Roach { 68d97083feSGreg Roach return $this->carbon->format('Y-m-d'); 69d97083feSGreg Roach } 70d97083feSGreg Roach 71d97083feSGreg Roach public function toDateTimeString(): string 72d97083feSGreg Roach { 73d97083feSGreg Roach return $this->carbon->format('Y-m-d H:i:s'); 74d97083feSGreg Roach } 75d97083feSGreg Roach 76*8e989043SGreg Roach public function compare(TimestampInterface $timestamp): int 77d97083feSGreg Roach { 78*8e989043SGreg Roach return $this->timestamp() <=> $timestamp->timestamp(); 79d97083feSGreg Roach } 80d97083feSGreg Roach 817761cf05SGreg Roach public function addSeconds(int $seconds): TimestampInterface 82d97083feSGreg Roach { 83*8e989043SGreg Roach return new self( 84*8e989043SGreg Roach $this->carbon->addSeconds($seconds)->getTimestamp(), 85*8e989043SGreg Roach $this->carbon->timezone->getName(), 86*8e989043SGreg Roach $this->carbon->locale); 87d97083feSGreg Roach } 88d97083feSGreg Roach 897761cf05SGreg Roach public function addMinutes(int $minutes): TimestampInterface 90d97083feSGreg Roach { 91*8e989043SGreg Roach return new self( 92*8e989043SGreg Roach $this->carbon->addMinutes($minutes)->getTimestamp(), 93*8e989043SGreg Roach $this->carbon->timezone->getName(), 94*8e989043SGreg Roach $this->carbon->locale); 95d97083feSGreg Roach } 96d97083feSGreg Roach 977761cf05SGreg Roach public function addHours(int $hours): TimestampInterface 98d97083feSGreg Roach { 99*8e989043SGreg Roach return new self( 100*8e989043SGreg Roach $this->carbon->addHours($hours)->getTimestamp(), 101*8e989043SGreg Roach $this->carbon->timezone->getName(), 102*8e989043SGreg Roach $this->carbon->locale); 103d97083feSGreg Roach } 104d97083feSGreg Roach 1057761cf05SGreg Roach public function addDays(int $days): TimestampInterface 106d97083feSGreg Roach { 107*8e989043SGreg Roach return new self( 108*8e989043SGreg Roach $this->carbon->addDays($days)->getTimestamp(), 109*8e989043SGreg Roach $this->carbon->timezone->getName(), 110*8e989043SGreg Roach $this->carbon->locale); 111d97083feSGreg Roach } 112d97083feSGreg Roach 1137761cf05SGreg Roach public function addMonths(int $months): TimestampInterface 114d97083feSGreg Roach { 115*8e989043SGreg Roach return new self( 116*8e989043SGreg Roach $this->carbon->addMonth($months)->getTimestamp(), 117*8e989043SGreg Roach $this->carbon->timezone->getName(), 118*8e989043SGreg Roach $this->carbon->locale); 119d97083feSGreg Roach } 120d97083feSGreg Roach 1217761cf05SGreg Roach public function addYears(int $years): TimestampInterface 122d97083feSGreg Roach { 123*8e989043SGreg Roach return new self( 124*8e989043SGreg Roach $this->carbon->addYears($years)->getTimestamp(), 125*8e989043SGreg Roach $this->carbon->timezone->getName(), 126*8e989043SGreg Roach $this->carbon->locale); 127d97083feSGreg Roach } 128d97083feSGreg Roach 1297761cf05SGreg Roach public function subtractSeconds(int $seconds): TimestampInterface 130d97083feSGreg Roach { 131*8e989043SGreg Roach return new self( 132*8e989043SGreg Roach $this->carbon->subSeconds($seconds)->getTimestamp(), 133*8e989043SGreg Roach $this->carbon->timezone->getName(), 134*8e989043SGreg Roach $this->carbon->locale); 135d97083feSGreg Roach } 136d97083feSGreg Roach 1377761cf05SGreg Roach public function subtractMinutes(int $minutes): TimestampInterface 138d97083feSGreg Roach { 139*8e989043SGreg Roach return new self( 140*8e989043SGreg Roach $this->carbon->subMinutes($minutes)->getTimestamp(), 141*8e989043SGreg Roach $this->carbon->timezone->getName(), 142*8e989043SGreg Roach $this->carbon->locale); 143d97083feSGreg Roach } 144d97083feSGreg Roach 1457761cf05SGreg Roach public function subtractHours(int $hours): TimestampInterface 146d97083feSGreg Roach { 147*8e989043SGreg Roach return new self( 148*8e989043SGreg Roach $this->carbon->subHours($hours)->getTimestamp(), 149*8e989043SGreg Roach $this->carbon->timezone->getName(), 150*8e989043SGreg Roach $this->carbon->locale); 151d97083feSGreg Roach } 152d97083feSGreg Roach 1537761cf05SGreg Roach public function subtractDays(int $days): TimestampInterface 154d97083feSGreg Roach { 155*8e989043SGreg Roach return new self( 156*8e989043SGreg Roach $this->carbon->subDays($days)->getTimestamp(), 157*8e989043SGreg Roach $this->carbon->timezone->getName(), 158*8e989043SGreg Roach $this->carbon->locale); 159d97083feSGreg Roach } 160d97083feSGreg Roach 1617761cf05SGreg Roach public function subtractMonths(int $months): TimestampInterface 162d97083feSGreg Roach { 163*8e989043SGreg Roach return new self( 164*8e989043SGreg Roach $this->carbon->subMonths($months)->getTimestamp(), 165*8e989043SGreg Roach $this->carbon->timezone->getName(), 166*8e989043SGreg Roach $this->carbon->locale); 167d97083feSGreg Roach } 168d97083feSGreg Roach 1697761cf05SGreg Roach public function subtractYears(int $years): TimestampInterface 170d97083feSGreg Roach { 171*8e989043SGreg Roach return new self( 172*8e989043SGreg Roach $this->carbon->subYears($years)->getTimestamp(), 173*8e989043SGreg Roach $this->carbon->timezone->getName(), 174*8e989043SGreg Roach $this->carbon->locale); 175d97083feSGreg Roach } 176d97083feSGreg Roach 177d97083feSGreg Roach public function timestamp(): int 178d97083feSGreg Roach { 179d97083feSGreg Roach return $this->carbon->getTimestamp(); 180d97083feSGreg Roach } 181d97083feSGreg Roach} 182