Inital structure for /system/time API call

This commit is contained in:
Marc Ole Bulling 2020-12-28 11:23:20 +01:00
parent 6fcc0636e8
commit 669d2d1e48
No known key found for this signature in database
GPG Key ID: C126AFC2A47B06FF
3 changed files with 33 additions and 0 deletions

View File

@ -43,6 +43,11 @@ class SystemApiController extends BaseApiController
return $this->ApiResponse($response, $this->getApplicationService()->GetSystemInfo());
}
public function GetSystemTime(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
return $this->ApiResponse($response, $this->getApplicationService()->GetSystemTime());
}
public function LogMissingLocalization(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{
if (GROCY_MODE === 'dev')

View File

@ -145,6 +145,7 @@ $app->group('/api', function (RouteCollectorProxy $group) {
// System
$group->get('/system/info', '\Grocy\Controllers\SystemApiController:GetSystemInfo');
$group->get('/system/time', '\Grocy\Controllers\SystemApiController:GetSystemTime');
$group->get('/system/db-changed-time', '\Grocy\Controllers\SystemApiController:GetDbChangedTime');
$group->get('/system/config', '\Grocy\Controllers\SystemApiController:GetConfig');
$group->post('/system/log-missing-localization', '\Grocy\Controllers\SystemApiController:LogMissingLocalization');

View File

@ -75,4 +75,31 @@ class ApplicationService extends BaseService
'sqlite_version' => $sqliteVersion
];
}
private static function convertToUtc(int $timestamp):string
{
$timestamp = time();
$dt = new DateTime('now', new DateTimeZone('UTC'));
$dt->setTimestamp($timestamp);
return $dt->format('Y-m-d H:i:s');
}
/**
* Returns the response for the API call /system/time
* @param int $offset an offset to be applied
* @return array
*/
public function GetSystemTime(int $offset = 0):array
{
$timestamp = time();
$timeLocal = date('Y-m-d H:i:s', $timestamp);
$timeUTC = self::convertToUtc($timestamp);
return [
'timezone' => date_default_timezone_get(),
'time_local' => $timeLocal,
'time_utc' => $timeUTC,
'timestamp' => $timestamp,
'offset' => $offset
];
}
}