This commit is contained in:
Bernd Bestel 2020-12-28 19:38:52 +01:00
parent f602719794
commit 16f400447a
No known key found for this signature in database
GPG Key ID: 71BD34C0D4891300
2 changed files with 32 additions and 14 deletions

View File

@ -45,11 +45,26 @@ class SystemApiController extends BaseApiController
public function GetSystemTime(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) public function GetSystemTime(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
{ {
$offset = 0; try
$params = $request->getQueryParams(); {
if (isset($params['offset'])) $offset = 0;
$offset = $params['offset']; $params = $request->getQueryParams();
return $this->ApiResponse($response, $this->getApplicationService()->GetSystemTime($offset)); if (isset($params['offset']))
{
if (!filter_var($params['offset'], FILTER_VALIDATE_INT))
{
throw new \Exception('Query parameter "offset" is not a valid integer');
}
$offset = $params['offset'];
}
return $this->ApiResponse($response, $this->getApplicationService()->GetSystemTime($offset));
}
catch (\Exception $ex)
{
return $this->GenericErrorResponse($response, $ex->getMessage());
}
} }
public function LogMissingLocalization(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) public function LogMissingLocalization(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)

View File

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