Index: handlers/views_handler_field_date.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views/handlers/views_handler_field_date.inc,v retrieving revision 1.2.2.2 diff -u -p -r1.2.2.2 views_handler_field_date.inc --- handlers/views_handler_field_date.inc 13 Aug 2010 22:27:20 -0000 1.2.2.2 +++ handlers/views_handler_field_date.inc 25 Aug 2010 17:27:16 -0000 @@ -6,6 +6,47 @@ * @ingroup views_field_handlers */ class views_handler_field_date extends views_handler_field { + +/** + * Converts $user->timezone to a DateTimeZone object + * + * @return + * Returns DateTimeZone object on success, FALSE on failure. + */ + function get_user_datetimezone() { + global $user; + $timezone = $user->timezone; + + // Get array of all timezone abbreviations. + $timezone_abbreviations = DateTimeZone::listAbbreviations(); + + // Search arrach for offset value and return timezone string. + // TODO Multiple timezones have the same offset, this will use the first it finds. + // TODO Drupal 6 only stores the offset in seconds, not the actual zone. + // TODO Drupal 7 SHOULD have access to the real zone names. Use that instead. + // TODO Need to understand the DST impact. + foreach($timezone_abbreviations as $continent => $zone) { + foreach($zone as $key => $val) { + if ($val['offset'] == $timezone) { + $timezone_id = $val['timezone_id']; + break; + } + } + if (isset($timezone_id)) { + break; + } + } + + // Create the DateTimeZone object from the $timezone_id string. + if (isset($timezone_id)) { + $datetimezone = timezone_open($timezone_id); + return $datetimezone; + } + else { + return FALSE; + } + } + function option_definition() { $options = parent::option_definition(); @@ -23,14 +64,10 @@ class views_handler_field_date extends v '#type' => 'select', '#title' => t('Date format'), '#options' => array( - 'small' => format_date($time, 'small'), - 'medium' => format_date($time, 'medium'), - 'large' => format_date($time, 'large'), + 'small' => format_date($time, 'custom', 'm/d/Y'), + 'medium' => format_date($time, 'custom', 'D, m/d/Y'), + 'large' => format_date($time, 'custom', 'l, F j, Y'), 'custom' => t('Custom'), - 'raw time ago' => t('Time ago'), - 'time ago' => t('Time ago (with "ago" appended)'), - 'raw time span' => t('Time span (future dates start with - )'), - 'time span' => t('Time span (with "ago/hence" appended)'), ), '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small', ); @@ -40,38 +77,53 @@ class views_handler_field_date extends v '#description' => t('If "Custom", see the PHP docs for date formats. If "Time ago" this is the the number of different units to display, which defaults to two.'), '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '', '#process' => array('views_process_dependency'), - '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')), + '#dependency' => array('edit-options-date-format' => array('custom')), ); } function render($values) { $value = $values->{$this->field_alias}; $format = $this->options['date_format']; - if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) { + if (in_array($format, array('custom'))) { $custom_format = $this->options['custom_date_format']; } - + + // Get timezone offset. + $datetimezone = $this->get_user_datetimezone(); + + // Check that get_user_datetimezone() returned a valid $datetimezone object. + if (!$datetimezone) { + drupal_set_message(t("You should set your Time zone for your account."), 'warning', FALSE); + $date = date_create($value); + } + else { // Set timezone offset. + $date = date_create($value); + date_timezone_set($date, $datetimezone); + } + + // Empty field. Render as text. if (!$value) { return theme('views_nodate'); } + // Check that date_create() returned a valid $date object. + // Set error message if not and render as text. + elseif (!$date) { + drupal_set_message(t("Invalide date field, '" . $this->options['field'] . "', should be of data type date."), 'error', FALSE); + return theme('views_nodate'); + } else { - $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) switch ($format) { - case 'raw time ago': - return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2); - case 'time ago': - return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2))); - case 'raw time span': - return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); - case 'time span': - return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2))); case 'custom': - if ($custom_format == 'r') { - return format_date($value, $format, $custom_format, null, 'en'); - } - return format_date($value, $format, $custom_format); + // Return custom formated date value. + return date_format($date, $custom_format); + case 'medium': + return date_format($date, 'D, m/d/Y'); + case 'large': + return date_format($date, 'l, F j, Y'); + case 'small': default: - return format_date($value, $format); + // Return default formated date value. + return date_format($date, 'm/d/Y'); } } } Index: handlers/views_handler_field_datetime.inc =================================================================== RCS file: handlers/views_handler_field_datetime.inc diff -N handlers/views_handler_field_datetime.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ handlers/views_handler_field_datetime.inc 25 Aug 2010 17:27:16 -0000 @@ -0,0 +1,107 @@ + 'small'); + $options['custom_date_format'] = array('default' => ''); + + return $options; + } + + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + $time = time(); + + $form['date_format'] = array( + '#type' => 'select', + '#title' => t('Date format'), + '#options' => array( + 'small' => format_date($time, 'small'), + 'medium' => format_date($time, 'medium'), + 'large' => format_date($time, 'large'), + 'custom' => t('Custom'), + 'raw time ago' => t('Time ago'), + 'time ago' => t('Time ago (with "ago" appended)'), + 'raw time span' => t('Time span (future dates start with - )'), + 'time span' => t('Time span (with "ago/hence" appended)'), + ), + '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small', + ); + $form['custom_date_format'] = array( + '#type' => 'textfield', + '#title' => t('Custom date format'), + '#description' => t('If "Custom", see the PHP docs for date formats. If "Time ago" this is the the number of different units to display, which defaults to two.'), + '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '', + '#process' => array('views_process_dependency'), + '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')), + ); + } + + function render($values) { + $value = $values->{$this->field_alias}; + $format = $this->options['date_format']; + if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) { + $custom_format = $this->options['custom_date_format']; + } + + // Get timezone offset. + $datetimezone = $this->get_user_datetimezone(); + + // Check that get_user_datetimezone() returned a valid $datetimezone object. + if (!$datetimezone) { + drupal_set_message(t("You should set your Time zone for your account."), 'warning', FALSE); + $date = date_create($value); + } + else { // Set timezone offset. + $date = date_create($value); + date_timezone_set($date, $datetimezone); + } + + // Empty field. Render as text. + if (!$value) { + return theme('views_nodate'); + } + // Check that date_create() returned a valid $date object. + // Set error message if not and render as text. + elseif (!$date) { + drupal_set_message(t("Invalide date field, '" . $this->options['field'] . "', should be of data type datetime."), 'error', FALSE); + return theme('views_nodate'); + } + else { + // TODO Add $timezone offset if needed. + $time_diff = time() - strtotime($value); // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) + switch ($format) { + case 'raw time ago': + return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2); + case 'time ago': + return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2))); + case 'raw time span': + return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); + case 'time span': + return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2))); + case 'custom': + // TODO probably do not need this if statement + if ($custom_format == 'r') { + return date_format($date, $custom_format); + } + // Return custom formated date value. + return date_format($date, $custom_format); + case 'medium': + return date_format($date, 'D, m/d/Y - H:i'); + case 'large': + return date_format($date, 'l, F j, Y - H:i'); + case 'small': + default: + // Return default formated date value. + return date_format($date, 'm/d/Y - H:i'); + } + } + } +} Index: handlers/views_handler_field_timestamp.inc =================================================================== RCS file: handlers/views_handler_field_timestamp.inc diff -N handlers/views_handler_field_timestamp.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ handlers/views_handler_field_timestamp.inc 25 Aug 2010 17:27:16 -0000 @@ -0,0 +1,83 @@ + 'small'); + + return $options; + } + + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + $time = time(); + + $form['date_format'] = array( + '#type' => 'select', + '#title' => t('Date format'), + '#options' => array( + 'small' => format_date($time, 'small'), + 'medium' => format_date($time, 'medium'), + 'large' => format_date($time, 'large'), + 'custom' => t('Custom'), + 'raw time ago' => t('Time ago'), + 'time ago' => t('Time ago (with "ago" appended)'), + 'raw time span' => t('Time span (future dates start with - )'), + 'time span' => t('Time span (with "ago/hence" appended)'), + ), + '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small', + ); + $form['custom_date_format'] = array( + '#type' => 'textfield', + '#title' => t('Custom date format'), + '#description' => t('If "Custom", see the PHP docs for date formats. If "Time ago" this is the the number of different units to display, which defaults to two.'), + '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '', + '#process' => array('views_process_dependency'), + '#dependency' => array('edit-options-date-format' => array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span')), + ); + } + + function render($values) { + $value = $values->{$this->field_alias}; + $format = $this->options['date_format']; + if (in_array($format, array('custom', 'raw time ago', 'time ago', 'raw time span', 'time span'))) { + $custom_format = $this->options['custom_date_format']; + } + + if (!$value) { + return theme('views_nodate'); + } + // Check that $value is numeric and can be converted with format_date. + // Set error message if not and render as text. + elseif (!is_numeric($value)) { + drupal_set_message(t("Invalide date field, '" . $this->options['field'] . "', should be a UNIX timestamp"), 'error', FALSE); + return theme('views_nodate'); + } + else { + $time_diff = time() - $value; // will be positive for a datetime in the past (ago), and negative for a datetime in the future (hence) + switch ($format) { + case 'raw time ago': + return format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2); + case 'time ago': + return t('%time ago', array('%time' => format_interval($time_diff, is_numeric($custom_format) ? $custom_format : 2))); + case 'raw time span': + return ($time_diff < 0 ? '-' : '') . format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2); + case 'time span': + return t(($time_diff < 0 ? '%time hence' : '%time ago'), array('%time' => format_interval(abs($time_diff), is_numeric($custom_format) ? $custom_format : 2))); + case 'custom': + if ($custom_format == 'r') { + return format_date($value, $format, $custom_format, null, 'en'); + } + return format_date($value, $format, $custom_format); + default: + return format_date($value, $format); + } + } + } +} Index: includes/handlers.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views/includes/handlers.inc,v retrieving revision 1.109.2.17 diff -u -p -r1.109.2.17 handlers.inc --- includes/handlers.inc 30 Jun 2010 19:19:02 -0000 1.109.2.17 +++ includes/handlers.inc 25 Aug 2010 17:27:18 -0000 @@ -1212,6 +1212,12 @@ function views_views_handlers() { 'views_handler_field_date' => array( 'parent' => 'views_handler_field', ), + 'views_handler_field_datetime' => array( + 'parent' => 'views_handler_field_date', + ), + 'views_handler_field_timestamp' => array( + 'parent' => 'views_handler_field_date', + ), 'views_handler_field_boolean' => array( 'parent' => 'views_handler_field', ),