Skip to content

Commit

Permalink
Issue #194: Fix incompatible float to int conversion (#202)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebi94nbg <sebastian.kraetzig@4g-server.eu>
  • Loading branch information
Sebbo94BY and Sebi94nbg committed Aug 3, 2023
1 parent 47a12c5 commit e133806
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Helper/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace PlanetTeamSpeak\TeamSpeak3Framework\Helper;

use DateTime;
use PlanetTeamSpeak\TeamSpeak3Framework\TeamSpeak3;

/**
Expand Down Expand Up @@ -85,13 +86,16 @@ public static function bytes(int $bytes, int $precision = 10): string
* @todo: Handle negative integer $seconds, or invalidate
*
*/
public static function seconds(int $seconds, bool $is_ms = false, string $format = "%dD %02d:%02d:%02d"): string
public static function seconds(int $seconds, bool $is_ms = false, string $format = "%aD %H:%I:%S"): string
{
if ($is_ms) {
$seconds = $seconds / 1000;
}

return sprintf($format, $seconds / 60 / 60 / 24, ($seconds / 60 / 60) % 24, ($seconds / 60) % 60, $seconds % 60);
$current_datetime = new DateTime("@0");
$seconds_datetime = new DateTime("@$seconds");

return $current_datetime->diff($seconds_datetime)->format($format);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Helper/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ public function testConvertSecondsToHumanReadable()
$this->assertEquals('1D 23:59:59', $output);
$this->assertIsString($output);

$output = Convert::seconds(90.083);
$this->assertEquals('0D 00:01:30', $output);
$this->assertIsString($output);

// @todo: Enable after ::seconds() can handle negative integers
//$output = Convert::seconds(-1);
//$this->assertEquals('-0D 00:00:01', $output);
Expand Down

0 comments on commit e133806

Please sign in to comment.