Javascript Timezone获取位置类似于PHP DateTimeZone::getLocation

Javascript Timezone get Location similar to PHP DateTimeZone::getLocation?

本文关键字:DateTimeZone getLocation PHP 类似于 Timezone 获取 位置 Javascript      更新时间:2023-09-26

是否有人知道一个库,函数或API的功能与PHP DateTimeZone::getLocation - http://php.net/manual/en/datetimezone.getlocation.php相同,但用于Javascript(或NodeJS NPM)。

请注意,不要寻找代码只是为了指出我在正确的方向。试着寻找类似的东西可以找到任何东西。

例如-

我想取UTC偏移量或时区:

utc_offset: -25200,
time_zone: 'Pacific Time (US & Canada)',

然后在此基础上返回位置/地理位置:

country_code: CZ
latitude: 50.08333
longitude: 14.43333

由于时间紧迫,我决定制作一个基于PHP DateTimeZone::getLocation函数的小API。我可能会在稍后的阶段将其作为公共API。

这里是gitHub下载也:https://github.com/sputn1k/timezoneGetLocationAPI/

<?php
//usage: /?timezone=Europe/Prague
//output(JSON): {"country_code":"CZ","latitude":50.08333,"longitude":14.43333,"comments":""}
if(isset($_GET['timezone'])) {
    $timezone = $_GET['timezone'];
    try {
        $timezone = new DateTimeZone($timezone);
        output(timezone_location_get($timezone));
    } catch(Exception $error) {
        output(array('error' => 'bad timezone')); //or add $error for full message 
    }
}
function output($data){
    echo json_encode($data);
}
?>

您可以使用JodaAPI。http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html希望有帮助^^