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\Factories; 21d97083feSGreg Roach 22d97083feSGreg Roachuse Fisharebest\Webtrees\Auth; 23d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampFactoryInterface; 24d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface; 25d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 26d97083feSGreg Roachuse Fisharebest\Webtrees\I18N; 27d97083feSGreg Roachuse Fisharebest\Webtrees\Site; 28d97083feSGreg Roachuse Fisharebest\Webtrees\Timestamp; 29dd750643SGreg Roachuse InvalidArgumentException; 30d97083feSGreg Roach 31d97083feSGreg Roachuse function date; 32d97083feSGreg Roachuse function date_create_from_format; 33d97083feSGreg Roachuse function time; 34d97083feSGreg Roach 35d97083feSGreg Roach/** 36d97083feSGreg Roach * Create a timestamp object. 37d97083feSGreg Roach */ 38d97083feSGreg Roachclass TimestampFactory implements TimestampFactoryInterface 39d97083feSGreg Roach{ 40d97083feSGreg Roach /** 41d97083feSGreg Roach * @param int $timestamp 42d97083feSGreg Roach * @param UserInterface|null $user 43d97083feSGreg Roach * 44d97083feSGreg Roach * @return TimestampInterface 45d97083feSGreg Roach */ 462c6f1bd5SGreg Roach public function make(int $timestamp, UserInterface|null $user = null): TimestampInterface 47d97083feSGreg Roach { 48d97083feSGreg Roach $user ??= Auth::user(); 49d97083feSGreg Roach $timezone = $user->getPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE')); 50d97083feSGreg Roach $locale = I18N::locale()->code(); 51d97083feSGreg Roach 52d97083feSGreg Roach return new Timestamp($timestamp, $timezone, $locale); 53d97083feSGreg Roach } 54d97083feSGreg Roach 55d97083feSGreg Roach /** 56d97083feSGreg Roach * @param string|null $string YYYY-MM-DD HH:MM:SS (as provided by SQL). 57d97083feSGreg Roach * @param string $format 58d97083feSGreg Roach * @param UserInterface|null $user 59d97083feSGreg Roach * 60d97083feSGreg Roach * @return TimestampInterface 61d97083feSGreg Roach */ 62*1ff45046SGreg Roach public function fromString(string|null $string, string $format = 'Y-m-d H:i:s', UserInterface|null $user = null): TimestampInterface 63d97083feSGreg Roach { 64d97083feSGreg Roach $string ??= date($format); 65d97083feSGreg Roach $timestamp = date_create_from_format($format, $string); 66d97083feSGreg Roach 67d97083feSGreg Roach if ($timestamp === false) { 68dd750643SGreg Roach throw new InvalidArgumentException('date/time "' . $string . '" does not match pattern "' . $format . '"'); 69d97083feSGreg Roach } 70d97083feSGreg Roach 71d97083feSGreg Roach return $this->make($timestamp->getTimestamp(), $user); 72d97083feSGreg Roach } 73d97083feSGreg Roach 74d97083feSGreg Roach /** 75d97083feSGreg Roach * @param UserInterface|null $user 76d97083feSGreg Roach * 77d97083feSGreg Roach * @return TimestampInterface 78d97083feSGreg Roach */ 792c6f1bd5SGreg Roach public function now(UserInterface|null $user = null): TimestampInterface 80d97083feSGreg Roach { 81d97083feSGreg Roach return $this->make(time(), $user); 82d97083feSGreg Roach } 83d97083feSGreg Roach} 84