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

562 views Asked by At

I am developping an embedded website contained within a windows CE device. See it as if it was the web configuration interface of your router. Everything is contained within a really small footprint of memory, the entire website is less than 500KB with every script, html, css and icons.

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.

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. Current application is custom (not windowce api related) and needs the DST/STD date pairs.

iana.org is maintaining a db for like every location in the world. I also saw that moment-timezone (javascript interface) is 'packaging' this data in a very compact package 25kb zipped.

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. 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 ? 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 ?

Thanxs

1

There are 1 answers

1
Philippe Brodeur On

My solution for extracting DST list for a given zone from moment-timezone. 1. "theTz" is a numerical index from a change on a select in the webpage. 2. select is populated with "List".

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:

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,