X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FTemporal.php;h=ec71ab5e963e4604f7c8c072ff1c11295f7bc3b7;hb=cc8a6c85ae939c0fc9258d7a11276bc7c2e59bc0;hp=8c465b4b66cd158e0df7f2bb782c3de9a8498ceb;hpb=b85490515016aad50e0fe73c1174949a1f72d3c3;p=friendica.git diff --git a/src/Util/Temporal.php b/src/Util/Temporal.php index 8c465b4b66..ec71ab5e96 100644 --- a/src/Util/Temporal.php +++ b/src/Util/Temporal.php @@ -8,21 +8,16 @@ namespace Friendica\Util; use DateTime; use DateTimeZone; -use Exception; -use Friendica\Core\Config; use Friendica\Core\L10n; use Friendica\Core\PConfig; - -require_once 'boot.php'; -require_once 'include/text.php'; +use Friendica\Core\Renderer; +use Friendica\Database\DBA; /** * @brief Temporal class */ class Temporal { - const ATOM = 'Y-m-d\TH:i:s\Z'; - /** * @brief Two-level sort for timezones. * @@ -62,7 +57,7 @@ class Temporal $o = '', '', $options); $options = str_replace('', '', $options); - $tpl = get_markup_template('field_select_raw.tpl'); - return replace_macros($tpl, [ + $tpl = Renderer::getMarkupTemplate('field_select_raw.tpl'); + return Renderer::replaceMacros($tpl, [ '$field' => [$name, $label, $current, $help, $options], ]); } - /** - * @brief General purpose date parse/convert function. - * - * @param string $s Some parseable date/time string - * @param string $from Source timezone - * @param string $to Dest timezone - * @param string $fmt Output format recognised from php's DateTime class - * http://www.php.net/manual/en/datetime.format.php - * - * @return string Formatted date according to given format - */ - public static function convert($s = 'now', $to = 'UTC', $from = 'UTC', $fmt = "Y-m-d H:i:s") - { - // Defaults to UTC if nothing is set, but throws an exception if set to empty string. - // Provide some sane defaults regardless. - if ($from === '') { - $from = 'UTC'; - } - - if ($to === '') { - $to = 'UTC'; - } - - if (($s === '') || (!is_string($s))) { - $s = 'now'; - } - - /* - * Slight hackish adjustment so that 'zero' datetime actually returns what is intended - * otherwise we end up with -0001-11-30 ... - * add 32 days so that we at least get year 00, and then hack around the fact that - * months and days always start with 1. - */ - - if (substr($s, 0, 10) <= '0001-01-01') { - $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC')); - return str_replace('1', '0', $d->format($fmt)); - } - - try { - $from_obj = new DateTimeZone($from); - } catch (Exception $e) { - $from_obj = new DateTimeZone('UTC'); - } - - try { - $d = new DateTime($s, $from_obj); - } catch (Exception $e) { - logger('datetime_convert: exception: ' . $e->getMessage()); - $d = new DateTime('now', $from_obj); - } - - try { - $to_obj = new DateTimeZone($to); - } catch (Exception $e) { - $to_obj = new DateTimeZone('UTC'); - } - - $d->setTimeZone($to_obj); - - return $d->format($fmt); - } - /** * @brief Wrapper for date selector, tailored for use in birthday fields. * * @param string $dob Date of Birth * @return string Formatted HTML + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function getDateofBirthField($dob) { + $a = \get_app(); + list($year, $month, $day) = sscanf($dob, '%4d-%2d-%2d'); if ($dob < '0000-01-01') { $value = ''; } else { - $value = Temporal::convert(($year > 1000) ? $dob : '1000-' . $month . '-' . $day, 'UTC', 'UTC', 'Y-m-d'); + $value = DateTimeFormat::utc(($year > 1000) ? $dob : '1000-' . $month . '-' . $day, 'Y-m-d'); } - $age = (intval($value) ? age($value, $a->user["timezone"], $a->user["timezone"]) : ""); + $age = (intval($value) ? self::getAgeByTimezone($value, $a->user["timezone"], $a->user["timezone"]) : ""); - $tpl = get_markup_template("field_input.tpl"); - $o = replace_macros($tpl, + $tpl = Renderer::getMarkupTemplate("field_input.tpl"); + $o = Renderer::replaceMacros($tpl, [ '$field' => [ 'dob', @@ -222,16 +158,17 @@ class Temporal /** * @brief Returns a date selector * - * @param string $min Unix timestamp of minimum date - * @param string $max Unix timestap of maximum date - * @param string $default Unix timestamp of default date - * @param string $id ID and name of datetimepicker (defaults to "datetimepicker") + * @param DateTime $min Minimum date + * @param DateTime $max Maximum date + * @param DateTime $default Default date + * @param string $id ID and name of datetimepicker (defaults to "datetimepicker") * * @return string Parsed HTML output. + * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ public static function getDateField($min, $max, $default, $id = 'datepicker') { - return datetimesel($min, $max, $default, '', $id, true, false, '', ''); + return self::getDateTimeField($min, $max, $default, '', $id, true, false, '', ''); } /** @@ -242,43 +179,55 @@ class Temporal * @param string $id ID and name of datetimepicker (defaults to "timepicker") * * @return string Parsed HTML output. + * @throws \Exception */ public static function getTimeField($h, $m, $id = 'timepicker') { - return datetimesel(new DateTime(), new DateTime(), new DateTime("$h:$m"), '', $id, false, true); + return self::getDateTimeField(new DateTime(), new DateTime(), new DateTime("$h:$m"), '', $id, false, true); } /** * @brief Returns a datetime selector. * - * @param string $min Unix timestamp of minimum date - * @param string $max Unix timestamp of maximum date - * @param string $default Unix timestamp of default date - * @param string $id Id and name of datetimepicker (defaults to "datetimepicker") - * @param bool $pickdate true to show date picker (default) - * @param bool $picktime true to show time picker (default) - * @param string $minfrom set minimum date from picker with id $minfrom (none by default) - * @param string $maxfrom set maximum date from picker with id $maxfrom (none by default) - * @param bool $required default false + * @param DateTime $minDate Minimum date + * @param DateTime $maxDate Maximum date + * @param DateTime $defaultDate Default date + * @param $label + * @param string $id Id and name of datetimepicker (defaults to "datetimepicker") + * @param bool $pickdate true to show date picker (default) + * @param bool $picktime true to show time picker (default) + * @param string $minfrom set minimum date from picker with id $minfrom (none by default) + * @param string $maxfrom set maximum date from picker with id $maxfrom (none by default) + * @param bool $required default false * * @return string Parsed HTML output. * - * @todo Once browser support is better this could probably be replaced with + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @todo Once browser support is better this could probably be replaced with * native HTML5 date picker. */ - public static function getDateTimeField($min, $max, $default, $label, $id = 'datetimepicker', $pickdate = true, - $picktime = true, $minfrom = '', $maxfrom = '', $required = false) + public static function getDateTimeField( + DateTime $minDate, + DateTime $maxDate, + DateTime $defaultDate, + $label, + $id = 'datetimepicker', + $pickdate = true, + $picktime = true, + $minfrom = '', + $maxfrom = '', + $required = false) { // First day of the week (0 = Sunday) $firstDay = PConfig::get(local_user(), 'system', 'first_day_of_week', 0); - $lang = substr(L10n::getBrowserLanguage(), 0, 2); + $lang = substr(L10n::getCurrentLang(), 0, 2); // Check if the detected language is supported by the picker if (!in_array($lang, ["ar", "ro", "id", "bg", "fa", "ru", "uk", "en", "el", "de", "nl", "tr", "fr", "es", "th", "pl", "pt", "ch", "se", "kr", "it", "da", "no", "ja", "vi", "sl", "cs", "hu"])) { - $lang = Config::get('system', 'language', 'en'); + $lang = 'en'; } $o = ''; @@ -296,41 +245,12 @@ class Temporal $dateformat .= 'H:i'; } - $minjs = $min ? ",minDate: new Date({$min->getTimestamp()}*1000), yearStart: " . $min->format('Y') : ''; - $maxjs = $max ? ",maxDate: new Date({$max->getTimestamp()}*1000), yearEnd: " . $max->format('Y') : ''; - - $input_text = $default ? date($dateformat, $default->getTimestamp()) : ''; - $defaultdatejs = $default ? ",defaultDate: new Date({$default->getTimestamp()}*1000)" : ''; + $input_text = $defaultDate ? date($dateformat, $defaultDate->getTimestamp()) : ''; - $pickers = ''; - if (!$pickdate) { - $pickers .= ', datepicker: false'; - } + $readable_format = str_replace(['Y', 'm', 'd', 'H', 'i'], ['yyyy', 'mm', 'dd', 'HH', 'MM'], $dateformat); - if (!$picktime) { - $pickers .= ',timepicker: false'; - } - - $extra_js = ''; - $pickers .= ",dayOfWeekStart: " . $firstDay . ",lang:'" . $lang . "'"; - if ($minfrom != '') { - $extra_js .= "\$('#id_$minfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#id_$id').data('xdsoft_datetimepicker').setOptions({minDate: currentDateTime})}})"; - } - - if ($maxfrom != '') { - $extra_js .= "\$('#id_$maxfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#id_$id').data('xdsoft_datetimepicker').setOptions({maxDate: currentDateTime})}})"; - } - - $readable_format = $dateformat; - $readable_format = str_replace('Y', 'yyyy', $readable_format); - $readable_format = str_replace('m', 'mm', $readable_format); - $readable_format = str_replace('d', 'dd', $readable_format); - $readable_format = str_replace('H', 'HH', $readable_format); - $readable_format = str_replace('i', 'MM', $readable_format); - - $tpl = get_markup_template('field_input.tpl'); - $o .= replace_macros($tpl, - [ + $tpl = Renderer::getMarkupTemplate('field_datetime.tpl'); + $o .= Renderer::replaceMacros($tpl, [ '$field' => [ $id, $label, @@ -339,12 +259,18 @@ class Temporal $required ? '*' : '', 'placeholder="' . $readable_format . '"' ], + '$datetimepicker' => [ + 'minDate' => $minDate, + 'maxDate' => $maxDate, + 'defaultDate' => $defaultDate, + 'dateformat' => $dateformat, + 'firstDay' => $firstDay, + 'lang' => $lang, + 'minfrom' => $minfrom, + 'maxfrom' => $maxfrom, + ] ]); - $o .= ""; - return $o; } @@ -368,16 +294,22 @@ class Temporal $abs = strtotime($localtime); - if (is_null($posted_date) || $posted_date <= NULL_DATE || $abs === false) { + if (is_null($posted_date) || $posted_date <= DBA::NULL_DATETIME || $abs === false) { return L10n::t('never'); } + $isfuture = false; $etime = time() - $abs; - if ($etime < 1) { + if ($etime < 1 && $etime >= 0) { return L10n::t('less than a second ago'); } + if ($etime < 0){ + $etime = -$etime; + $isfuture = true; + } + $a = [12 * 30 * 24 * 60 * 60 => [L10n::t('year'), L10n::t('years')], 30 * 24 * 60 * 60 => [L10n::t('month'), L10n::t('months')], 7 * 24 * 60 * 60 => [L10n::t('week'), L10n::t('weeks')], @@ -393,7 +325,12 @@ class Temporal $r = round($d); // translators - e.g. 22 hours ago, 1 minute ago if (!$format) { - $format = L10n::t('%1$d %2$s ago'); + if($isfuture){ + $format = L10n::t('in %1$d %2$s'); + } + else { + $format = L10n::t('%1$d %2$s ago'); + } } return sprintf($format, $r, (($r == 1) ? $str[0] : $str[1])); @@ -416,11 +353,12 @@ class Temporal * and become a year older. If you wish me happy birthday on January 1 * (San Bruno time), you'll be a day late. * - * @param string $dob Date of Birth - * @param string $owner_tz (optional) Timezone of the person of interest + * @param string $dob Date of Birth + * @param string $owner_tz (optional) Timezone of the person of interest * @param string $viewer_tz (optional) Timezone of the person viewing * * @return int Age in years + * @throws \Exception */ public static function getAgeByTimezone($dob, $owner_tz = '', $viewer_tz = '') { @@ -434,11 +372,11 @@ class Temporal $viewer_tz = date_default_timezone_get(); } - $birthdate = Temporal::convert($dob . ' 00:00:00+00:00', $owner_tz, 'UTC', 'Y-m-d'); + $birthdate = DateTimeFormat::convert($dob . ' 00:00:00+00:00', $owner_tz, 'UTC', 'Y-m-d'); list($year, $month, $day) = explode("-", $birthdate); - $year_diff = Temporal::convert('now', $viewer_tz, 'UTC', 'Y') - $year; - $curr_month = Temporal::convert('now', $viewer_tz, 'UTC', 'm'); - $curr_day = Temporal::convert('now', $viewer_tz, 'UTC', 'd'); + $year_diff = DateTimeFormat::timezoneNow($viewer_tz, 'Y') - $year; + $curr_month = DateTimeFormat::timezoneNow($viewer_tz, 'm'); + $curr_day = DateTimeFormat::timezoneNow($viewer_tz, 'd'); if (($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day))) { $year_diff--; @@ -461,7 +399,6 @@ class Temporal public static function getDaysInMonth($y, $m) { return date('t', mktime(0, 0, 0, $m, 1, $y)); - ; } /** @@ -473,12 +410,13 @@ class Temporal * @param int $m Month (1=January, 12=December) * * @return string day 0 = Sunday through 6 = Saturday + * @throws \Exception */ - public static function getFirstDayInMonth($y, $m) + private static function getFirstDayInMonth($y, $m) { $d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m)); - return Temporal::convert($d, 'UTC', 'UTC', 'w'); + return DateTimeFormat::utc($d, 'w'); } /** @@ -489,14 +427,15 @@ class Temporal * altering td class. * Months count from 1. * - * @param int $y Year - * @param int $m Month + * @param int $y Year + * @param int $m Month * @param array $links (default null) * @param string $class * * @return string * - * @todo Provide (prev, next) links, define class variations for different size calendars + * @throws \Exception + * @todo Provide (prev, next) links, define class variations for different size calendars */ public static function getCalendarTable($y = 0, $m = 0, $links = null, $class = '') { @@ -508,8 +447,8 @@ class Temporal 'October', 'November', 'December' ]; - $thisyear = Temporal::convert('now', date_default_timezone_get(), 'UTC', 'Y'); - $thismonth = Temporal::convert('now', date_default_timezone_get(), 'UTC', 'm'); + $thisyear = DateTimeFormat::localNow('Y'); + $thismonth = DateTimeFormat::localNow('m'); if (!$y) { $y = $thisyear; } @@ -519,21 +458,21 @@ class Temporal } $dn = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; - $f = get_first_dim($y, $m); - $l = get_dim($y, $m); + $f = self::getFirstDayInMonth($y, $m); + $l = self::getDaysInMonth($y, $m); $d = 1; $dow = 0; $started = false; if (($y == $thisyear) && ($m == $thismonth)) { - $tddate = intval(Temporal::convert('now', date_default_timezone_get(), 'UTC', 'j')); + $tddate = intval(DateTimeFormat::localNow('j')); } - $str_month = day_translate($mtab[$m]); + $str_month = L10n::getDay($mtab[$m]); $o = ''; $o .= ""; for ($a = 0; $a < 7; $a ++) { - $o .= ''; + $o .= ''; } $o .= ''; @@ -547,7 +486,7 @@ class Temporal $o .= "
$str_month $y
' . mb_substr(day_translate($dn[$a]), 0, 3, 'UTF-8') . '' . mb_substr(L10n::getDay($dn[$a]), 0, 3, 'UTF-8') . '
"; $day = str_replace(' ', ' ', sprintf('%2.2d', $d)); if ($started) { - if (x($links, $d) !== false) { + if (isset($links[$d])) { $o .= "$day"; } else { $o .= $day;