Added /system/time API call (#1223)

* Inital structure for /system/time API call

* Parse arguments for offset

* Correctly parsing parameters

* Fixed implimentation, added to openapi.json

* Modified DOC

* Added Sqlite3 time to output

* Fixed error with negative offset

* Review

Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
Marc Ole Bulling
2020-12-28 19:39:24 +01:00
committed by GitHub
parent 6fcc0636e8
commit 7e8f460dad
4 changed files with 135 additions and 0 deletions

View File

@@ -75,4 +75,44 @@ class ApplicationService extends BaseService
'sqlite_version' => $sqliteVersion
];
}
private static function convertToUtc(int $timestamp): string
{
$dt = new \DateTime('now', new \DateTimeZone('UTC'));
$dt->setTimestamp($timestamp);
return $dt->format('Y-m-d H:i:s');
}
private static function getSqliteLocaltime(int $offset): string
{
$pdo = new \PDO('sqlite::memory:');
if ($offset > 0)
{
return $pdo->query('SELECT datetime(\'now\', \'+' . $offset . ' seconds\', \'localtime\');')->fetch()[0];
}
else
{
return $pdo->query('SELECT datetime(\'now\', \'' . $offset . ' seconds\', \'localtime\');')->fetch()[0];
}
}
/**
* Returns the response for the API call /system/time
* @param int $offset an offset in seconds to be applied
* @return array
*/
public function GetSystemTime(int $offset = 0): array
{
$timestamp = time() + $offset;
$timeLocal = date('Y-m-d H:i:s', $timestamp);
$timeUTC = self::convertToUtc($timestamp);
return [
'timezone' => date_default_timezone_get(),
'time_local' => $timeLocal,
'time_local_sqlite3' => self::getSqliteLocaltime($offset),
'time_utc' => $timeUTC,
'timestamp' => $timestamp,
'offset' => $offset
];
}
}