1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Shared;
|
|
|
4 |
|
|
|
5 |
use DateTimeZone;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
|
|
|
7 |
|
|
|
8 |
class TimeZone
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* Default Timezone used for date/time conversions.
|
|
|
12 |
*
|
|
|
13 |
* @var string
|
|
|
14 |
*/
|
|
|
15 |
protected static $timezone = 'UTC';
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Validate a Timezone name.
|
|
|
19 |
*
|
|
|
20 |
* @param string $timezoneName Time zone (e.g. 'Europe/London')
|
|
|
21 |
*
|
|
|
22 |
* @return bool Success or failure
|
|
|
23 |
*/
|
|
|
24 |
private static function validateTimeZone(string $timezoneName): bool
|
|
|
25 |
{
|
|
|
26 |
return in_array($timezoneName, DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC), true);
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Set the Default Timezone used for date/time conversions.
|
|
|
31 |
*
|
|
|
32 |
* @param string $timezoneName Time zone (e.g. 'Europe/London')
|
|
|
33 |
*
|
|
|
34 |
* @return bool Success or failure
|
|
|
35 |
*/
|
|
|
36 |
public static function setTimeZone(string $timezoneName): bool
|
|
|
37 |
{
|
|
|
38 |
if (self::validateTimeZone($timezoneName)) {
|
|
|
39 |
self::$timezone = $timezoneName;
|
|
|
40 |
|
|
|
41 |
return true;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
return false;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Return the Default Timezone used for date/time conversions.
|
|
|
49 |
*
|
|
|
50 |
* @return string Timezone (e.g. 'Europe/London')
|
|
|
51 |
*/
|
|
|
52 |
public static function getTimeZone(): string
|
|
|
53 |
{
|
|
|
54 |
return self::$timezone;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Return the Timezone offset used for date/time conversions to/from UST
|
|
|
59 |
* This requires both the timezone and the calculated date/time to allow for local DST.
|
|
|
60 |
*
|
|
|
61 |
* @param ?string $timezoneName The timezone for finding the adjustment to UST
|
|
|
62 |
* @param float|int $timestamp PHP date/time value
|
|
|
63 |
*
|
|
|
64 |
* @return int Number of seconds for timezone adjustment
|
|
|
65 |
*/
|
|
|
66 |
public static function getTimeZoneAdjustment(?string $timezoneName, $timestamp): int
|
|
|
67 |
{
|
|
|
68 |
$timezoneName = $timezoneName ?? self::$timezone;
|
|
|
69 |
$dtobj = Date::dateTimeFromTimestamp("$timestamp");
|
|
|
70 |
if (!self::validateTimeZone($timezoneName)) {
|
|
|
71 |
throw new PhpSpreadsheetException("Invalid timezone $timezoneName");
|
|
|
72 |
}
|
|
|
73 |
$dtobj->setTimeZone(new DateTimeZone($timezoneName));
|
|
|
74 |
|
|
|
75 |
return $dtobj->getOffset();
|
|
|
76 |
}
|
|
|
77 |
}
|