From 669d2d1e48d1b80c08e9b3d930504da868cd604d Mon Sep 17 00:00:00 2001 From: Marc Ole Bulling Date: Mon, 28 Dec 2020 11:23:20 +0100 Subject: [PATCH] Inital structure for /system/time API call --- controllers/SystemApiController.php | 5 +++++ routes.php | 1 + services/ApplicationService.php | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/controllers/SystemApiController.php b/controllers/SystemApiController.php index debadc9f..99e2a187 100644 --- a/controllers/SystemApiController.php +++ b/controllers/SystemApiController.php @@ -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') diff --git a/routes.php b/routes.php index 333057f1..4ce72312 100644 --- a/routes.php +++ b/routes.php @@ -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'); diff --git a/services/ApplicationService.php b/services/ApplicationService.php index 6b4679f3..6dc78dfa 100644 --- a/services/ApplicationService.php +++ b/services/ApplicationService.php @@ -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 + ]; + } }