diff options
-rw-r--r-- | core/controllers/welcome.php | 3 | ||||
-rw-r--r-- | modules/comment/helpers/comment.php | 46 | ||||
-rw-r--r-- | modules/comment/tests/Comment_Helper_Test.php | 16 |
3 files changed, 39 insertions, 26 deletions
diff --git a/core/controllers/welcome.php b/core/controllers/welcome.php index 4bc709b3..40b8390a 100644 --- a/core/controllers/welcome.php +++ b/core/controllers/welcome.php @@ -189,7 +189,6 @@ class Welcome_Controller extends Template_Controller { ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."; - $seconds_in_a_year = 31556926; if (empty($photos)) { url::redirect("welcome"); @@ -199,7 +198,7 @@ class Welcome_Controller extends Template_Controller { $photo = $photos[array_rand($photos)]; comment::create("John Doe", "johndoe@example.com", substr($sample_text, 0, rand(30, strlen($sample_text))), $photo->id, - time() - rand(0, 2 * $seconds_in_a_year)); + time() - rand(0, 2 * comment::SECONDS_IN_A_YEAR)); } url::redirect("welcome"); diff --git a/modules/comment/helpers/comment.php b/modules/comment/helpers/comment.php index 232f2584..9eed8451 100644 --- a/modules/comment/helpers/comment.php +++ b/modules/comment/helpers/comment.php @@ -24,6 +24,12 @@ * Note: by design, this class does not do any permission checking. */ class Comment_Core { + const SECONDS_IN_A_MINUTE = 60; + const SECONDS_IN_AN_HOUR = 3600; + const SECONDS_IN_A_DAY = 86400; + const SECONDS_IN_A_MONTH = 2629744; + const SECONDS_IN_A_YEAR = 31556926; + /** * Create a new photo. * @param string $author author's name @@ -76,45 +82,37 @@ class Comment_Core { * * @todo Take into account the viewer's time zone. * @todo Properly pluralize strings. - * @todo Write test once we settle on the final forms of the messages. * * @param integer $timestamp Unix format timestamp to compare with * @return string user-friendly string containing the amount of time passed */ static function format_elapsed_time($timestamp) { $now = time(); - $date_info_comment = getdate($timestamp); - $date_info_now = getdate($now); - - /* Check if the comment was posted today or yesterday. */ - if ($date_info_comment['year'] == $date_info_now['year'] - && $date_info_comment['mon'] == $date_info_now['mon']) { - if ($date_info_comment['mday'] == $date_info_now['mday']) { - $message = _('said today'); - } else if ($date_info_comment['mday'] == $date_info_now['mday'] - 1) { - $message = _('said yesterday'); - } - return $message; - } - - /* Calculate the number of days, months or years elapsed since the specified timestamp. */ - $seconds_in_a_day = 86400; - $seconds_in_a_month = 2629744; - $seconds_in_a_year = 31556926; $time_difference = $now - $timestamp; + $date_info_now = getdate($now); - $elapsed_days = round($time_difference / $seconds_in_a_day); - $elapsed_months = round($time_difference / $seconds_in_a_month); - $elapsed_years = round($time_difference / $seconds_in_a_year); + /* Calculate the number of days, months and years elapsed since the specified timestamp. */ + $elapsed_days = round($time_difference / comment::SECONDS_IN_A_DAY); + $elapsed_months = round($time_difference / comment::SECONDS_IN_A_MONTH); + $elapsed_years = round($time_difference / comment::SECONDS_IN_A_YEAR); + $seconds_since_midnight = $date_info_now['hours'] * comment::SECONDS_IN_AN_HOUR + + $date_info_now['minutes'] * comment::SECONDS_IN_A_MINUTE + $date_info_now['seconds']; + /* Construct message depending on how much time passed. */ if ($elapsed_years > 0) { $message = sprintf(_('said %d years ago'), $elapsed_years); } else if ($elapsed_months > 0) { $message = sprintf(_('said %d months ago'), $elapsed_months); } else { - $message = sprintf(_('said %d days ago'), $elapsed_days); + if ($time_difference < $seconds_since_midnight) { + $message = _('said today'); + } else if ($time_difference < $seconds_since_midnight + comment::SECONDS_IN_A_DAY) { + $message = _('said yesterday'); + } else { + $message = sprintf(_('said %d days ago'), $elapsed_days); + } } return $message; - } + } } diff --git a/modules/comment/tests/Comment_Helper_Test.php b/modules/comment/tests/Comment_Helper_Test.php index 2a112a7d..74f784b2 100644 --- a/modules/comment/tests/Comment_Helper_Test.php +++ b/modules/comment/tests/Comment_Helper_Test.php @@ -39,4 +39,20 @@ class Comment_Helper_Test extends Unit_Test_Case { $this->assert_equal($rand, $comment->item_id); $this->assert_true($comment->datetime > time() - 10 && $comment->datetime <= time()); } + + public function format_elapsed_time_test() { + /* This test could be improved by using random numbers and specifically testing corner cases. */ + $now = time(); + + $yesterday = $now - comment::SECONDS_IN_A_DAY; + $daysago = $now - 6 * comment::SECONDS_IN_A_DAY; + $monthsago = $now - 2 * comment::SECONDS_IN_A_MONTH; + $yearsago = $now - 5 * comment::SECONDS_IN_A_YEAR; + + $this->assert_equal('said today', comment::format_elapsed_time($now)); + $this->assert_equal('said yesterday', comment::format_elapsed_time($yesterday)); + $this->assert_equal('said 6 days ago', comment::format_elapsed_time($daysago)); + $this->assert_equal('said 2 months ago', comment::format_elapsed_time($monthsago)); + $this->assert_equal('said 5 years ago', comment::format_elapsed_time($yearsago)); + } } |