Skip to content

Commit

Permalink
Merge pull request #58 from miamibc/carbon
Browse files Browse the repository at this point in the history
Carbon added
  • Loading branch information
miamibc authored Dec 17, 2023
2 parents bb09b94 + 813721c commit 4a5b1b3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

jobs:
build:
name: Unit tests

runs-on: ubuntu-latest

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
}
],
"require": {
"php": ">=7.4",
"ext-gd": "*",
"ext-json": "*",
"ext-dom": "*",
Expand All @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion joker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Helper">
<directory suffix="Test.php">./tests/Helper</directory>
</testsuite>
<testsuite name="Parser">
<directory suffix="Test.php">./tests/Parser</directory>
</testsuite>
Expand Down
12 changes: 8 additions & 4 deletions src/Helper/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Joker\Helper;

use Carbon\Carbon;

class Strings
{

Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Plugin/Twitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Joker\Plugin;

use Carbon\Carbon;
use Joker\Helper\Strings;
use Joker\Parser\Update;

Expand Down Expand Up @@ -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
"<a href=\"https://www.twitch.tv/{$item['display_name']}\">" . trim($item['title']) . "</a>",
// 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]) : '' ,
]));
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Helper/StringsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
*
* @package joker-telegram-bot
* @author Sergei Miami <[email protected]>
*/

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));
}

}

0 comments on commit 4a5b1b3

Please sign in to comment.