From 97c2aa2bda7c25e89770b25f5810cc544272c08c Mon Sep 17 00:00:00 2001 From: Kjartan Mannes Date: Sun, 8 Feb 2004 21:42:59 +0000 Subject: - Modified format_date() to handle timezones properly. --- includes/common.inc | 48 +++++++++++++++++++++++++++++--------------- modules/node.module | 2 +- modules/node/node.module | 2 +- modules/system.module | 13 ++++++++++-- modules/system/system.module | 13 ++++++++++-- 5 files changed, 56 insertions(+), 22 deletions(-) diff --git a/includes/common.inc b/includes/common.inc index 6e4c5eb22..3f20e3f15 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -844,39 +844,55 @@ function format_interval($timestamp, $granularity = 2) { * should contain the format string * @param $format Format string (as required by the PHP date() function). * Only required if 'custom' date format is requested. + * @param $timezone Timezone offset in seconds in case the user timezone + * should not be used. * @return Translated date string in the requested format */ -function format_date($timestamp, $type = "medium", $format = "") { - global $user; +function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL) { + if ($timezone === NULL) { + global $user; + $timezone = $user->uid ? $user->timezone : variable_get('date_default_timezone', 0); + } - $timestamp += ($user->timezone) ? $user->timezone - date("Z") : 0; + $timestamp += $timezone; switch ($type) { - case "small": - $format = variable_get("date_format_short", "m/d/Y - H:i"); + case 'small': + $format = variable_get('date_format_short', 'm/d/Y - H:i'); break; - case "large": - $format = variable_get("date_format_long", "l, F j, Y - H:i"); + case 'large': + $format = variable_get('date_format_long', 'l, F j, Y - H:i'); break; - case "custom": + case 'custom': // No change to format break; - case "medium": + case 'medium': default: - $format = variable_get("date_format_medium", "D, m/d/Y - H:i"); + $format = variable_get('date_format_medium', 'D, m/d/Y - H:i'); } - for ($i = strlen($format); $i >= 0; $c = $format[--$i]) { - if (strstr("AaDFlM", $c)) { - $date = t(date($c, $timestamp)) . $date; + $max = strlen($format); + for ($i = 0; $i <= $max; $c = $format{$i++}) { + if (strpos('AaDFlM', $c)) { + $date .= gmdate($c, $timestamp); + } + else if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c)) { + $date .= gmdate($c, $timestamp); + } + else if ($c == 'r') { + $date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone); } - else if (strstr("BdgGhHiIjLmnOrsStTUwWYyZz", $c)) { - $date = date($c, $timestamp) . $date; + else if ($c == 'O') { + $date .= sprintf('%s%02d%02d', ($timezone < 0 ? '-' : '+'), abs($timezone / 3600), abs($timezone % 3600) / 60); + } + else if ($c == 'Z') { + $date .= $timezone; } else { - $date = $c.$date; + $date .= $c; } } + return $date; } diff --git a/modules/node.module b/modules/node.module index e5b9d29c5..5664702ab 100644 --- a/modules/node.module +++ b/modules/node.module @@ -1075,7 +1075,7 @@ function node_validate($node, &$error) { } if (!$node->date) { - $node->date = date('M j, Y g:i a', $node->created); + $node->date = format_date($node->created, 'custom', 'Y-m-d H:i O'); } if (!is_numeric($node->status)) { diff --git a/modules/node/node.module b/modules/node/node.module index e5b9d29c5..5664702ab 100644 --- a/modules/node/node.module +++ b/modules/node/node.module @@ -1075,7 +1075,7 @@ function node_validate($node, &$error) { } if (!$node->date) { - $node->date = date('M j, Y g:i a', $node->created); + $node->date = format_date($node->created, 'custom', 'Y-m-d H:i O'); } if (!is_numeric($node->status)) { diff --git a/modules/system.module b/modules/system.module index cf1d72d5c..b9a7918bb 100644 --- a/modules/system.module +++ b/modules/system.module @@ -86,10 +86,11 @@ function system_user($type, $edit, &$user) { $data[t('Theme settings')] = form_item(t("Theme"), "", t("Selecting a different theme will change the look and feel of the site.")); } + $timestamp = time(); $zonelist = array(-11, -10, -9.5, -9, -8, -7, -6, -5, -4, -3.5, -3, -2, -1, 0, 1, 2, 3, 3.5, 4, 5, 5.5, 5.75, 6, 6.5, 7, 8, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 14); foreach ($zonelist as $offset) { $zone = $offset * 3600; - $zones["$zone"] = date(variable_get("date_format_long", "l, F dS, Y - g:ia"), time() - date("Z") + $zone) . sprintf(" (GMT %s%02d:%02d)\n", ($offset >= 0) ? "+" : "-", floor(abs($offset)), (abs($offset) * 60) % 60); + $zones[$zone] = format_date($timestamp, 'custom', variable_get('date_format_long', $datelong[0]) . ' O', $zone); } $data[t('Locale settings')] = form_select(t("Time zone"), "timezone", $edit["timezone"], $zones, t("Select what time you currently have and your time zone settings will be set appropriate.")); return $data; @@ -141,6 +142,13 @@ function system_view_general() { // date settings: + $timestamp = time(); + $zonelist = array(-11, -10, -9.5, -9, -8, -7, -6, -5, -4, -3.5, -3, -2, -1, 0, 1, 2, 3, 3.5, 4, 5, 5.5, 5.75, 6, 6.5, 7, 8, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 14); + foreach ($zonelist as $offset) { + $zone = $offset * 3600; + $zones[$zone] = format_date($timestamp, 'custom', variable_get('date_format_long', $datelong[0]) . ' O', $zone); + } + // date settings: possible date formats $dateshort = array("m/d/Y - H:i", "d/m/Y - H:i", "Y/m/d - H:i", "m/d/Y - g:ia", "d/m/Y - g:ia", "Y/m/d - g:ia", @@ -164,7 +172,8 @@ function system_view_general() { $datelongchoices[$f] = format_date(time(), "custom", $f); } - $group = form_select(t("Date format (short)"), "date_format_short", variable_get("date_format_short", $dateshort[0]), $dateshortchoices, t("The short format of date display.")); + $group = form_select(t("Time zone"), "date_default_timezone", variable_get('date_default_timezone', 0), $zones, t("Select the default site timezone.")); + $group .= form_select(t("Date format (short)"), "date_format_short", variable_get("date_format_short", $dateshort[0]), $dateshortchoices, t("The short format of date display.")); $group .= form_select(t("Date format (medium)"), "date_format_medium", variable_get("date_format_medium", $datemedium[0]), $datemediumchoices, t("The medium sized date display.")); $group .= form_select(t("Date format (long)"), "date_format_long", variable_get("date_format_long", $datelong[0]), $datelongchoices, t("Longer date format used for detailed display.")); diff --git a/modules/system/system.module b/modules/system/system.module index cf1d72d5c..b9a7918bb 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -86,10 +86,11 @@ function system_user($type, $edit, &$user) { $data[t('Theme settings')] = form_item(t("Theme"), "", t("Selecting a different theme will change the look and feel of the site.")); } + $timestamp = time(); $zonelist = array(-11, -10, -9.5, -9, -8, -7, -6, -5, -4, -3.5, -3, -2, -1, 0, 1, 2, 3, 3.5, 4, 5, 5.5, 5.75, 6, 6.5, 7, 8, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 14); foreach ($zonelist as $offset) { $zone = $offset * 3600; - $zones["$zone"] = date(variable_get("date_format_long", "l, F dS, Y - g:ia"), time() - date("Z") + $zone) . sprintf(" (GMT %s%02d:%02d)\n", ($offset >= 0) ? "+" : "-", floor(abs($offset)), (abs($offset) * 60) % 60); + $zones[$zone] = format_date($timestamp, 'custom', variable_get('date_format_long', $datelong[0]) . ' O', $zone); } $data[t('Locale settings')] = form_select(t("Time zone"), "timezone", $edit["timezone"], $zones, t("Select what time you currently have and your time zone settings will be set appropriate.")); return $data; @@ -141,6 +142,13 @@ function system_view_general() { // date settings: + $timestamp = time(); + $zonelist = array(-11, -10, -9.5, -9, -8, -7, -6, -5, -4, -3.5, -3, -2, -1, 0, 1, 2, 3, 3.5, 4, 5, 5.5, 5.75, 6, 6.5, 7, 8, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 14); + foreach ($zonelist as $offset) { + $zone = $offset * 3600; + $zones[$zone] = format_date($timestamp, 'custom', variable_get('date_format_long', $datelong[0]) . ' O', $zone); + } + // date settings: possible date formats $dateshort = array("m/d/Y - H:i", "d/m/Y - H:i", "Y/m/d - H:i", "m/d/Y - g:ia", "d/m/Y - g:ia", "Y/m/d - g:ia", @@ -164,7 +172,8 @@ function system_view_general() { $datelongchoices[$f] = format_date(time(), "custom", $f); } - $group = form_select(t("Date format (short)"), "date_format_short", variable_get("date_format_short", $dateshort[0]), $dateshortchoices, t("The short format of date display.")); + $group = form_select(t("Time zone"), "date_default_timezone", variable_get('date_default_timezone', 0), $zones, t("Select the default site timezone.")); + $group .= form_select(t("Date format (short)"), "date_format_short", variable_get("date_format_short", $dateshort[0]), $dateshortchoices, t("The short format of date display.")); $group .= form_select(t("Date format (medium)"), "date_format_medium", variable_get("date_format_medium", $datemedium[0]), $datemediumchoices, t("The medium sized date display.")); $group .= form_select(t("Date format (long)"), "date_format_long", variable_get("date_format_long", $datelong[0]), $datelongchoices, t("Longer date format used for detailed display.")); -- cgit v1.2.3