简体   繁体   English

获取给定时区的DST / STD日期和时间(JavaScript)

[英]Get DST/STD dates and times for a given timezone (Javascript)

I am developping an embedded website contained within a windows CE device. 我正在开发Windows CE设备中包含的嵌入式网站。 See it as if it was the web configuration interface of your router. 可以将其视为路由器的Web配置界面。 Everything is contained within a really small footprint of memory, the entire website is less than 500KB with every script, html, css and icons. 一切都包含在很小的内存中,整个网站的每个脚本,html,css和图标都少于500KB。

We have to assume that the user that is going to 'browse' into that interface does not have access to the internet (LAN only) so no 'online' solution here. 我们必须假设要“浏览”该界面的用户无法访问互联网(仅LAN),因此此处没有“在线”解决方案。

I am looking for a solution so the user choose his timezone and the code will get all the DST/STD times and dates for the next 10-20 years at least and downloaded them to the device that will run autonomously after that and at specific dates, will change its time to DST/STD by itself. 我正在寻找一种解决方案,因此用户可以选择自己的时区,并且代码至少将获得未来10-20年的所有DST / STD时间和日期,并将其下载到设备上,然后在该日期和特定日期自动运行,将自己的时间更改为DST / STD。 Current application is custom (not windowce api related) and needs the DST/STD date pairs . 当前应用程序是自定义的(与windowce api不相关),并且需要DST / STD日期对

iana.org is maintaining a db for like every location in the world. iana.org正在维护世界上每个位置的数据库。 I also saw that moment-timezone (javascript interface) is 'packaging' this data in a very compact package 25kb zipped. 我还看到了moment-timezone(javascript界面​​)将这些数据“打包”在一个非常紧凑的25kb压缩包中。

I need to know if it is possible to: 我需要知道是否可以:

  1. 'Extract' a main tz list from this DB so the user can choose its own tz. 从该数据库中“提取”主tz列表,以便用户选择自己的tz。 I looked at their doc but example didn't work: 我看了看他们的文档,但示例不起作用:

var itsTimeZones = moment.tz.names();

2- Extract the next 10-20 years of DST/STD dates/times for a chosen zone ? 2-提取选定区域的未来10-20年DST / STD日期/时间? I haven't saw any documentation anywhere on this topic. 在该主题的任何地方,我都没有看到任何文档。 But since it's kind of the purpose of such a database, i would say it must be burried in there somewhere, need a way to dig it out. 但这是出于此类数据库的目的,我想说它必须埋藏在某个地方,需要一种挖掘方法。

3- If moment timezone is not the right track to go, anybody has a solution that will fullfill that requirement ? 3-如果时区不是正确的选择,那么有人有解决方案可以满足该要求吗?

Thanxs 谢谢

My solution for extracting DST list for a given zone from moment-timezone. 我从矩时区提取给定区域的DST列表的解决方案。 1. "theTz" is a numerical index from a change on a select in the webpage. 1.“ theTz”是网页中所选内容更改后的数字索引。 2. select is populated with "List". 2. select填充有“列表”。

var itsTz =
{
    "List" : moment.tz.names(), // Get all zone 'Names'
    "Transitions" : [],
};


function TimeZoneChanged(theTz)
{
    var myZone = moment.tz.zone(itsTz.List[theTz]);  // Get zone by its name from List
    var myTime = moment.tz(moment(), myZone.name);   // Start today
    var myResult;

    // Build transition list according to new zone selected
    itsTz.Transitions = [];
    do
    {
        myResult = GetNextTransition(myZone, myTime);

        if(myResult != null)
        {
            itsTz.Transitions.push(moment(myResult).format("YYYY/MM/DD"));
            myTime = moment.tz(myResult, myZone.name);  // Get next from date found
        }
    }while(myResult != null);
}

function GetNextTransition(theZone, theTime)
{
    var myResult;

    for(var i = 0; i < theZone.untils.length; i++) 
    {
        if(theZone.untils[i] > theTime) 
        {
            theZone.untils[i] == Infinity ? myResult = null : myResult = new moment(theZone.untils[i]).format();
            return  myResult;
        }
    }
}

Results for 'America/Toronto: 'America / Toronto的结果:

2017/03/12, 2017/11/05, 2018/03/11, 2018/11/04, 2019/03/10, 2019/11/03, 2020/03/08, 2020/11/01, 2021/03/14, 2021/11/07, 2022/03/13, 2022/11/06, 2023/03/12, 2023/11/05, 2024/03/10, 2024/11/03, 2025/03/09, 2025/11/02, 2026/03/08, 2026/11/01, 2027/03/14, 2027/11/07, 2028/03/12, 2028/11/05, 2029/03/11, 2029/11/04, 2030/03/10, 2030/11/03, 2031/03/09, 2031/11/02, 2032/03/14, 2032/11/07, 2033/03/13, 2033/11/06, 2034/03/12, 2034/11/05, 2035/03/11, 2035/11/04, 2036/03/09, 2036/11/02, 2037/03/08, 2037/11/01, 2017/03 / 12、2017 / 11 / 05、2018 / 03 / 11、2018 / 11 / 04、2019 / 03 / 10、2019 / 11 / 03、2020 / 03 / 08、2020 / 11 / 01、2021 / 03 / 14、2021 / 11 / 07、2022 / 03 / 13、2022 / 11 / 06、2023 / 03 / 12、2023 / 11 / 05、2024 / 03 / 10、2024 / 11 / 03、2025 / 03 / 09、2025 / 11 / 02、2026 / 03 / 08、2026 / 11 / 01、2027 / 03 / 14、2027 / 11 / 07、2028 / 03 / 12、2028 / 11 / 05、2029 / 03/11, 2029/11 / 04、2030 / 03 / 10、2030 / 11 / 03、2031 / 03 / 09、2031 / 11 / 02、2032 / 03 / 14、2032 / 11 / 07、2033 / 03 / 13、2033 / 11 / 06、2034 / 03 / 12、2034 / 11 / 05、2035 / 03 / 11、2035 / 11 / 04、2036 / 03 / 09、2036 / 11 / 02、2037 / 03 / 08、2037 / 11 / 01,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM