From 62fc9d7f5528f16614ff78cacf3e11f44257391a Mon Sep 17 00:00:00 2001
From: Jesse Beach <splendidnoise@gmail.com>
Date: Sun, 6 Nov 2011 21:20:52 -0500
Subject: [PATCH] Issue #1183250 by jessebeach, linclark, ericduran, Everett Zufelt, sun, aspilicious, scor: Introduced theme_datetime

Signed-off-by: Jesse Beach <splendidnoise@gmail.com>
---
 core/includes/common.inc                 |    3 +
 core/includes/theme.inc                  |   60 ++++++++++++++++++++++++++++++
 core/modules/simpletest/tests/theme.test |   52 ++++++++++++++++++++++++++
 3 files changed, 115 insertions(+), 0 deletions(-)

diff --git a/core/includes/common.inc b/core/includes/common.inc
index 4229d52..5e0f806 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -6564,6 +6564,9 @@ function drupal_common_theme() {
       'render element' => 'elements',
       'template' => 'region',
     ),
+    'datetime' => array(
+      'variables' => array('timestamp' => NULL, 'text' => NULL, 'attributes' => array()),
+    ),
     'status_messages' => array(
       'variables' => array('display' => NULL),
     ),
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 1e45b2a..237ab6a 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -1460,6 +1460,66 @@ function theme_disable($theme_list) {
  */
 
 /**
+ * Preprocess variables for theme_datetime().
+ */
+function template_preprocess_datetime(&$variables) {
+  // Use the datetime attribute if provided.
+  if (empty($variables['attributes']['datetime'])) {
+    // If the datetime attribute was not provided, attempt to use the timestamp
+    // value as the datetime attribute.
+    if (!empty($variables['timestamp'])) {
+      // Format the timestamp with gmdate to produce a valid date/time format.
+      // See http://www.w3.org/TR/html5/common-microsyntaxes.html#valid-date-string-with-optional-time
+      $variables['attributes']['datetime'] = gmdate('Y-m-d\TH:i:s\Z', $variables['timestamp']);
+      // If no text for the <time> element was provided, print a human-readable
+      // version of the timestamp.
+      if (!isset($variables['text'])) {
+        $variables['text'] = format_date($variables['timestamp']);
+      }
+    }
+  }
+  // If no text was provided, use the datetime attribute as the text. If the
+  // the datetime attribute is not set then return an empty string.
+  if (!isset($variables['text'])) {
+    $variables['text'] = !empty($variables['attributes']['datetime']) ? $variables['attributes']['datetime'] : '';
+  }
+
+  // Run $variables['text'] through filter_xss.
+  $variables['text'] = filter_xss($variables['text']);
+}
+
+/**
+ * Returns HTML for a date / time.
+ *
+ * @param $variables
+ *   An associative array containing:
+ *   - timestamp: (optional) A UNIX timestamp for the datetime. If the
+ *     datetime cannot be represented as a UNIX timestamp, instead use a valid
+ *     date string with optional time in $variables['attributes']['datetime'],
+ *     for example a simple year '2011', a date '2011-12-31' or a date with a
+ *     time in Eastern Standard time '2011-12-31T23:59-05:00'.
+ *     For valid date string formats see
+ *     http://www.w3.org/TR/html5/common-microsyntaxes.html#valid-date-string-with-optional-time
+ *   - text: (optional) The text content to display in the <time> element. Use
+ *     this variable (formatted with format_date() for example) if you provide a
+ *     date value in $variables['attributes']['datetime'] or if an irregular
+ *     text content value is needed, such as "my birthday".
+ *   - attributes: (optional) Associative array of attributes to be placed in
+ *     the <time> element. If a datetime attribute is passed in, it will
+ *     override the timestamp.
+ *     See http://www.w3.org/TR/html5-author/the-time-element.html#attr-time-datetime
+ *
+ *     The <time> element also supports a pubdate attribute that allows an
+ *     author to differentiate the creation date from the publication date.
+ *     See http://www.w3.org/TR/html5-author/the-time-element.html#attr-time-pubdate
+ *
+ * @see template_preprocess_datetime
+ */
+function theme_datetime($variables) {
+  return '<time' . drupal_attributes($variables['attributes']) . '>' . $variables['text'] . '</time>';
+}
+
+/**
  * Returns HTML for status and/or error messages, grouped by type.
  *
  * An invisible heading identifies the messages for assistive technology.
diff --git a/core/modules/simpletest/tests/theme.test b/core/modules/simpletest/tests/theme.test
index 7968cf7..59b3087 100644
--- a/core/modules/simpletest/tests/theme.test
+++ b/core/modules/simpletest/tests/theme.test
@@ -488,3 +488,55 @@ class ThemeHtmlTplPhpAttributesTestCase extends DrupalWebTestCase {
     $this->assertTrue(count($attributes) == 1, t('Attribute set in the body element via hook_preprocess_html() found.'));
   }
 }
+
+/**
+ * Tests for theme_datetime().
+ */
+class ThemeDatetime extends DrupalWebTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Theme Datetime',
+      'description' => 'Test the theme_datetime() function.',
+      'group' => 'Theme',
+    );
+  }
+
+  /**
+   * Test function theme_datetime().
+   */
+  function testThemeDatetime() {
+    // Create timestamp and formatted date for testing.
+    $timestamp = 280281600;
+    $date = format_date($timestamp);
+
+    // Test with timestamp.
+    $variables = array(
+      'timestamp' => $timestamp,
+    );
+    $this->assertEqual('<time datetime="1978-11-19T00:00:00Z">' . $date . '</time>', theme('datetime', $variables), t('Test theme_datetime with timestamp.'));
+
+    // Test with text and timestamp.
+    $variables = array(
+      'timestamp' => $timestamp,
+      'text' => 'Dries\'s birthday',
+    );
+    $this->assertEqual('<time datetime="1978-11-19T00:00:00Z">Dries\'s birthday</time>', theme('datetime', $variables), t('Test theme_datetime with text and timestamp.'));
+
+    // Test with datetime attribute.
+    $variables = array(
+      'attributes' => array(
+        'datetime' => '1978-11-19',
+      ),
+    );
+    $this->assertEqual('<time datetime="1978-11-19">1978-11-19</time>', theme('datetime', $variables), t('Test theme_datetime with datetime attribute.'));
+
+    // Test with text and datetime attribute.
+    $variables = array(
+      'text' => 'Dries\'s birthday',
+      'attributes' => array(
+        'datetime' => '1978-11-19',
+      ),
+    );
+    $this->assertEqual('<time datetime="1978-11-19">Dries\'s birthday</time>', theme('datetime', $variables), t('Test theme_datetime with text and datetime attribute.'));
+  }
+}
-- 
1.7.3.4

