Last active
October 23, 2024 17:13
-
-
Save mlocati/8de27740d24f98c78373b825a5cd5117 to your computer and use it in GitHub Desktop.
Convert time zone identifiers to/from PHP from/to Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* License: MIT | |
* Author: Michele Locati <[email protected]> | |
*/ | |
declare(strict_types=1); | |
set_error_handler( | |
static function ($errno, $errstr) { | |
throw new RuntimeException($errstr); | |
}, | |
-1 | |
); | |
function getXMLDocument(): DOMDocument | |
{ | |
$xml = file_get_contents('https://raw.githubusercontent.com/unicode-org/cldr/release-44-1/common/supplemental/windowsZones.xml'); | |
$doc = new DOMDocument(); | |
if (!$doc->loadXML($xml)) { | |
throw new RuntimeException('Failed to load xml'); | |
} | |
return $doc; | |
} | |
function sortTimeZoneIdentifiers(string $timeZoneA, string $timeZoneB): int | |
{ | |
$matchesA = []; | |
$matchesB = []; | |
preg_match('/^(?<pre>.*?)(?<num>[+\-]?\d+)(?<post>.*?)$/', $timeZoneA, $matchesA); | |
if ($matchesA === []) { | |
preg_match('/^(?<pre>.*?)(?<num>[+\-]?\d+)(?<post>.*?)$/', $timeZoneA . '+0', $matchesA); | |
} | |
preg_match('/^(?<pre>.*?)(?<num>[+\-]?\d+)(?<post>.*?)$/', $timeZoneB, $matchesB); | |
if ($matchesB === []) { | |
preg_match('/^(?<pre>.*?)(?<num>[+\-]?\d+)(?<post>.*?)$/', $timeZoneB . '+0', $matchesB); | |
} | |
if ($matchesA['pre'] === $matchesB['pre'] && $matchesA['post'] === $matchesB['post']) { | |
return (int) $matchesA['num'] - (int) $matchesB['num']; | |
} | |
return strcasecmp($timeZoneA, $timeZoneB); | |
} | |
function normalizePhpTimeZoneID(string $timeZoneIdentifier): string | |
{ | |
set_error_handler(static function() {}, -1); | |
try { | |
$timeZone = timezone_open($timeZoneIdentifier); | |
} finally { | |
restore_error_handler(); | |
} | |
return $timeZone instanceof DateTimeZone ? $timeZone->getName() : ''; | |
} | |
function buildWindowsToPhp(DOMDocument $doc): array | |
{ | |
$xpath = new DOMXPath($doc); | |
$mapZones = $xpath->query('/supplementalData/windowsZones/mapTimezones/mapZone'); | |
$map = []; | |
foreach ($mapZones as $mapZone) { | |
$windowsName = (string) $mapZone->getAttribute('other'); | |
if ($windowsName === '') { | |
throw new RuntimeException('Failed to retrieve Windows time zone from XML ' . $doc->saveXML($mapZone)); | |
} | |
if (!isset($map[$windowsName])) { | |
$map[$windowsName] = []; | |
} | |
$phpNames = (string) $mapZone->getAttribute('type'); | |
if ($phpNames === '') { | |
throw new RuntimeException('Failed to retrieve PHP time zones from ' . $doc->saveXML($phpNames)); | |
} | |
foreach (array_unique(preg_split('/\s+/', $phpNames, -1, PREG_SPLIT_NO_EMPTY)) as $phpName) { | |
$actualPhpName = normalizePhpTimeZoneID($phpName); | |
if ($actualPhpName === '') { | |
fwrite(STDERR, "Invalid PHP time zone name: '{$phpName}'\n"); | |
} elseif (!in_array($actualPhpName, $map[$windowsName], true)) { | |
$map[$windowsName][] = $actualPhpName; | |
} | |
} | |
} | |
foreach ($map as $windowsName => $phpNames) { | |
if ($phpNames === []) { | |
throw new RuntimeException("No PHP time zone is applicable to the Windows time zone '{$windowsName}'"); | |
} | |
} | |
uksort($map, 'sortTimeZoneIdentifiers'); | |
return array_map( | |
static function (array $phpNames): array { | |
usort($phpNames, 'sortTimeZoneIdentifiers'); | |
return $phpNames; | |
}, | |
$map | |
); | |
} | |
function buildPhpToWindows(array $windowsToPhp): array | |
{ | |
$map = []; | |
foreach ($windowsToPhp as $windows => $phpNames) { | |
foreach ($phpNames as $phpName) { | |
if (isset($map[$phpName])) { | |
throw new RuntimeException("The PHP time zone '{$phpName}' is applicable to more than one Windows time zone"); | |
} | |
$map[$phpName] = $windows; | |
} | |
} | |
uksort($map, 'sortTimeZoneIdentifiers'); | |
return $map; | |
} | |
function quote(string $s): string | |
{ | |
return "'" . addcslashes($s, "'\\") . "'"; | |
} | |
$doc = getXMLDocument(); | |
$windowsToPhp = buildWindowsToPhp($doc); | |
$phpToWindows = buildPhpToWindows($windowsToPhp); | |
echo "const WINDOWS_TO_PHP = [\n"; | |
foreach ($windowsToPhp as $windows => $phps) { | |
echo ' ', quote($windows), ' => [' . implode(', ', array_map('quote', $phps)), '],', "\n"; | |
} | |
echo "\n"; | |
echo "const PHP_TO_WINDOWS = [\n"; | |
foreach ($phpToWindows as $php => $windows) { | |
echo ' ', quote($php), ' => ', quote($windows), ",\n"; | |
} | |
echo "];\n"; | |
echo "\n"; | |
echo "const PHP_TO_WINDOWS = [\n"; | |
foreach ($phpToWindows as $php => $windows) { | |
echo ' ', quote($php), ' => ', quote($windows), ",\n"; | |
} | |
echo "];\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: