From 0bee706e515c9dbbe17dab6eeb6887b33a15f1c0 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 26 Jan 2018 19:29:06 -0500 Subject: [PATCH] Add Temporal::local() shorthand for Temporal::convert() --- include/conversation.php | 2 +- include/event.php | 32 +++++++++++++++---------------- mod/cal.php | 6 +++--- mod/contacts.php | 2 +- mod/events.php | 6 +++--- mod/message.php | 4 ++-- mod/ping.php | 2 +- mod/profiles.php | 2 +- src/Core/NotificationsManager.php | 8 ++++---- src/Object/Post.php | 4 ++-- src/Util/Temporal.php | 16 ++++++++++++++-- 11 files changed, 48 insertions(+), 36 deletions(-) diff --git a/include/conversation.php b/include/conversation.php index 755f247641..98cb1773b1 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -790,7 +790,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false) { 'categories' => $categories, 'folders' => $folders, 'text' => strip_tags($body_e), - 'localtime' => Temporal::convert($item['created'], date_default_timezone_get(), 'UTC', 'r'), + 'localtime' => Temporal::local($item['created'], 'r'), 'ago' => (($item['app']) ? L10n::t('%s from %s', relative_date($item['created']),$item['app']) : relative_date($item['created'])), 'location' => $location_e, 'indent' => '', diff --git a/include/event.php b/include/event.php index faa0f9b9f7..95be09cd79 100644 --- a/include/event.php +++ b/include/event.php @@ -28,13 +28,13 @@ function format_event_html($ev, $simple = false) { $event_start = day_translate( $ev['adjust'] ? - Temporal::convert($ev['start'], date_default_timezone_get(), 'UTC', $bd_format) + Temporal::local($ev['start'], $bd_format) : Temporal::utc($ev['start'], $bd_format) ); $event_end = day_translate( $ev['adjust'] ? - Temporal::convert($ev['finish'], date_default_timezone_get(), 'UTC', $bd_format) + Temporal::local($ev['finish'], $bd_format) : Temporal::utc($ev['finish'], $bd_format) ); @@ -200,8 +200,8 @@ function sort_by_date($a) { function ev_compare($a,$b) { - $date_a = (($a['adjust']) ? Temporal::convert($a['start'], date_default_timezone_get()) : $a['start']); - $date_b = (($b['adjust']) ? Temporal::convert($b['start'], date_default_timezone_get()) : $b['start']); + $date_a = (($a['adjust']) ? Temporal::local($a['start']) : $a['start']); + $date_b = (($b['adjust']) ? Temporal::local($b['start']) : $b['start']); if ($date_a === $date_b) { return strcasecmp($a['desc'], $b['desc']); @@ -595,15 +595,15 @@ function process_events($arr) { $fmt = L10n::t('l, F j'); if (count($arr)) { foreach ($arr as $rr) { - $j = (($rr['adjust']) ? Temporal::convert($rr['start'], date_default_timezone_get(), 'UTC', 'j') : Temporal::utc($rr['start'], 'j')); - $d = (($rr['adjust']) ? Temporal::convert($rr['start'], date_default_timezone_get(), 'UTC', $fmt) : Temporal::utc($rr['start'], $fmt)); + $j = (($rr['adjust']) ? Temporal::local($rr['start'], 'j') : Temporal::utc($rr['start'], 'j')); + $d = (($rr['adjust']) ? Temporal::local($rr['start'], $fmt) : Temporal::utc($rr['start'], $fmt)); $d = day_translate($d); - $start = (($rr['adjust']) ? Temporal::convert($rr['start'], date_default_timezone_get(), 'UTC', 'c') : Temporal::utc($rr['start'], 'c')); + $start = (($rr['adjust']) ? Temporal::local($rr['start'], 'c') : Temporal::utc($rr['start'], 'c')); if ($rr['nofinish']) { $end = null; } else { - $end = (($rr['adjust']) ? Temporal::convert($rr['finish'], date_default_timezone_get(), 'UTC', 'c') : Temporal::utc($rr['finish'], 'c')); + $end = (($rr['adjust']) ? Temporal::local($rr['finish'], 'c') : Temporal::utc($rr['finish'], 'c')); } $is_first = ($d !== $last_date); @@ -930,26 +930,26 @@ function format_event_item($item) { // Convert the time to different formats. $dtstart_dt = day_translate( $item['event-adjust'] ? - Temporal::convert($item['event-start'], date_default_timezone_get(), 'UTC', $dformat) + Temporal::local($item['event-start'], $dformat) : Temporal::utc($item['event-start'], $dformat) ); $dtstart_title = Temporal::utc($item['event-start'], $item['event-adjust'] ? Temporal::ATOM : 'Y-m-d\TH:i:s'); // Format: Jan till Dec. $month_short = day_short_translate( $item['event-adjust'] ? - Temporal::convert($item['event-start'], date_default_timezone_get(), 'UTC', 'M') + Temporal::local($item['event-start'], 'M') : Temporal::utc($item['event-start'], 'M') ); // Format: 1 till 31. $date_short = $item['event-adjust'] ? - Temporal::convert($item['event-start'], date_default_timezone_get(), 'UTC', 'j') + Temporal::local($item['event-start'], 'j') : Temporal::utc($item['event-start'], 'j'); $start_time = $item['event-adjust'] ? - Temporal::convert($item['event-start'], date_default_timezone_get(), 'UTC', $tformat) + Temporal::local($item['event-start'], $tformat) : Temporal::utc($item['event-start'], $tformat); $start_short = day_short_translate( $item['event-adjust'] ? - Temporal::convert($item['event-start'], date_default_timezone_get(), 'UTC', $dformat_short) + Temporal::local($item['event-start'], $dformat_short) : Temporal::utc($item['event-start'], $dformat_short) ); @@ -958,17 +958,17 @@ function format_event_item($item) { $finish = true; $dtend_dt = day_translate( $item['event-adjust'] ? - Temporal::convert($item['event-finish'], date_default_timezone_get(), 'UTC', $dformat) + Temporal::local($item['event-finish'], $dformat) : Temporal::utc($item['event-finish'], $dformat) ); $dtend_title = Temporal::utc($item['event-finish'], $item['event-adjust'] ? Temporal::ATOM : 'Y-m-d\TH:i:s'); $end_short = day_short_translate( $item['event-adjust'] ? - Temporal::convert($item['event-finish'], date_default_timezone_get(), 'UTC', $dformat_short) + Temporal::local($item['event-finish'], $dformat_short) : Temporal::utc($item['event-finish'], $dformat_short) ); $end_time = $item['event-adjust'] ? - Temporal::convert($item['event-finish'], date_default_timezone_get(), 'UTC', $tformat) + Temporal::local($item['event-finish'], $tformat) : Temporal::utc($item['event-finish'], $tformat); // Check if start and finish time is at the same day. if (substr($dtstart_title, 0, 10) === substr($dtend_title, 0, 10)) { diff --git a/mod/cal.php b/mod/cal.php index d48ce1c5e8..22005c06ae 100644 --- a/mod/cal.php +++ b/mod/cal.php @@ -206,8 +206,8 @@ function cal_content(App $a) $start = Temporal::utc($start); $finish = Temporal::utc($finish); - $adjust_start = Temporal::convert($start, date_default_timezone_get()); - $adjust_finish = Temporal::convert($finish, date_default_timezone_get()); + $adjust_start = Temporal::local($start); + $adjust_finish = Temporal::local($finish); // put the event parametes in an array so we can better transmit them $event_params = [ @@ -231,7 +231,7 @@ function cal_content(App $a) if (DBM::is_result($r)) { $r = sort_by_date($r); foreach ($r as $rr) { - $j = $rr['adjust'] ? Temporal::convert($rr['start'], date_default_timezone_get(), 'UTC', 'j') : Temporal::utc($rr['start'], 'j'); + $j = $rr['adjust'] ? Temporal::local($rr['start'], 'j') : Temporal::utc($rr['start'], 'j'); if (!x($links, $j)) { $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j; } diff --git a/mod/contacts.php b/mod/contacts.php index 6bd6f753ff..81f1a6cafd 100644 --- a/mod/contacts.php +++ b/mod/contacts.php @@ -540,7 +540,7 @@ function contacts_content(App $a) $insecure = L10n::t('Private communications are not available for this contact.'); - $last_update = (($contact['last-update'] <= NULL_DATE) ? L10n::t('Never') : Temporal::convert($contact['last-update'], date_default_timezone_get(), 'UTC', 'D, j M Y, g:i A')); + $last_update = (($contact['last-update'] <= NULL_DATE) ? L10n::t('Never') : Temporal::local($contact['last-update'], 'D, j M Y, g:i A')); if ($contact['last-update'] > NULL_DATE) { $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? L10n::t("\x28Update was successful\x29") : L10n::t("\x28Update was not successful\x29")); diff --git a/mod/events.php b/mod/events.php index 028e23e51f..1d61dfcc33 100644 --- a/mod/events.php +++ b/mod/events.php @@ -326,8 +326,8 @@ function events_content(App $a) { $start = Temporal::utc($start); $finish = Temporal::utc($finish); - $adjust_start = Temporal::convert($start, date_default_timezone_get()); - $adjust_finish = Temporal::convert($finish, date_default_timezone_get()); + $adjust_start = Temporal::local($start); + $adjust_finish = Temporal::local($finish); // put the event parametes in an array so we can better transmit them $event_params = [ @@ -351,7 +351,7 @@ function events_content(App $a) { if (DBM::is_result($r)) { $r = sort_by_date($r); foreach ($r as $rr) { - $j = $rr['adjust'] ? Temporal::convert($rr['start'], date_default_timezone_get(), 'UTC', 'j') : Temporal::utc($rr['start'], 'j'); + $j = $rr['adjust'] ? Temporal::local($rr['start'], 'j') : Temporal::utc($rr['start'], 'j'); if (! x($links,$j)) { $links[$j] = System::baseUrl() . '/' . $a->cmd . '#link-' . $j; } diff --git a/mod/message.php b/mod/message.php index c1b2d989e9..5f1efe5ba3 100644 --- a/mod/message.php +++ b/mod/message.php @@ -397,7 +397,7 @@ function message_content(App $a) 'body' => $body_e, 'delete' => L10n::t('Delete message'), 'to_name' => $to_name_e, - 'date' => Temporal::convert($message['created'], date_default_timezone_get(), 'UTC', 'D, d M Y - g:i A'), + 'date' => Temporal::local($message['created'], 'D, d M Y - g:i A'), 'ago' => relative_date($message['created']), ]; @@ -498,7 +498,7 @@ function render_messages(array $msg, $t) '$delete' => L10n::t('Delete conversation'), '$body' => $body_e, '$to_name' => $to_name_e, - '$date' => Temporal::convert($rr['mailcreated'], date_default_timezone_get(), 'UTC', L10n::t('D, d M Y - g:i A')), + '$date' => Temporal::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')), '$ago' => relative_date($rr['mailcreated']), '$seen' => $rr['mailseen'], '$count' => L10n::tt('%d message', '%d messages', $rr['count']), diff --git a/mod/ping.php b/mod/ping.php index d26fc61bef..56d5fe290d 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -362,7 +362,7 @@ function ping_init(App $a) $notif['photo'] = proxy_url($notif['photo'], false, PROXY_SIZE_MICRO); } - $local_time = Temporal::convert($notif['date'], date_default_timezone_get()); + $local_time = Temporal::local($notif['date']); $notifications[] = [ 'id' => $notif['id'], diff --git a/mod/profiles.php b/mod/profiles.php index 6dfc61f0a6..25ce15ce8b 100644 --- a/mod/profiles.php +++ b/mod/profiles.php @@ -725,7 +725,7 @@ function profiles_content(App $a) { '$gender' => ContactSelector::gender($r[0]['gender']), '$marital' => ContactSelector::maritalStatus($r[0]['marital']), '$with' => ['with', L10n::t("Who: \x28if applicable\x29"), strip_tags($r[0]['with']), L10n::t('Examples: cathy123, Cathy Williams, cathy@example.com')], - '$howlong' => ['howlong', L10n::t('Since [date]:'), ($r[0]['howlong'] <= NULL_DATE ? '' : Temporal::convert($r[0]['howlong'], date_default_timezone_get()))], + '$howlong' => ['howlong', L10n::t('Since [date]:'), ($r[0]['howlong'] <= NULL_DATE ? '' : Temporal::local($r[0]['howlong']))], '$sexual' => ContactSelector::sexualPreference($r[0]['sexual']), '$about' => ['about', L10n::t('Tell us about yourself...'), $r[0]['about']], '$xmpp' => ['xmpp', L10n::t("XMPP \x28Jabber\x29 address:"), $r[0]['xmpp'], L10n::t("The XMPP address will be propagated to your contacts so that they can follow you.")], diff --git a/src/Core/NotificationsManager.php b/src/Core/NotificationsManager.php index 8853c16d53..a28dfbb064 100644 --- a/src/Core/NotificationsManager.php +++ b/src/Core/NotificationsManager.php @@ -43,7 +43,7 @@ class NotificationsManager extends BaseObject { $rets = []; foreach ($notes as $n) { - $local_time = Temporal::convert($n['date'], date_default_timezone_get()); + $local_time = Temporal::local($n['date']); $n['timestamp'] = strtotime($local_time); $n['date_rel'] = relative_date($n['date']); $n['msg_html'] = bbcode($n['msg'], false, false, false, false); @@ -244,7 +244,7 @@ class NotificationsManager extends BaseObject $default_item_image = proxy_url($it['photo'], false, PROXY_SIZE_MICRO); $default_item_url = $it['url']; $default_item_text = strip_tags(bbcode($it['msg'])); - $default_item_when = Temporal::convert($it['date'], date_default_timezone_get(), 'UTC', 'r'); + $default_item_when = Temporal::local($it['date'], 'r'); $default_item_ago = relative_date($it['date']); break; @@ -254,7 +254,7 @@ class NotificationsManager extends BaseObject $default_item_image = proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO); $default_item_url = $it['author-link']; $default_item_text = L10n::t("%s commented on %s's post", $it['author-name'], $it['pname']); - $default_item_when = Temporal::convert($it['created'], date_default_timezone_get(), 'UTC', 'r'); + $default_item_when = Temporal::local($it['created'], 'r'); $default_item_ago = relative_date($it['created']); break; @@ -266,7 +266,7 @@ class NotificationsManager extends BaseObject $default_item_text = (($it['id'] == $it['parent']) ? L10n::t("%s created a new post", $it['author-name']) : L10n::t("%s commented on %s's post", $it['author-name'], $it['pname'])); - $default_item_when = Temporal::convert($it['created'], date_default_timezone_get(), 'UTC', 'r'); + $default_item_when = Temporal::local($it['created'], 'r'); $default_item_ago = relative_date($it['created']); } diff --git a/src/Object/Post.php b/src/Object/Post.php index be3018cc94..4e83d2ff97 100644 --- a/src/Object/Post.php +++ b/src/Object/Post.php @@ -124,7 +124,7 @@ class Post extends BaseObject if (strtotime($item['edited']) - strtotime($item['created']) > 1) { $edited = [ 'label' => L10n::t('This entry was edited'), - 'date' => Temporal::convert($item['edited'], date_default_timezone_get(), 'UTC', 'r'), + 'date' => Temporal::local($item['edited'], 'r'), 'relative' => relative_date($item['edited']) ]; } @@ -364,7 +364,7 @@ class Post extends BaseObject 'osparkle' => $osparkle, 'sparkle' => $sparkle, 'title' => $title_e, - 'localtime' => Temporal::convert($item['created'], date_default_timezone_get(), 'UTC', 'r'), + 'localtime' => Temporal::local($item['created'], 'r'), 'ago' => $item['app'] ? L10n::t('%s from %s', relative_date($item['created']), $item['app']) : relative_date($item['created']), 'app' => $item['app'], 'created' => relative_date($item['created']), diff --git a/src/Util/Temporal.php b/src/Util/Temporal.php index 8deadd5e8f..0e8c8f18cf 100644 --- a/src/Util/Temporal.php +++ b/src/Util/Temporal.php @@ -135,6 +135,18 @@ class Temporal return self::convert($time, 'UTC', 'UTC', $format); } + /** + * convert() shorthand for local. + * + * @param string $time A date/time string + * @param string $format DateTime format string or Temporal constant + * @return string + */ + public static function local($time, $format = self::MYSQL) + { + return self::convert($time, date_default_timezone_get(), 'UTC', $format); + } + /** * convert() shorthand for timezoned now. * @@ -154,7 +166,7 @@ class Temporal */ public static function localNow($format = self::MYSQL) { - return self::convert('now', date_default_timezone_get(), 'UTC', $format); + return self::local('now', $format); } /** @@ -165,7 +177,7 @@ class Temporal */ public static function utcNow($format = self::MYSQL) { - return self::convert('now', 'UTC', 'UTC', $format); + return self::utc('now', $format); } /**