Checking for daylight saving

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RobG

    Checking for daylight saving

    I was investigating a function to determine whether daylight saving
    was being observed on a particular date (given the platform's regional
    settings) and came across a suggestion at merlyn.com to test the time
    zone offset on a variety of dates to see if it changes. Based on
    that, I developed the following checkDST() function which, as far as I
    can tell, should be sufficient.

    It checks either the date passed to it or the current date with 3
    other dates, each 3 months further into the future. If any of their
    time zones differs from the epoch then either:

    true - daylight saving is being observed on the date, or

    false - daylight saving is observed but not on the date

    are returned. If no difference is found, undefined is returned. I
    explicitly return undefined as a matter of style rather than let the
    loop peter out and return undefined by default.

    Can anyone suggest whether the algorithm is appropriate and if my
    implementation is OK? It will fail if there are changes to time zone
    offsets that are not related to daylight saving, and if changes occur
    for a period of less than 3 months and fall within the dates tested.

    As far as I know, there isn't anywhere that does that, but they might
    be accommodated by an "acceptable range" test (say allow changes of up
    to 5 minutes) and checking for symmetry - if the time zone is
    different in x months time, check if it was also different (12 - x)
    months ago, and check if it differed by the same amount taking into
    consideration the acceptable range.


    function checkDST(d) {

    d = d || new Date();

    var x = new Date(d);
    var dt = d.getTimezoneOf fset();
    var xt;

    for (var i=0; i<3; i++) {
    x.setMonth(x.ge tMonth() + 3)
    xt = x.getTimezoneOf fset();

    if (dt != xt) {
    return dt < xt;
    }
    }
    return undefined;
    }

    function showDST(){

    var x = isDST(new Date());
    var msg;

    if (x === true) {
    msg = 'Daylight saving is observed here ' +
    'and is in force.';
    } else if (x === false) {
    msg = 'Daylight saving is observed here, ' +
    'but not at the moment.';
    } else {
    msg = 'Daylight saving is not observed here at all.'
    }
    alert(x + ': ' + msg);
    }

    showDST();



    --
    Rob
  • Randy Webb

    #2
    Re: Checking for daylight saving

    RobG said the following on 12/20/2007 1:27 AM:

    <snip>
    function checkDST(d) {
    <snip>
    function showDST(){
    >
    var x = isDST(new Date());
    isDST is not defined. FF2.0
    Object Expected. IE7.

    Long day today?

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

    Comment

    • RobG

      #3
      Re: Checking for daylight saving

      On Dec 20, 4:45 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
      RobG said the following on 12/20/2007 1:27 AM:
      >
      <snip>
      >
      function checkDST(d) {
      >
      <snip>
      >
      function showDST(){
      >
      var x = isDST(new Date());
      That should of course be:

      var x = checkDST(new Date());
      >
      isDST is not defined. FF2.0
      Object Expected. IE7.
      >
      Long day today?
      :-x bugger...


      I changed the name of the function just before posting - does it work
      for you? I can't mess with my regional settings for a little while
      yet, I'll test it more when I can.


      --
      Rob

      Comment

      • Randy Webb

        #4
        Re: Checking for daylight saving

        RobG said the following on 12/20/2007 2:01 AM:
        On Dec 20, 4:45 pm, Randy Webb <HikksNotAtH... @aol.comwrote:
        >RobG said the following on 12/20/2007 1:27 AM:
        >>
        ><snip>
        >>
        >>function checkDST(d) {
        ><snip>
        >>
        >>function showDST(){
        >> var x = isDST(new Date());
        >
        That should of course be:
        >
        var x = checkDST(new Date());
        I changed the name of the function.
        >isDST is not defined. FF2.0
        >Object Expected. IE7.
        >>
        >Long day today?
        >
        :-x bugger...
        >
        >
        I changed the name of the function just before posting - does it work
        for you? I can't mess with my regional settings for a little while
        yet, I'll test it more when I can.
        I tested it for about 5 minutes fooling with the date on my computer. I
        never did change any time zone settings. I can only tell you that it got
        it right for my location.

        --
        Randy
        Chance Favors The Prepared Mind
        comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
        Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

        Comment

        • -Lost

          #5
          Re: Checking for daylight saving

          Response to Randy Webb <HikksNotAtHome @aol.com>:
          I tested it for about 5 minutes fooling with the date on my
          computer. I never did change any time zone settings. I can only
          tell you that it got it right for my location.
          I don't suppose something like this would help?

          <URL: http://www.nirsoft.net/utils/run_as_date.htm l>

          ....most likely not, but it came to mind.

          --
          -Lost
          Remove the extra words to reply by e-mail. Don't e-mail me. I am
          kidding. No I am not.

          Comment

          • Randy Webb

            #6
            Re: Checking for daylight saving

            -Lost said the following on 12/20/2007 3:41 AM:
            Response to Randy Webb <HikksNotAtHome @aol.com>:
            >
            >I tested it for about 5 minutes fooling with the date on my
            >computer. I never did change any time zone settings. I can only
            >tell you that it got it right for my location.
            >
            I don't suppose something like this would help?
            >
            <URL: http://www.nirsoft.net/utils/run_as_date.htm l>
            >
            ...most likely not, but it came to mind.
            >
            Not for what Rob is wanting to test. The only way to know if it is
            getting it correct in different time zones is to change the time zone
            settings. It is just as trivial to do that for testing as changing the
            time/date. I just didn't do it when I was testing. Right click the clock
            on the taskbar in Windows>Adjust Date and Time>Time Zone tab. Change it,
            apply it, test it, repeat. There is also a setting there for
            automatically changing the time for DST. With it unchecked, the test
            gives incorrect results for me when testing my own time zone.

            "undefined: Daylight saving is not observed here at all. "

            And that is in Eastern Standard Time Zone in the US.

            --
            Randy
            Chance Favors The Prepared Mind
            comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
            Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

            Comment

            • Evertjan.

              #7
              Re: Checking for daylight saving

              Randy Webb wrote on 20 dec 2007 in comp.lang.javas cript:
              Not for what Rob is wanting to test. The only way to know if it is
              getting it correct in different time zones is to change the time zone
              settings. It is just as trivial to do that for testing as changing the
              time/date. I just didn't do it when I was testing. Right click the
              clock on the taskbar in Windows>Adjust Date and Time>Time Zone tab.
              Change it, apply it, test it, repeat. There is also a setting there
              for automatically changing the time for DST. With it unchecked, the
              test gives incorrect results for me when testing my own time zone.
              >
              "undefined: Daylight saving is not observed here at all. "
              >
              And that is in Eastern Standard Time Zone in the US.
              This should work, IMHO:

              <script type='text/javascript'>

              var d1 = new Date(2007,0,1). getTimezoneOffs et()
              var d2 = new Date(2007,6,1). getTimezoneOffs et()

              var t = 'This computer is set for a zone with'

              if (d1==d2)
              alert(t + 'out summertime offset');
              else if (d1>d2)
              alert(t + ' summertime offset in Northern hemisphere');
              else
              alert(t + ' summertime offset in Southern hemisphere');

              </script>

              IE7 and Central European Time Zone tested.

              That such offset saves daylight is absolute nonsense,
              though it may save energy consumption.

              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • Randy Webb

                #8
                Re: Checking for daylight saving

                Evertjan. said the following on 12/20/2007 4:59 PM:
                Randy Webb wrote on 20 dec 2007 in comp.lang.javas cript:
                >
                >Not for what Rob is wanting to test. The only way to know if it is
                >getting it correct in different time zones is to change the time zone
                >settings. It is just as trivial to do that for testing as changing the
                >time/date. I just didn't do it when I was testing. Right click the
                >clock on the taskbar in Windows>Adjust Date and Time>Time Zone tab.
                >Change it, apply it, test it, repeat. There is also a setting there
                >for automatically changing the time for DST. With it unchecked, the
                >test gives incorrect results for me when testing my own time zone.
                >>
                >"undefined: Daylight saving is not observed here at all. "
                >>
                >And that is in Eastern Standard Time Zone in the US.
                >
                This should work, IMHO:
                >
                <script type='text/javascript'>
                >
                var d1 = new Date(2007,0,1). getTimezoneOffs et()
                var d2 = new Date(2007,6,1). getTimezoneOffs et()
                >
                var t = 'This computer is set for a zone with'
                >
                if (d1==d2)
                alert(t + 'out summertime offset');
                else if (d1>d2)
                alert(t + ' summertime offset in Northern hemisphere');
                else
                alert(t + ' summertime offset in Southern hemisphere');
                >
                </script>
                >
                IE7 and Central European Time Zone tested.
                It tells me I have a summertime offset in Northern hemisphere, but, it
                doesn't tell me whether I use Daylight Savings Time or not. No setting
                on the PC can either. Arizona, USA, is split on DST. Parts of the state
                use it and parts don't. Indiana was another state that just recently
                started using it.
                That such offset saves daylight is absolute nonsense,
                though it may save energy consumption.
                It doesn't "save daylight", it just changes the hours in the day when it
                is there. Personally, I wish they would just do away with the entire
                concept as it is a broken phenomenon now.

                --
                Randy
                Chance Favors The Prepared Mind
                comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
                Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                Comment

                • -Lost

                  #9
                  Re: Checking for daylight saving

                  Response to Randy Webb <HikksNotAtHome @aol.com>:
                  >That such offset saves daylight is absolute nonsense,
                  >though it may save energy consumption.
                  >
                  It doesn't "save daylight", it just changes the hours in the day
                  when it is there. Personally, I wish they would just do away with
                  the entire concept as it is a broken phenomenon now.
                  Same here. Halfway through the week (this past DST change) we kept
                  wondering why no one updated their clocks and come to find out we were
                  the dummies.

                  Why in the world did Congress or whoever change that at the last moment
                  anyway?

                  --
                  -Lost
                  Remove the extra words to reply by e-mail. Don't e-mail me. I am
                  kidding. No I am not.

                  Comment

                  • -Lost

                    #10
                    Re: Checking for daylight saving

                    Response to Randy Webb <HikksNotAtHome @aol.com>:
                    You can get to it without going into the Control Panel but it
                    changes it in the same place. Right click the clock on the Windows
                    Taskbar>Adjust Date/Time.
                    Or double-click.

                    --
                    -Lost
                    Remove the extra words to reply by e-mail. Don't e-mail me. I am
                    kidding. No I am not.

                    Comment

                    • Randy Webb

                      #11
                      Re: Checking for daylight saving

                      -Lost said the following on 12/20/2007 9:24 PM:
                      Response to Randy Webb <HikksNotAtHome @aol.com>:
                      >
                      >>That such offset saves daylight is absolute nonsense,
                      >>though it may save energy consumption.
                      >It doesn't "save daylight", it just changes the hours in the day
                      >when it is there. Personally, I wish they would just do away with
                      >the entire concept as it is a broken phenomenon now.
                      >
                      Same here. Halfway through the week (this past DST change) we kept
                      wondering why no one updated their clocks and come to find out we were
                      the dummies.
                      >
                      Why in the world did Congress or whoever change that at the last moment
                      anyway?
                      <shrugWhy do politicians do any of the things they do?

                      One reason I read (and don't know if it is true or not) is:

                      A new law to extend DST to the first Sunday in November will take effect
                      in 2007, with the purpose of providing trick-or-treaters more light and
                      therefore more safety from traffic accidents.

                      Go figure.

                      --
                      Randy
                      Chance Favors The Prepared Mind
                      comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
                      Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                      Comment

                      • -Lost

                        #12
                        Re: Checking for daylight saving

                        Response to Randy Webb <HikksNotAtHome @aol.com>:
                        >Why in the world did Congress or whoever change that at the last
                        >moment anyway?
                        >
                        <shrugWhy do politicians do any of the things they do?
                        Heh, fair point.
                        One reason I read (and don't know if it is true or not) is:
                        >
                        A new law to extend DST to the first Sunday in November will take
                        effect in 2007, with the purpose of providing trick-or-treaters
                        more light and therefore more safety from traffic accidents.
                        >
                        Go figure.
                        Well, anything to make us think they care I suppose.

                        --
                        -Lost
                        Remove the extra words to reply by e-mail. Don't e-mail me. I am
                        kidding. No I am not.

                        Comment

                        • Evertjan.

                          #13
                          Re: Checking for daylight saving

                          Randy Webb wrote on 21 dec 2007 in comp.lang.javas cript:
                          -Lost said the following on 12/20/2007 9:24 PM:
                          >Why in the world did Congress or whoever change that at the last moment
                          >anyway?
                          What congress?

                          Do you mean 'congress' in the sense of the Kamasutra
                          and changing a stand of such congress at the last moment?

                          Wouldn't that put you off, Lost?
                          <shrugWhy do politicians do any of the things they do?
                          >
                          One reason I read (and don't know if it is true or not) is:
                          >
                          A new law to extend DST to the first Sunday in November will take effect
                          in 2007, with the purpose of providing trick-or-treaters more light and
                          therefore more safety from traffic accidents.
                          What law?

                          Is that what politicians do, Randy, changing a stand at the last moment?

                          --
                          Evertjan.
                          The Netherlands.
                          (Please change the x'es to dots in my emailaddress)

                          Comment

                          • Randy Webb

                            #14
                            Re: Checking for daylight saving

                            Evertjan. said the following on 12/21/2007 3:43 AM:
                            Randy Webb wrote on 21 dec 2007 in comp.lang.javas cript:
                            >-Lost said the following on 12/20/2007 9:24 PM:
                            >>Why in the world did Congress or whoever change that at the last moment
                            >>anyway?
                            >
                            What congress?
                            You have been reading too much JRS.
                            Do you mean 'congress' in the sense of the Kamasutra
                            and changing a stand of such congress at the last moment?
                            >
                            Wouldn't that put you off, Lost?
                            No, not Kamasutra, it was the World Congress of Politicians.
                            ><shrugWhy do politicians do any of the things they do?
                            >>
                            >One reason I read (and don't know if it is true or not) is:
                            >>
                            >A new law to extend DST to the first Sunday in November will take effect
                            >in 2007, with the purpose of providing trick-or-treaters more light and
                            >therefore more safety from traffic accidents.
                            >
                            What law?
                            The one that extended DST to the first Sunday in November in the USA.
                            Is that what politicians do, Randy, changing a stand at the last moment?
                            I am not even sure that politicians know what politicians do. I do know
                            they can screw up something that should be trivially simple.

                            --
                            Randy
                            Chance Favors The Prepared Mind
                            comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
                            Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

                            Comment

                            • Evertjan.

                              #15
                              Re: Checking for daylight saving

                              Randy Webb wrote on 21 dec 2007 in comp.lang.javas cript:
                              It should. It has a setting to let Windows change the time for you. By
                              law, in the USA, it always starts and ends on the same days every
                              year, no matter what zone you are in in the USA, if you use it.
                              That gives two nights that this offset is rippling though your continent,
                              and must be terrible for large organisations like radio/tv.

                              The European law switching at the same time across all timezones is
                              slightly better.
                              >Does the London local setting know about the "double summertime" of
                              >WWII?
                              >
                              No idea.
                              In general, MS-windows zones surely cannot fill in the historical patchwork
                              of summertime regions.

                              <http://en.wikipedia.or g/wiki/Daylight_saving _time>
                              points to an European viewpoint:

                              Guess whose!

                              --
                              Evertjan.
                              The Netherlands.
                              (Please change the x'es to dots in my emailaddress)

                              Comment

                              Working...