PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Shared / TimeZone.php

TimeZone.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Shared/TimeZone.php

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