Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Add JHtmlDate class with relative time function #370

Merged
merged 18 commits into from Nov 18, 2011
@@ -0,0 +1,89 @@
+<?php
+/**
+ * @package Joomla.Platform
+ * @subpackage HTML
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+defined('JPATH_PLATFORM') or die;
+
+/**
+ * Extended Utility class for handling date display.
+ *
+ * @package Joomla.Platform
+ * @subpackage HTML
+ * @since 11.3
+ */
+abstract class JHtmlDate
+{
+ /**
+ * Function to convert a static time into a relative measurement
+ *
+ * @param string $date The date to convert
+ * @param string $unit The optional unit of measurement to return
+ * if the value of the diff is greater than one
+ * @param string $time An optional time to compare to, defaults to now
+ *
+ * @return string The converted time string
+ *
+ * @since 11.3
+ */
+ public static function relative($date, $unit = null, $time = null)
+ {
+ if (is_null($time))
+ {
+ // Get now
+ $time = JFactory::getDate('now');
+ }
+
+ // Get the difference in seconds between now and the time
+ $diff = strtotime($time) - strtotime($date);
+
+ // Less than a minute
+ if ($diff < 60)
+ {
+ return JText::_('JLIB_HTML_DATE_RELATIVE_LESSTHANAMINUTE');
+ }
+
+ // Round to minutes
+ $diff = round($diff / 60);
+
+ // 1 to 59 minutes
+ if ($diff < 60 || $unit == 'minute')
+ {
+ return JText::plural('JLIB_HTML_DATE_RELATIVE_MINUTES', $diff);
+ }
+
+ // Round to hours
+ $diff = round($diff / 60);
+
+ // 1 to 23 hours
+ if ($diff < 24 || $unit == 'hour')
+ {
+ return JText::plural('JLIB_HTML_DATE_RELATIVE_HOURS', $diff);
+ }
+
+ // Round to days
+ $diff = round($diff / 24);
+
+ // 1 to 6 days
+ if ($diff < 7 || $unit == 'day')
+ {
+ return JText::plural('JLIB_HTML_DATE_RELATIVE_DAYS', $diff);
+ }
+
+ // Round to weeks
+ $diff = round($diff / 7);
+
+ // 1 to 4 weeks
+ if ($diff <= 4 || $unit == 'week')
+ {
+ return JText::plural('JLIB_HTML_DATE_RELATIVE_WEEKS', $diff);
+ }
+
+ // Over a month, return the absolute time
+ return JHtml::_('date', $date);
+ }
+}
@@ -0,0 +1,18 @@
+; Joomla! Project
+; Copyright (C) 2005 - 2011 Open Source Matters. All rights reserved.
+; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
+; Note : All ini files need to be saved as UTF-8 - No BOM
+
+JLIB_HTML_DATE_RELATIVE_DAYS="%s days ago"
+JLIB_HTML_DATE_RELATIVE_DAYS_1="%s day ago"
+JLIB_HTML_DATE_RELATIVE_DAYS_0="%s days ago"
+JLIB_HTML_DATE_RELATIVE_HOURS="%s hours ago"
+JLIB_HTML_DATE_RELATIVE_HOURS_1="%s hour ago"
+JLIB_HTML_DATE_RELATIVE_HOURS_0="%s hours ago"
+JLIB_HTML_DATE_RELATIVE_LESSTHANAMINUTE="Less than a minute ago"
+JLIB_HTML_DATE_RELATIVE_MINUTES="%s minutes ago"
+JLIB_HTML_DATE_RELATIVE_MINUTES_1="%s minute ago"
+JLIB_HTML_DATE_RELATIVE_MINUTES_0="%s minutes ago"
+JLIB_HTML_DATE_RELATIVE_WEEKS="%s weeks ago"
+JLIB_HTML_DATE_RELATIVE_WEEKS_1="%s week ago"
+JLIB_HTML_DATE_RELATIVE_WEEKS_0="%s weeks ago"
@@ -0,0 +1 @@
+<!DOCTYPE html><title></title>
@@ -0,0 +1 @@
+<!DOCTYPE html><title></title>
@@ -0,0 +1,120 @@
+<?php
+/**
+ * @package Joomla.UnitTest
+ * @subpackage HTML
+ *
+ * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
+ * @license GNU General Public License version 2 or later; see LICENSE
+ */
+
+require_once JPATH_PLATFORM.'/joomla/html/html/date.php';
+
+/**
+ * Test class for JHtmlDate.
+ *
+ * @since 11.3
+ */
+class JHtmlDateTest extends JoomlaTestCase
+{
+ /**
+ * Setup for testing.
+ *
+ * @return void
+ *
+ * @since 11.3
+ */
+ public function setUp()
+ {
+ parent::setUp();
+
+ // We are only coupled to Document and Language in JFactory.
+ $this->saveFactoryState();
+
+ JFactory::$language = $this->getMockLanguage();
+ }
+
+ /**
+ * Overrides the parent tearDown method.
+ *
+ * @return void
+ *
+ * @see PHPUnit_Framework_TestCase::tearDown()
+ * @since 11.3
+ */
+ protected function tearDown()
+ {
+ $this->restoreFactoryState();
+
+ parent::tearDown();
+ }
+
+ /**
+ * @return array
+ *
+ * @since 11.3
+ */
+ public function dataTestRelative()
+ {
+ return array(
+ // Element order: result, date, unit, time
+ // result - 1 hour ago
+ array(
+ 'JLIB_HTML_DATE_RELATIVE_HOURS',
+ JFactory::getDate('2011-10-18 11:00:00'),
+ null,
+ JFactory::getDate('2011-10-18 12:00:00')
+ ),
+ // result - 10 days ago
+ array(
+ 'JLIB_HTML_DATE_RELATIVE_DAYS',
+ JFactory::getDate('2011-10-08 12:00:00'),
+ 'day',
+ JFactory::getDate('2011-10-18 12:00:00')
+ ),
+ // result - 3 weeks ago
+ array(
+ 'JLIB_HTML_DATE_RELATIVE_WEEKS',
+ JFactory::getDate('2011-09-27 12:00:00'),
+ 'week',
+ JFactory::getDate('2011-10-18 12:00:00')
+ ),
+ // result - 10 minutes ago
+ array(
+ 'JLIB_HTML_DATE_RELATIVE_MINUTES',
+ JFactory::getDate('2011-10-18 11:50:00'),
+ 'minute',
+ JFactory::getDate('2011-10-18 12:00:00')
+ ),
+ // Cannot test this result while running the full suite
+ // because the getDate function returns the time the suite starts testing
+
+ // result - Less than a minute ago
+ //array(
+ //'JLIB_HTML_DATE_RELATIVE_LESSTHANAMINUTE',
+ //JFactory::getDate('now'),
+ //)
+ );
+ }
+
+ /**
+ * Tests the JHtmlDate::relative method.
+ *
+ * @param string $result The expected test result
+ * @param string $date The date to convert
+ * @param string $unit The optional unit of measurement to return
+ * if the value of the diff is greater than one
+ * @param string $time An optional time to compare to, defaults to now
+ *
+ * @return void
+ *
+ * @since 11.3
+ * @dataProvider dataTestRelative
+ */
+ public function testRelative($result, $date, $unit = null, $time = null)
+ {
+ $this->assertThat(
+ JHtmlDate::relative($date, $unit, $time),
+ $this->equalTo($result)
+ );
+ }
+}