diff options
author | Mario Vavti <mario@mariovavti.com> | 2024-12-07 23:49:17 +0100 |
---|---|---|
committer | Mario Vavti <mario@mariovavti.com> | 2024-12-07 23:49:17 +0100 |
commit | bb0a22ee582c7268431ccb6e44ffc6378ca00093 (patch) | |
tree | 575b4526fe33abe5e2c0a5e7644df08dd7e2a4ea | |
parent | e88ae54bef268e455b6d6028a697e9dbd98a7853 (diff) | |
download | volse-hubzilla-bb0a22ee582c7268431ccb6e44ffc6378ca00093.tar.gz volse-hubzilla-bb0a22ee582c7268431ccb6e44ffc6378ca00093.tar.bz2 volse-hubzilla-bb0a22ee582c7268431ccb6e44ffc6378ca00093.zip |
add test for relative_time()
-rw-r--r-- | include/datetime.php | 15 | ||||
-rw-r--r-- | tests/unit/includes/DatetimeTest.php | 53 | ||||
-rw-r--r-- | vendor/composer/installed.php | 4 |
3 files changed, 64 insertions, 8 deletions
diff --git a/include/datetime.php b/include/datetime.php index e23a11741..89e2876d0 100644 --- a/include/datetime.php +++ b/include/datetime.php @@ -268,12 +268,15 @@ function relative_date($posted_date, $format = null) { return $abs; } - -function relative_time($timestamp) { - $timestamp = datetime_convert('UTC', date_default_timezone_get(), $timestamp); - - $now = new DateTime(); - $time = new DateTime($timestamp); +/** + * @brief Returns a relative time string like 3 seconds ago. + * @param string $posted_date (UTC) + * @param DateTime $now (optional) + * @return string with relative time + */ +function relative_time($timestamp, $now = new DateTime()) { + $localtime = datetime_convert('UTC', date_default_timezone_get(), $timestamp); + $time = new DateTime($localtime); $interval = $now->diff($time); diff --git a/tests/unit/includes/DatetimeTest.php b/tests/unit/includes/DatetimeTest.php new file mode 100644 index 000000000..f8c480449 --- /dev/null +++ b/tests/unit/includes/DatetimeTest.php @@ -0,0 +1,53 @@ +<?php +/** + * tests function from include/datetime.php + * + * @package test.util + */ + +use Zotlabs\Tests\Unit\UnitTestCase; + +class DatetimeTest extends UnitTestCase { + + // Test when the timestamp is in the past + public function test_relative_time_past() { + $now = new DateTime('2024-12-07 00:00:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2023-12-05 10:30:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('1 year ago', $result); + } + + // Test when the timestamp is in the future + public function test_relative_time_future() { + $now = new DateTime('2024-12-07 00:00:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-09 12:00:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('in 2 days', $result); + } + + // Test for "now" case (timestamp exactly equal to current time) + public function test_relative_time_now() { + $now = new DateTime('2024-12-07 00:00:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-07 00:00:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('now', $result); + } + + // Test for future time with smaller units (e.g., minutes) + public function test_relative_time_future_minutes() { + $now = new DateTime('2024-12-07 10:30:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-07 10:35:00'); + $result = relative_time($timestamp, $now); + $this->assertEquals('in 5 minutes', $result); + } + + // Test for past time with smaller units (e.g., seconds) + public function test_relative_time_past_seconds() { + $now = new DateTime('2024-12-07 10:30:00'); + $timestamp = datetime_convert(date_default_timezone_get(), 'UTC', '2024-12-07 10:29:58'); + $result = relative_time($timestamp, $now); + $this->assertEquals('2 seconds ago', $result); + } +} + + diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index f2f9fe2d9..e43f8b8c7 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -3,7 +3,7 @@ 'name' => 'zotlabs/hubzilla', 'pretty_version' => 'dev-9.4RC', 'version' => 'dev-9.4RC', - 'reference' => '9fa18a55579bf4eafc4484536ed29335cd98b2a9', + 'reference' => 'e88ae54bef268e455b6d6028a697e9dbd98a7853', 'type' => 'application', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -367,7 +367,7 @@ 'zotlabs/hubzilla' => array( 'pretty_version' => 'dev-9.4RC', 'version' => 'dev-9.4RC', - 'reference' => '9fa18a55579bf4eafc4484536ed29335cd98b2a9', + 'reference' => 'e88ae54bef268e455b6d6028a697e9dbd98a7853', 'type' => 'application', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), |