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; 23d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface; 24d97083feSGreg Roach 25d97083feSGreg Roach/** 26d97083feSGreg Roach * A localized date-time. 27d97083feSGreg Roach */ 28d97083feSGreg Roachclass Timestamp implements TimestampInterface 29d97083feSGreg Roach{ 30d97083feSGreg Roach private Carbon $carbon; 31d97083feSGreg Roach 32d97083feSGreg Roach /** 33d97083feSGreg Roach * @param int $timestamp 34d97083feSGreg Roach * @param string $timezone 35d97083feSGreg Roach * @param string $locale 36d97083feSGreg Roach */ 37d97083feSGreg Roach public function __construct(int $timestamp, string $timezone, string $locale) 38d97083feSGreg Roach { 39d97083feSGreg Roach $this->carbon = Carbon::createFromTimestamp($timestamp, $timezone); 40d97083feSGreg Roach $this->carbon->locale($locale); 41d97083feSGreg Roach } 42d97083feSGreg Roach 43*ee551e0bSGreg Roach public function __clone() 44*ee551e0bSGreg Roach { 45*ee551e0bSGreg Roach $this->carbon = clone($this->carbon); 46*ee551e0bSGreg Roach } 47*ee551e0bSGreg Roach 48d97083feSGreg Roach /** 49d97083feSGreg Roach * Convert a datetime to the user's Julian day number. 50d97083feSGreg Roach * 51d97083feSGreg Roach * @return int 52d97083feSGreg Roach */ 53d97083feSGreg Roach public function julianDay(): int 54d97083feSGreg Roach { 55d97083feSGreg Roach return gregoriantojd($this->carbon->month, $this->carbon->day, $this->carbon->year); 56d97083feSGreg Roach } 57d97083feSGreg Roach 58d97083feSGreg Roach /** 59d97083feSGreg Roach * @return string 60d97083feSGreg Roach */ 61d97083feSGreg Roach public function diffForHumans(): string 62d97083feSGreg Roach { 63d97083feSGreg Roach return $this->carbon->diffForHumans(); 64d97083feSGreg Roach } 65d97083feSGreg Roach 66d97083feSGreg Roach /** 67d97083feSGreg Roach * @param string $format 68d97083feSGreg Roach * 69d97083feSGreg Roach * @return string 70d97083feSGreg Roach */ 71d97083feSGreg Roach public function format(string $format): string 72d97083feSGreg Roach { 73d97083feSGreg Roach return $this->carbon->format($format); 74d97083feSGreg Roach } 75d97083feSGreg Roach 76d97083feSGreg Roach /** 77d97083feSGreg Roach * @param string $format 78d97083feSGreg Roach * 79d97083feSGreg Roach * @return string 80d97083feSGreg Roach */ 81d97083feSGreg Roach public function isoFormat(string $format): string 82d97083feSGreg Roach { 83d97083feSGreg Roach return $this->carbon->isoFormat($format); 84d97083feSGreg Roach } 85d97083feSGreg Roach 86d97083feSGreg Roach /** 87d97083feSGreg Roach * @return string 88d97083feSGreg Roach */ 89d97083feSGreg Roach public function toDateString(): string 90d97083feSGreg Roach { 91d97083feSGreg Roach return $this->carbon->format('Y-m-d'); 92d97083feSGreg Roach } 93d97083feSGreg Roach 94d97083feSGreg Roach /** 95d97083feSGreg Roach * @return string 96d97083feSGreg Roach */ 97d97083feSGreg Roach public function toDateTimeString(): string 98d97083feSGreg Roach { 99d97083feSGreg Roach return $this->carbon->format('Y-m-d H:i:s'); 100d97083feSGreg Roach } 101d97083feSGreg Roach 102d97083feSGreg Roach /** 103d97083feSGreg Roach * @param TimestampInterface $datetime 104d97083feSGreg Roach * 105d97083feSGreg Roach * @return int 106d97083feSGreg Roach */ 107d97083feSGreg Roach public function compare(TimestampInterface $datetime): int 108d97083feSGreg Roach { 109*ee551e0bSGreg Roach if ($this->carbon->lt($datetime->carbon)) { 110d97083feSGreg Roach return -1; 111d97083feSGreg Roach } 112d97083feSGreg Roach 113*ee551e0bSGreg Roach if ($this->carbon->gt($datetime->carbon)) { 114d97083feSGreg Roach return 1; 115d97083feSGreg Roach } 116d97083feSGreg Roach 117d97083feSGreg Roach return 0; 118d97083feSGreg Roach } 119d97083feSGreg Roach 120d97083feSGreg Roach /** 121d97083feSGreg Roach * @param int $seconds 122d97083feSGreg Roach * 1236612c384SGreg Roach * @return self 124d97083feSGreg Roach */ 1257761cf05SGreg Roach public function addSeconds(int $seconds): TimestampInterface 126d97083feSGreg Roach { 127d97083feSGreg Roach $clone = clone($this); 128d97083feSGreg Roach 12977961529Smiqrogroove $clone->carbon->addSeconds($seconds); 130d97083feSGreg Roach 131d97083feSGreg Roach return $clone; 132d97083feSGreg Roach } 133d97083feSGreg Roach 134d97083feSGreg Roach /** 135d97083feSGreg Roach * @param int $minutes 136d97083feSGreg Roach * 1376612c384SGreg Roach * @return self 138d97083feSGreg Roach */ 1397761cf05SGreg Roach public function addMinutes(int $minutes): TimestampInterface 140d97083feSGreg Roach { 141d97083feSGreg Roach $clone = clone($this); 142d97083feSGreg Roach 14377961529Smiqrogroove $clone->carbon->addMinutes($minutes); 144d97083feSGreg Roach 145d97083feSGreg Roach return $this; 146d97083feSGreg Roach } 147d97083feSGreg Roach 148d97083feSGreg Roach /** 149d97083feSGreg Roach * @param int $hours 150d97083feSGreg Roach * 1516612c384SGreg Roach * @return self 152d97083feSGreg Roach */ 1537761cf05SGreg Roach public function addHours(int $hours): TimestampInterface 154d97083feSGreg Roach { 155d97083feSGreg Roach $clone = clone($this); 156d97083feSGreg Roach 15777961529Smiqrogroove $clone->carbon->addHours($hours); 158d97083feSGreg Roach 159d97083feSGreg Roach return $clone; 160d97083feSGreg Roach } 161d97083feSGreg Roach 162d97083feSGreg Roach /** 163d97083feSGreg Roach * @param int $days 164d97083feSGreg Roach * 1656612c384SGreg Roach * @return self 166d97083feSGreg Roach */ 1677761cf05SGreg Roach public function addDays(int $days): TimestampInterface 168d97083feSGreg Roach { 169d97083feSGreg Roach $clone = clone($this); 170d97083feSGreg Roach 17177961529Smiqrogroove $clone->carbon->addDays($days); 172d97083feSGreg Roach 173d97083feSGreg Roach return $clone; 174d97083feSGreg Roach } 175d97083feSGreg Roach 176d97083feSGreg Roach /** 17777961529Smiqrogroove * Add to the month portion of the date. 17877961529Smiqrogroove * 17977961529Smiqrogroove * Allows overflow, consistent with v2.1.0 ... 2023-10-31 plus 1 month = 2023-12-01. 18077961529Smiqrogroove * 181d97083feSGreg Roach * @param int $months 182d97083feSGreg Roach * 1836612c384SGreg Roach * @return self 184d97083feSGreg Roach */ 1857761cf05SGreg Roach public function addMonths(int $months): TimestampInterface 186d97083feSGreg Roach { 187d97083feSGreg Roach $clone = clone($this); 188d97083feSGreg Roach 18977961529Smiqrogroove $clone->carbon->addMonths($months); 190d97083feSGreg Roach 191d97083feSGreg Roach return $clone; 192d97083feSGreg Roach } 193d97083feSGreg Roach 194d97083feSGreg Roach /** 19577961529Smiqrogroove * Add to the year portion of the date. 19677961529Smiqrogroove * 19777961529Smiqrogroove * Allows overflow, consistent with v2.1.0 ... 2024-02-29 plus 1 year = 2025-03-01. 19877961529Smiqrogroove * 199d97083feSGreg Roach * @param int $years 200d97083feSGreg Roach * 2016612c384SGreg Roach * @return self 202d97083feSGreg Roach */ 2037761cf05SGreg Roach public function addYears(int $years): TimestampInterface 204d97083feSGreg Roach { 205d97083feSGreg Roach $clone = clone($this); 206d97083feSGreg Roach 20777961529Smiqrogroove $clone->carbon->addYears($years); 208d97083feSGreg Roach 209d97083feSGreg Roach return $clone; 210d97083feSGreg Roach } 211d97083feSGreg Roach 212d97083feSGreg Roach /** 213d97083feSGreg Roach * @param int $seconds 214d97083feSGreg Roach * 2156612c384SGreg Roach * @return self 216d97083feSGreg Roach */ 2177761cf05SGreg Roach public function subtractSeconds(int $seconds): TimestampInterface 218d97083feSGreg Roach { 219d97083feSGreg Roach $clone = clone($this); 220d97083feSGreg Roach 22177961529Smiqrogroove $clone->carbon->subSeconds($seconds); 222d97083feSGreg Roach 223d97083feSGreg Roach return $clone; 224d97083feSGreg Roach } 225d97083feSGreg Roach 226d97083feSGreg Roach /** 227d97083feSGreg Roach * @param int $minutes 228d97083feSGreg Roach * 2296612c384SGreg Roach * @return self 230d97083feSGreg Roach */ 2317761cf05SGreg Roach public function subtractMinutes(int $minutes): TimestampInterface 232d97083feSGreg Roach { 233d97083feSGreg Roach $clone = clone($this); 234d97083feSGreg Roach 23577961529Smiqrogroove $clone->carbon->subMinutes($minutes); 236d97083feSGreg Roach 23777961529Smiqrogroove return $this; 238d97083feSGreg Roach } 239d97083feSGreg Roach 240d97083feSGreg Roach /** 241d97083feSGreg Roach * @param int $hours 242d97083feSGreg Roach * 2436612c384SGreg Roach * @return self 244d97083feSGreg Roach */ 2457761cf05SGreg Roach public function subtractHours(int $hours): TimestampInterface 246d97083feSGreg Roach { 247d97083feSGreg Roach $clone = clone($this); 248d97083feSGreg Roach 24977961529Smiqrogroove $clone->carbon->subHours($hours); 250d97083feSGreg Roach 251d97083feSGreg Roach return $clone; 252d97083feSGreg Roach } 253d97083feSGreg Roach 254d97083feSGreg Roach /** 255d97083feSGreg Roach * @param int $days 256d97083feSGreg Roach * 2576612c384SGreg Roach * @return self 258d97083feSGreg Roach */ 2597761cf05SGreg Roach public function subtractDays(int $days): TimestampInterface 260d97083feSGreg Roach { 261d97083feSGreg Roach $clone = clone($this); 262d97083feSGreg Roach 26377961529Smiqrogroove $clone->carbon->subDays($days); 264d97083feSGreg Roach 265d97083feSGreg Roach return $clone; 266d97083feSGreg Roach } 267d97083feSGreg Roach 268d97083feSGreg Roach /** 26977961529Smiqrogroove * Subtract from the month portion of the date. 27077961529Smiqrogroove * 27177961529Smiqrogroove * Allows overflow, consistent with v2.1.0 ... 2023-10-31 minus 1 month = 2023-10-01. 27277961529Smiqrogroove * 273d97083feSGreg Roach * @param int $months 274d97083feSGreg Roach * 2756612c384SGreg Roach * @return self 276d97083feSGreg Roach */ 2777761cf05SGreg Roach public function subtractMonths(int $months): TimestampInterface 278d97083feSGreg Roach { 279d97083feSGreg Roach $clone = clone($this); 280d97083feSGreg Roach 28177961529Smiqrogroove $clone->carbon->subMonths($months); 282d97083feSGreg Roach 283d97083feSGreg Roach return $clone; 284d97083feSGreg Roach } 285d97083feSGreg Roach 286d97083feSGreg Roach /** 28777961529Smiqrogroove * Subtract from the year portion of the date. 28877961529Smiqrogroove * 28977961529Smiqrogroove * Allows overflow, consistent with v2.1.0 ... 2024-02-29 minus 1 year = 2023-03-01. 29077961529Smiqrogroove * 291d97083feSGreg Roach * @param int $years 292d97083feSGreg Roach * 2936612c384SGreg Roach * @return self 294d97083feSGreg Roach */ 2957761cf05SGreg Roach public function subtractYears(int $years): TimestampInterface 296d97083feSGreg Roach { 297d97083feSGreg Roach $clone = clone($this); 298d97083feSGreg Roach 29977961529Smiqrogroove $clone->carbon->subYears($years); 300d97083feSGreg Roach 301d97083feSGreg Roach return $clone; 302d97083feSGreg Roach } 303d97083feSGreg Roach 304d97083feSGreg Roach /** 305d97083feSGreg Roach * @return int 306d97083feSGreg Roach */ 307d97083feSGreg Roach public function timestamp(): int 308d97083feSGreg Roach { 309d97083feSGreg Roach return $this->carbon->getTimestamp(); 310d97083feSGreg Roach } 311d97083feSGreg Roach} 312