forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeZoneHelper.php
More file actions
58 lines (48 loc) · 1.56 KB
/
TimeZoneHelper.php
File metadata and controls
58 lines (48 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace App\Helpers;
use App\Settings\GeneralSettings;
use DateTimeZone;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class TimeZoneHelper
{
/**
* Returns the display timezone
*/
public static function displayTimeZone(GeneralSettings $settings): string
{
// Don't translate to local time if the database already returns it.
if ($settings->db_has_timezone) {
return 'UTC';
}
return $settings->timezone
?? 'UTC';
}
/**
* Returns a collection of time zones with their offset from UTC.
*/
public static function list()
{
$seconds = 3600; // 1hr
return Cache::remember('timezones_list_collection', $seconds, function () {
$timestamp = time();
foreach (timezone_identifiers_list() as $key => $value) {
date_default_timezone_set($value);
$timezone[$value] = $value.' (UTC '.date('P', $timestamp).')';
}
return collect($timezone)->sortKeys();
});
}
/**
* Validates the time zone string provided.
*
* Ref: https://github.com/laravel/framework/blob/10.x/src/Illuminate/Validation/Concerns/ValidatesAttributes.php#L2406-L2420
*/
public static function validate($value, $parameters = [])
{
return in_array($value, timezone_identifiers_list(
constant(DateTimeZone::class.'::'.Str::upper($parameters[0] ?? 'ALL')),
isset($parameters[1]) ? Str::upper($parameters[1]) : null,
), true);
}
}