add projet
This commit is contained in:
Executable
+96
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class TimeController extends BaseApiController
|
||||
{
|
||||
protected string $defaultUserTimezone = 'America/New_York';
|
||||
protected string $serverTimezone;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->serverTimezone = config('app.timezone', 'UTC');
|
||||
}
|
||||
|
||||
protected function detectUserTimezone(): string
|
||||
{
|
||||
$header = $this->laravelRequest->headers->get('X-Timezone');
|
||||
if (!empty($header) && in_array($header, timezone_identifiers_list(), true)) {
|
||||
return $header;
|
||||
}
|
||||
|
||||
$param = $this->laravelRequest->get('timezone');
|
||||
if (!empty($param) && in_array($param, timezone_identifiers_list(), true)) {
|
||||
return $param;
|
||||
}
|
||||
|
||||
return $this->defaultUserTimezone;
|
||||
}
|
||||
|
||||
public function convert()
|
||||
{
|
||||
$data = $this->payloadData();
|
||||
if (empty($data['time']) || empty($data['direction'])) {
|
||||
return $this->respondValidationError(['time' => 'Required', 'direction' => 'Required']);
|
||||
}
|
||||
|
||||
$userTz = $this->detectUserTimezone();
|
||||
$time = trim((string) $data['time']);
|
||||
$direction = strtolower((string) $data['direction']);
|
||||
|
||||
try {
|
||||
if ($direction === 'toutc') {
|
||||
$converted = Carbon::parse($time, $userTz)
|
||||
->setTimezone('UTC')
|
||||
->toDateTimeString();
|
||||
|
||||
return $this->success([
|
||||
'user_timezone' => $userTz,
|
||||
'server_timezone' => 'UTC',
|
||||
'original_time' => $time,
|
||||
'converted_time' => $converted,
|
||||
], 'Conversion completed');
|
||||
}
|
||||
|
||||
if ($direction === 'tolocal') {
|
||||
$converted = Carbon::parse($time, 'UTC')
|
||||
->setTimezone($userTz)
|
||||
->toDateTimeString();
|
||||
|
||||
return $this->success([
|
||||
'user_timezone' => $userTz,
|
||||
'server_timezone' => 'UTC',
|
||||
'original_time' => $time,
|
||||
'converted_time' => $converted,
|
||||
], 'Conversion completed');
|
||||
}
|
||||
|
||||
return $this->respondValidationError(['direction' => 'Must be toUTC or toLocal']);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->respondError('Failed to convert time: ' . $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
public function now()
|
||||
{
|
||||
$userTz = $this->detectUserTimezone();
|
||||
|
||||
try {
|
||||
$local = Carbon::now($userTz)->toDateTimeString();
|
||||
$utc = Carbon::now('UTC')->toDateTimeString();
|
||||
|
||||
return $this->success([
|
||||
'user_timezone' => $userTz,
|
||||
'server_timezone' => 'UTC',
|
||||
'local_time' => $local,
|
||||
'utc_time' => $utc,
|
||||
], 'Current time retrieved');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->respondError('Failed to determine current time', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user