From 31e1c15db722d8363ceab6c97da14d703badb4f5 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 2 Oct 2013 15:44:44 +0200 Subject: [PATCH 1/5] Added dateOnly argument to relative_modified_date Improved the template function relative_modified_date by adding an optional dateOnly argument which will output "today" or "yesterday" or "x days ago". --- lib/private/template/functions.php | 40 ++++++++++++++++++++++++------ lib/public/template.php | 4 +-- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php index 501f8081bf..f0fd9999d9 100644 --- a/lib/private/template/functions.php +++ b/lib/private/template/functions.php @@ -85,22 +85,46 @@ function human_file_size( $bytes ) { return OC_Helper::humanFileSize( $bytes ); } -function relative_modified_date($timestamp) { +/** + * @brief Strips the timestamp of its time value + * @param int $timestamp UNIX timestamp to strip + * @return $timestamp without time value + */ +function strip_time($timestamp){ + $date = new \DateTime("@{$timestamp}"); + $date->setTime(0, 0, 0); + return intval($date->format('U')); +} + +/** + * @brief Formats timestamp relatively to the current time using + * a human-friendly format like "x minutes ago" or "yesterday" + * @param int $timestamp timestamp to format + * @param bool $dateOnly whether to strip time information + * @return formatted timestamp + */ +function relative_modified_date($timestamp, $dateOnly = false) { $l=OC_L10N::get('lib'); - $timediff = time() - $timestamp; + $time = time(); + if ($dateOnly){ + $time = strip_time($time); + $timestamp = strip_time($timestamp); + } + $timediff = $time - $timestamp; $diffminutes = round($timediff/60); $diffhours = round($diffminutes/60); $diffdays = round($diffhours/24); $diffmonths = round($diffdays/31); + \OC_Log::write('functions', '################ ' . $timediff . ' ' . $diffhours, \OC_Log::DEBUG); - if($timediff < 60) { return $l->t('seconds ago'); } - else if($timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); } - else if($timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); } - else if((date('G')-$diffhours) > 0) { return $l->t('today'); } - else if((date('G')-$diffhours) > -24) { return $l->t('yesterday'); } + if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); } + else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); } + else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); } + else if((date('G', $time)-$diffhours) >= 0) { return $l->t('today'); } + else if((date('G', $time)-$diffhours) >= -24) { return $l->t('yesterday'); } else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); } else if($timediff < 5184000) { return $l->t('last month'); } - else if((date('n')-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); } + else if((date('n', $time)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); } else if($timediff < 63113852) { return $l->t('last year'); } else { return $l->t('years ago'); } } diff --git a/lib/public/template.php b/lib/public/template.php index 3b1a4ed490..b3bffaf1af 100644 --- a/lib/public/template.php +++ b/lib/public/template.php @@ -90,8 +90,8 @@ function human_file_size( $bytes ) { * @param $timestamp unix timestamp * @returns human readable interpretation of the timestamp */ -function relative_modified_date($timestamp) { - return(\relative_modified_date($timestamp)); +function relative_modified_date($timestamp, $dateOnly = false) { + return(\relative_modified_date($timestamp, $dateOnly)); } From ed14541aea648605536c8180ab56f511013bce38 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 2 Oct 2013 16:44:38 +0200 Subject: [PATCH 2/5] Removed stray debug log write --- lib/private/template/functions.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php index f0fd9999d9..6002113bdd 100644 --- a/lib/private/template/functions.php +++ b/lib/private/template/functions.php @@ -115,7 +115,6 @@ function relative_modified_date($timestamp, $dateOnly = false) { $diffhours = round($diffminutes/60); $diffdays = round($diffhours/24); $diffmonths = round($diffdays/31); - \OC_Log::write('functions', '################ ' . $timediff . ' ' . $diffhours, \OC_Log::DEBUG); if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); } else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); } From b0bb64c3eebce41a35cfe6674f63f454d22f200c Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 2 Oct 2013 20:16:35 +0200 Subject: [PATCH 3/5] Added unit tests for relative_modified_date, changed method signature Changed method signature of relative_modified_date template method to make it possible to add a fromTime to compare with, mostly to make it possible to test it. Added unit test for date and time cases. --- lib/private/template/functions.php | 20 +-- lib/public/template.php | 4 +- tests/lib/template.php | 190 ++++++++++++++++++++++++++++- 3 files changed, 204 insertions(+), 10 deletions(-) diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php index 6002113bdd..858134dd8c 100644 --- a/lib/private/template/functions.php +++ b/lib/private/template/functions.php @@ -100,17 +100,20 @@ function strip_time($timestamp){ * @brief Formats timestamp relatively to the current time using * a human-friendly format like "x minutes ago" or "yesterday" * @param int $timestamp timestamp to format + * @param int $fromTime timestamp to compare from, defaults to current time * @param bool $dateOnly whether to strip time information * @return formatted timestamp */ -function relative_modified_date($timestamp, $dateOnly = false) { +function relative_modified_date($timestamp, $fromTime, $dateOnly = false) { $l=OC_L10N::get('lib'); - $time = time(); + if (!isset($fromTime)){ + $fromTime = time(); + } if ($dateOnly){ - $time = strip_time($time); + $fromTime = strip_time($fromTime); $timestamp = strip_time($timestamp); } - $timediff = $time - $timestamp; + $timediff = $fromTime - $timestamp; $diffminutes = round($timediff/60); $diffhours = round($diffminutes/60); $diffdays = round($diffhours/24); @@ -119,11 +122,14 @@ function relative_modified_date($timestamp, $dateOnly = false) { if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); } else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); } else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); } - else if((date('G', $time)-$diffhours) >= 0) { return $l->t('today'); } - else if((date('G', $time)-$diffhours) >= -24) { return $l->t('yesterday'); } + else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); } + else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); } + // 86400 * 31 days = 2678400 else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); } + // 86400 * 60 days = 518400 else if($timediff < 5184000) { return $l->t('last month'); } - else if((date('n', $time)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); } + else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); } + // 86400 * 365.25 days * 2 = 63113852 else if($timediff < 63113852) { return $l->t('last year'); } else { return $l->t('years ago'); } } diff --git a/lib/public/template.php b/lib/public/template.php index b3bffaf1af..6349501272 100644 --- a/lib/public/template.php +++ b/lib/public/template.php @@ -90,8 +90,8 @@ function human_file_size( $bytes ) { * @param $timestamp unix timestamp * @returns human readable interpretation of the timestamp */ -function relative_modified_date($timestamp, $dateOnly = false) { - return(\relative_modified_date($timestamp, $dateOnly)); +function relative_modified_date($timestamp, $fromTime, $dateOnly = false) { + return(\relative_modified_date($timestamp, $fromTime, $dateOnly)); } diff --git a/tests/lib/template.php b/tests/lib/template.php index fd12119da5..b4f1a4c405 100644 --- a/tests/lib/template.php +++ b/tests/lib/template.php @@ -46,7 +46,6 @@ class Test_TemplateFunctions extends PHPUnit_Framework_TestCase { $this->assertEquals("This is a good string!", $result); } - public function testPrintUnescaped() { $htmlString = ""; @@ -66,5 +65,194 @@ class Test_TemplateFunctions extends PHPUnit_Framework_TestCase { $this->assertEquals("This is a good string!", $result); } + // --------------------------------------------------------------------------- + // Test relative_modified_date with dates only + // --------------------------------------------------------------------------- + public function testRelativeDateToday(){ + $currentTime = 1380703592; + $elementTime = $currentTime; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + $this->assertEquals('today', $result); + + // 2 hours ago is still today + $elementTime = $currentTime - 2 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('today', $result); + } + + public function testRelativeDateYesterday(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 24 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('yesterday', $result); + + // yesterday - 2 hours is still yesterday + $elementTime = $currentTime - 26 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('yesterday', $result); + } + + public function testRelativeDate2DaysAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 48 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('2 days ago', $result); + + // 2 days ago minus 4 hours is still 2 days ago + $elementTime = $currentTime - 52 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('2 days ago', $result); + } + + public function testRelativeDateLastMonth(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 31; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('last month', $result); + + $elementTime = $currentTime - 86400 * 35; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('last month', $result); + } + + public function testRelativeDateMonthsAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 60; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('2 months ago', $result); + + $elementTime = $currentTime - 86400 * 65; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('2 months ago', $result); + } + + public function testRelativeDateLastYear(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 365; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('last year', $result); + + $elementTime = $currentTime - 86400 * 450; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('last year', $result); + } + + public function testRelativeDateYearsAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 365.25 * 2; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('years ago', $result); + + $elementTime = $currentTime - 86400 * 365.25 * 3; + $result = (string)relative_modified_date($elementTime, $currentTime, true); + + $this->assertEquals('years ago', $result); + } + + // --------------------------------------------------------------------------- + // Test relative_modified_date with timestamps only (date + time value) + // --------------------------------------------------------------------------- + + public function testRelativeTimeSecondsAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 5; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('seconds ago', $result); + } + + public function testRelativeTimeMinutesAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 190; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('3 minutes ago', $result); + } + + public function testRelativeTimeHoursAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 7500; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('2 hours ago', $result); + } + + public function testRelativeTime2DaysAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 48 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('2 days ago', $result); + + // 2 days ago minus 4 hours is still 2 days ago + $elementTime = $currentTime - 52 * 3600; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('2 days ago', $result); + } + + public function testRelativeTimeLastMonth(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 31; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('last month', $result); + + $elementTime = $currentTime - 86400 * 35; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('last month', $result); + } + + public function testRelativeTimeMonthsAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 60; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('2 months ago', $result); + + $elementTime = $currentTime - 86400 * 65; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('2 months ago', $result); + } + + public function testRelativeTimeLastYear(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 365; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('last year', $result); + + $elementTime = $currentTime - 86400 * 450; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('last year', $result); + } + + public function testRelativeTimeYearsAgo(){ + $currentTime = 1380703592; + $elementTime = $currentTime - 86400 * 365.25 * 2; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('years ago', $result); + + $elementTime = $currentTime - 86400 * 365.25 * 3; + $result = (string)relative_modified_date($elementTime, $currentTime, false); + + $this->assertEquals('years ago', $result); + } } From 006799616d584fc3f83e325910368a8af0aee072 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 3 Oct 2013 14:21:18 +0200 Subject: [PATCH 4/5] Fixed missing default values Added default value for $fromTime to prevent missing argument errors and keep backward compatible. --- lib/private/template/functions.php | 4 ++-- lib/public/template.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php index 858134dd8c..0aa2b27b96 100644 --- a/lib/private/template/functions.php +++ b/lib/private/template/functions.php @@ -104,9 +104,9 @@ function strip_time($timestamp){ * @param bool $dateOnly whether to strip time information * @return formatted timestamp */ -function relative_modified_date($timestamp, $fromTime, $dateOnly = false) { +function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) { $l=OC_L10N::get('lib'); - if (!isset($fromTime)){ + if (!isset($fromTime) || $fromTime === null){ $fromTime = time(); } if ($dateOnly){ diff --git a/lib/public/template.php b/lib/public/template.php index 6349501272..8800f5c856 100644 --- a/lib/public/template.php +++ b/lib/public/template.php @@ -90,7 +90,7 @@ function human_file_size( $bytes ) { * @param $timestamp unix timestamp * @returns human readable interpretation of the timestamp */ -function relative_modified_date($timestamp, $fromTime, $dateOnly = false) { +function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) { return(\relative_modified_date($timestamp, $fromTime, $dateOnly)); } From f3594904c2d2b4a1296ee7dcc65a0c6cefa9ab40 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Fri, 4 Oct 2013 14:45:12 +0200 Subject: [PATCH 5/5] Removed $fromTime argument from public template API --- lib/public/template.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/public/template.php b/lib/public/template.php index 8800f5c856..a5c500b0e2 100644 --- a/lib/public/template.php +++ b/lib/public/template.php @@ -90,8 +90,8 @@ function human_file_size( $bytes ) { * @param $timestamp unix timestamp * @returns human readable interpretation of the timestamp */ -function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) { - return(\relative_modified_date($timestamp, $fromTime, $dateOnly)); +function relative_modified_date($timestamp, $dateOnly = false) { + return(\relative_modified_date($timestamp, null, $dateOnly)); }