mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
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:
@@ -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
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user