Skip to content

Commit

Permalink
Merge pull request #8366 from Sesquipedalian/3.0/time
Browse files Browse the repository at this point in the history
Some minor improvements to SMF\Time
  • Loading branch information
Sesquipedalian authored Dec 21, 2024
2 parents cd2d0b9 + 6716cac commit 7a78137
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 101 deletions.
1 change: 1 addition & 0 deletions Languages/en_US/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@

$txt['today'] = '<strong>Today</strong> at ';
$txt['yesterday'] = '<strong>Yesterday</strong> at ';
$txt['tomorrow'] = '<strong>Tomorrow</strong> at ';
$txt['new_poll'] = 'New poll';
$txt['poll_question'] = 'Question';
$txt['poll_vote'] = 'Submit Vote';
Expand Down
4 changes: 2 additions & 2 deletions Languages/en_US/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,15 @@
$helptxt['titlesEnable'] = 'Switching Custom Titles on will allow members with the relevant permission to create a special title for themselves.
This will be shown underneath the name.<br><em>For example:</em><br>Jeff<br>Cool Guy';
$helptxt['onlineEnable'] = 'This will show an image to indicate whether the member is online or offline';
$helptxt['todayMod'] = 'This will show &quot;Today&quot; or &quot;Yesterday&quot; instead of the date.<br><br>
$helptxt['todayMod'] = 'This will show &quot;Yesterday&quot;, &quot;Today&quot;, or &quot;Tomorrow&quot; instead of the date.<br><br>
<strong>Examples:</strong><br><br>
<ul class="normallist">
<li>
<strong>Disabled</strong><br>
October 3, 2009 at 12:59:18 am</li>
<li><strong>Only Today</strong><br>
Today at 12:59:18 am</li>
<li><strong>Today &amp; Yesterday</strong><br>
<li><strong>Yesterday, Today, &amp; Tomorrow</strong><br>
Yesterday at 09:36:55 pm</li>
</ul>';
$helptxt['disableCustomPerPage'] = 'Check this setting to stop users from customizing the amount of messages and topics to display per page on the Message Index and Topic Display page respectively.';
Expand Down
2 changes: 1 addition & 1 deletion Languages/en_US/ManageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
$txt['todayMod'] = 'Enable shorthand date display';
$txt['today_disabled'] = 'Disabled';
$txt['today_only'] = 'Only Today';
$txt['yesterday_today'] = 'Today &amp; Yesterday';
$txt['yesterday_today'] = 'Yesterday, Today, &amp; Tomorrow';
$txt['onlineEnable'] = 'Show online/offline status in posts and PMs';
$txt['defaultMaxMembers'] = 'Members per page in member list';
$txt['timeLoadPageEnable'] = 'Display time taken to create every page';
Expand Down
2 changes: 1 addition & 1 deletion Sources/Actions/Admin/Bans.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ public function log(): void
],
'data' => [
'function' => function ($rowData) {
return Time::timeformat($rowData['log_time']);
return Time::stringFromUnix($rowData['log_time']);
},
],
'sort' => [
Expand Down
52 changes: 4 additions & 48 deletions Sources/Actions/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function show(): void

// Need a start date for all views
if (!empty($_REQUEST['start_date'])) {
$start_parsed = date_parse(str_replace(',', '', self::convertDateToEnglish($_REQUEST['start_date'])));
$start_parsed = date_parse(str_replace(',', '', Time::convertToEnglish($_REQUEST['start_date'])));

if (empty($start_parsed['error_count']) && empty($start_parsed['warning_count'])) {
$_REQUEST['year'] = $start_parsed['year'];
Expand All @@ -187,7 +187,7 @@ public function show(): void

// Need an end date for the list view
if (!empty($_REQUEST['end_date'])) {
$end_parsed = date_parse(str_replace(',', '', self::convertDateToEnglish($_REQUEST['end_date'])));
$end_parsed = date_parse(str_replace(',', '', Time::convertToEnglish($_REQUEST['end_date'])));

if (empty($end_parsed['error_count']) && empty($end_parsed['warning_count'])) {
$_REQUEST['end_year'] = $end_parsed['year'];
Expand Down Expand Up @@ -1486,7 +1486,7 @@ public static function validateEventPost(): void
if (!isset($_POST['deleteevent'])) {
// The 2.1 way
if (isset($_POST['start_date'])) {
$d = date_parse(str_replace(',', '', self::convertDateToEnglish($_POST['start_date'])));
$d = date_parse(str_replace(',', '', Time::convertToEnglish($_POST['start_date'])));

if (!empty($d['error_count']) || !empty($d['warning_count'])) {
ErrorHandler::fatalLang('invalid_date', false);
Expand All @@ -1500,7 +1500,7 @@ public static function validateEventPost(): void
ErrorHandler::fatalLang('event_month_missing', false);
}
} elseif (isset($_POST['start_datetime'])) {
$d = date_parse(str_replace(',', '', self::convertDateToEnglish($_POST['start_datetime'])));
$d = date_parse(str_replace(',', '', Time::convertToEnglish($_POST['start_datetime'])));

if (!empty($d['error_count']) || !empty($d['warning_count'])) {
ErrorHandler::fatalLang('invalid_date', false);
Expand Down Expand Up @@ -1624,50 +1624,6 @@ public static function removeHolidays(array $holiday_ids): void
}
}

/**
* Helper function to convert date string to english
* so that date_parse can parse the date
*
* @param string $date A localized date string
* @return string English date string
*/
public static function convertDateToEnglish(string $date): string
{
if (User::$me->language == 'english') {
return $date;
}

$replacements = array_combine(array_map('strtolower', Lang::$txt['months_titles']), [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December',
]);
$replacements += array_combine(array_map('strtolower', Lang::$txt['months_short']), [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
]);
$replacements += array_combine(array_map('strtolower', Lang::$txt['days']), [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
]);
$replacements += array_combine(array_map('strtolower', Lang::$txt['days_short']), [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat',
]);
// Find all possible variants of AM and PM for this language.
$replacements[strtolower(Lang::$txt['time_am'])] = 'AM';
$replacements[strtolower(Lang::$txt['time_pm'])] = 'PM';

if (($am = Time::strftime('%p', strtotime('01:00:00'))) !== 'p' && $am !== false) {
$replacements[strtolower($am)] = 'AM';
$replacements[strtolower(Time::strftime('%p', strtotime('23:00:00')))] = 'PM';
}

if (($am = Time::strftime('%P', strtotime('01:00:00'))) !== 'P' && $am !== false) {
$replacements[strtolower($am)] = 'AM';
$replacements[strtolower(Time::strftime('%P', strtotime('23:00:00')))] = 'PM';
}

return strtr(strtolower($date), $replacements);
}

/******************
* Internal methods
******************/
Expand Down
2 changes: 1 addition & 1 deletion Sources/Parsers/BBCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ class BBCodeParser extends Parser
'parameters' => [
'author' => ['match' => '([^<>]{1,192}?)'],
'link' => ['match' => '(?:board=\d+;)?((?:topic|threadid)=[\dmsg#\./]{1,40}(?:;start=[\dmsg#\./]{1,40})?|msg=\d+?|action=profile;u=\d+)'],
'date' => ['match' => '(\d+)', 'validate' => 'SMF\\Time::timeformat'],
'date' => ['match' => '(\d+)', 'validate' => 'SMF\\Time::stringFromUnix'],
],
'before' => '<blockquote><cite><a href="{scripturl}?{link}">{txt_quote_from}: {author} {txt_search_on} {date}</a></cite>',
'after' => '</blockquote>',
Expand Down
20 changes: 13 additions & 7 deletions Sources/Subs-Compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -1940,11 +1940,6 @@ function removeHolidays(array $holiday_ids): void
Actions\Calendar::removeHolidays($holiday_ids);
}

function convertDateToEnglish(string $date): string
{
return Actions\Calendar::convertDateToEnglish($date);
}

/**
* Begin
* Actions\CoppaForm
Expand Down Expand Up @@ -4764,13 +4759,24 @@ function get_date_or_time_format(string $type = '', string $format = '', ?bool $

function timeformat(int $log_time, bool|string $show_today = true, ?string $tzid = null): string
{
return SMF\Time::timeformat($log_time, $show_today, $tzid);
// For backward compatibility, replace empty values with the user's time
// zone and replace anything invalid with the forum's default time zone.
$tzid = empty($tzid) ? SMF\User::getTimezone() : (($tzid === 'forum' || @timezone_open((string) $tzid) === false) ? SMF\Config::$modSettings['default_timezone'] : $tzid);

$date = new SMF\Time('@' . $log_time, $tzid);

return is_bool($show_today) ? $date->format(null, $show_today) : $date->format($show_today);
}

function convertDateToEnglish(string $date): string
{
return SMF\Time::convertToEnglish($date);
}

/** @deprecated since 2.1 */
function forum_time(bool $use_user_offset = true, ?int $timestamp = null): int
{
return SMF\Time::forumTime($use_user_offset, $timestamp);
return !isset($timestamp) ? time() : (int) $timestamp;
}

/**
Expand Down
Loading

0 comments on commit 7a78137

Please sign in to comment.