From 691c1781fad32fb21d96d0522c039bb1bddca928 Mon Sep 17 00:00:00 2001 From: Sergei Miami Date: Sun, 17 Dec 2023 04:42:18 +0200 Subject: [PATCH] Carbon added --- composer.json | 4 +++- joker.php | 2 +- phpunit.xml.dist | 3 +++ src/Helper/Strings.php | 12 ++++++++---- src/Plugin/Twitch.php | 5 +++-- tests/Helper/StringsTest.php | 22 ++++++++++++++++++++++ 6 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 tests/Helper/StringsTest.php diff --git a/composer.json b/composer.json index e4d8df5..07c8b80 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ } ], "require": { + "php": "^7.4", "ext-gd": "*", "ext-json": "*", "ext-dom": "*", @@ -29,7 +30,8 @@ "mnapoli/silly": "^1.7", "guzzlehttp/guzzle": "^7.3", "imangazaliev/didom": "^1.18", - "wamania/php-stemmer": "^3.0" + "wamania/php-stemmer": "^3.0", + "nesbot/carbon": "^2.72" }, "autoload": { "psr-4": { diff --git a/joker.php b/joker.php index f31adb2..212b0f1 100644 --- a/joker.php +++ b/joker.php @@ -74,7 +74,7 @@ 'bio' => implode("\n", [ 'Your name is Joker or Джокер. You are russian-speaking friend, that answers with sarcastic responses and funny jokes.', 'Your author is Sergei Miami and BlackCrystal team.', - 'You live in Tallinn. Today is ' . date('Y-m-d') . ' time is ' . date('H:i'), + 'You live in Tallinn, today is ' . date(DATE_RFC2822), ]), 'model' => 'gpt-4', 'temperature' => 0.5, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b3143cf..111d8a8 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,9 @@ processIsolation="false" stopOnFailure="false"> + + ./tests/Helper + ./tests/Parser diff --git a/src/Helper/Strings.php b/src/Helper/Strings.php index d9fd3dc..1d72947 100644 --- a/src/Helper/Strings.php +++ b/src/Helper/Strings.php @@ -2,6 +2,8 @@ namespace Joker\Helper; +use Carbon\Carbon; + class Strings { @@ -50,14 +52,16 @@ public static function timeElapsed( $datetime, $full = false) */ public static function diffTimeInWords($from,$to) { - $date1 = new \DateTime("@$from"); - $date2 = new \DateTime("@$to"); - $interval = date_diff($date1, $date2); + + $date1 = new \DateTime( is_numeric($from) ? "@$from" : "$from"); + $date2 = new \DateTime( is_numeric($to) ? "@$to" : "$to"); + $interval = $date1->diff($date2); $result = []; foreach (['%y'=>'years', '%m' => 'months', '%d' => 'days', '%h' => 'hours', '%i' => 'minutes', '%s' => 'seconds'] as $key => $value) + { if ($num = $interval->format($key)) $result[] = "$num $value"; - + } return implode(" ", $result); } diff --git a/src/Plugin/Twitch.php b/src/Plugin/Twitch.php index 7d9fbe8..c0de60d 100644 --- a/src/Plugin/Twitch.php +++ b/src/Plugin/Twitch.php @@ -12,6 +12,7 @@ namespace Joker\Plugin; +use Carbon\Carbon; use Joker\Helper\Strings; use Joker\Parser\Update; @@ -62,13 +63,13 @@ public function searchChannels( $query ) foreach ($array['data'] as $item) { // if (count($result) > 10) break; - $result[] = trim( implode( " ", [ + $result[] = trim( implode( ' ', [ // online/offline $item['is_live'] ?'🌕':'🌑', // title and a link "" . trim($item['title']) . "", // started time - $item['started_at'] ? ' started '. Strings::diffTimeInWords($item['started_at'], time()) . ' ago' : '' + $item['started_at'] ? ' started '. Carbon::parse($item['started_at'])->diffForHumans(['short' => true, 'parts' => 3]) : '' , ])); } diff --git a/tests/Helper/StringsTest.php b/tests/Helper/StringsTest.php new file mode 100644 index 0000000..d02ee94 --- /dev/null +++ b/tests/Helper/StringsTest.php @@ -0,0 +1,22 @@ + + */ + +namespace Tests\Helper; + +use PHPUnit\Framework\TestCase; +use Joker\Helper\Strings; + +class StringsTest extends TestCase +{ + + public function testDiffTimeInWords() + { + $this->assertEquals("18 hours 11 minutes 9 seconds", Strings::diffTimeInWords("2023-12-16T07:51:57Z", 1702778586)); + $this->assertEquals("18 hours 11 minutes 9 seconds", Strings::diffTimeInWords(1702713117, 1702778586)); + } + +}