From 7743e21a193cffd651ede70af792f8234452c053 Mon Sep 17 00:00:00 2001
From: jessebeach <jessebeach@748566.no-reply.drupal.org>
Date: Sun, 7 Aug 2011 22:32:07 -0400
Subject: [PATCH] Issue #1183250 by jessebeach: Introduced theme_datetime to theme.inc

---
 includes/common.inc |    3 ++
 includes/theme.inc  |   82 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index e75aa81..0d7fa7b 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -6415,6 +6415,9 @@ function drupal_common_theme() {
       'render element' => 'elements',
       'template' => 'region',
     ),
+    'datetime' => array(
+      'variables' => array('timestamp' => NULL, 'gmdate_format' => 'Y-m-d\TH:i:s\Z', 'offset' => NULL, 'format_type' => NULL, 'custom' => NULL, 'attributes' => array(), 'labels' => array(), 'href' => NULL),
+    ),
     'status_messages' => array(
       'variables' => array('display' => NULL),
     ),
diff --git a/includes/theme.inc b/includes/theme.inc
index 6c2b640..8ca763c 100644
--- a/includes/theme.inc
+++ b/includes/theme.inc
@@ -1312,6 +1312,88 @@ function theme_disable($theme_list) {
  */
 
 /**
+ * Returns html for a date / time.
+ *
+ * @param $variables
+ *   An associative array containing:
+ *   - timestamp: The Unix style timestamp to be themed.
+ *   - gmdate_format (optional): The format for the Unix style timestamp passed
+ *     to the gmdate() function. Defaults to 'Y-m-d\TH:i:s\Z'. We use gmdate()
+ *     with Z for the UTC value so that the datestamp remains constant
+ *     regardless of the time on the server that requested it.
+ *     @see http://www.php.net/manual/en/function.gmdate.php
+ *   - offset (optional): A Unix style time from or until the timestamp for
+ *     printing the time element text as an interval
+ *   - format_type (optional): The date format type to be used for the displayed
+ *     date or time, e.g. 'short', 'medium'. Defaults to 'medium'.
+ *   - custom (optional): The custom formatter to be used if 'format_type' is
+ *     set to 'custom'.
+ *   - attributes: (optional) Attributes for the time element. @see
+ *     http://www.w3.org/TR/html5/text-level-semantics.html#the-time-element
+ *   - labels (optional): An associative array containing the following optional
+ *     properties
+ *     - past: The label for an interval time that occurred in the past
+ *     - future: The label for an interval time that occurs in the future
+ *     - now: The label for an interval time that is the current time
+ *     - granularity: The granularity of the interval time, passed to
+ *       format_interval()
+ *     - langcode: The langcode of the interval time, passed to
+ *       format_interval()
+ *   - href (optional): A string that contains a URL or URL fragment to pass to
+ *     the l() function to render the time element text as a link
+ */
+function theme_datetime($variables) {
+  $output = '<time';
+
+  // The datetime attribute for machines.
+  $output .= ' datetime="' . gmdate($variables['gmdate_format'], $variables['timestamp']) . '"';
+  if (!empty($variables['attributes'])) {
+    $output .= drupal_attributes($variables['attributes']);
+  }
+  $output .= '>';
+  // The formatted timestamp for humans.
+  $human_readable = '';
+  if (!empty($variables['offset'])) {
+    // Prepare the interval labels.
+    $past_label =   (!empty($variables['labels']['past'])) ? $variables['labels']['past'] : t('ago');
+    $future_label = (!empty($variables['labels']['future'])) ? $variables['labels']['future'] : t('to go');
+    $now_label =    (!empty($variables['labels']['now'])) ? $variables['labels']['now'] : t('just now');
+    $granularity =  (!empty($variables['labels']['granularity'])) ? $variables['labels']['granularity'] : 2;
+    $langcode =     (!empty($variables['labels']['langcode'])) ? $variables['labels']['langcode'] : NULL;
+    // Calculate the time interval.
+    $interval = $variables['timestamp'] - $variables['offset'];
+    // Determine if the date is in the past or the future and
+    // adjust the human readable label according to the interval value.
+    switch ($interval) {
+      case ($interval > 0) :
+        $human_readable = format_interval(abs($interval), $granularity, $langcode) . ' ' . $future_label;
+        break;
+      case ($interval < 0) :
+        $human_readable = format_interval(abs($interval), $granularity, $langcode). ' ' . $past_label;
+        break;
+      case ($interval === 0) :
+      default :
+        $human_readable = t($now_label);
+        break;
+    }
+  }
+  else {
+    $human_readable = format_date($variables['timestamp'], $variables['format_type'], $variables['custom']);
+  }
+  // Wrap the human readable label in a link if an href is provided.
+  if (!empty($variables['href'])) {
+    $output .= l($human_readable, $variables['href']);
+  }
+  else {
+    $output .= $human_readable;
+  }
+  // Close the time element.
+  $output .= '</time>';
+
+  return $output;
+}
+
+/**
  * Returns HTML for status and/or error messages, grouped by type.
  *
  * An invisible heading identifies the messages for assistive technology.
-- 
1.7.3.4

