Permalink
  • vote
    1
    0 starsmike | Shared With: Everyone - Jan 03 2009 | date, programming, calculations
    Cause of Zune 30 leapyear problem ISOLATED! - Zune Boards

    Here's a pretty crappy piece of code in the Zune clock driver. To convert days since 1/1/1980, it loops repeatedly subtracting the number of days in the year, and incrementing the year value.

    a) It's slow
    b) It's error prone (as we see with the Zune failure) to put potential infinite loops in your code (static analysis could have caught this, BTW)

    Proper year calculation can be got by dividing days by 365.25 (and correcting for century and 400-year exceptions). It's a bitch to get all the calendar calculations right, but can be done much more simply than this.

    Here's how I did this w/o looping for SharePoint (before all browser had the needed decoding functions!):

    // For Nav 4.0 - no UTC based decoding
    // We want to support valid dates from 1754 to 9999
    function DateDecode(date)
    {
    this.ms = date.getTime();
    this.msDay = this.ms % DateOptions.msDay;
    if (this.sec < 0)
    this.sec += DateOptions.msDay;

    this.idy = Math.floor(this.ms / DateOptions.msDay) + DateOptions.ddayOrigin; // Origin date to 3/1/1600

    var idyQC = this.idy % DateOptions.cdyQC; // Relative to 400 year block beginning 3/1/1600
    var iQC = Math.floor(this.idy / DateOptions.cdyQC);

    var idyC = idyQC % DateOptions.cdyC; // Relative to 100 year block beginning 3/1/xx00
    var iC = Math.floor(idyQC / DateOptions.cdyC);
    // End of Quad-Century leap year
    if (iC == 4)
    {
    iC = 3;
    idyC = DateOptions.cdyC;
    }

    var idyQYr = idyC % DateOptions.cdyQYr; // Relative to 4 year block beginning 3/1/xxxx - ending on leap year
    var iQYr = Math.floor(idyC / DateOptions.cdyQYr);

    var idyYr = idyQYr % DateOptions.cdyYr; // Relative to year begining 3/1/xxxx
    var iYr = Math.floor(idyQYr / DateOptions.cdyYr);
    // Feb 29
    if (iYr == 4)
    {
    iYr = 3;
    idyYr = DateOptions.cdyYr;
    }

    this.yr = 1600 + iQC * 400 + iC * 100 + iQYr * 4 + iYr;
    if (idyYr >= DateOptions.idyJan1)
    this.yr++;

    var imon;
    for (imon = 0; imon < 11; imon++)
    {
    if (idyYr < DateOptions.mpMonIdy[imon])
    break;
    }

    this.mon = (imon + 2) % 12;
    this.day = idyYr - ((imon > 0) ? DateOptions.mpMonIdy[imon-1] : 0) + 1;
    this.hr = Math.floor(this.msDay/DateOptions.msHour);
    this.min = Math.floor((this.msDay%DateOptions.msHour)/DateOptions.msMinute);
    this.sec = Math.floor((this.msDay%DateOptions.msMinute)/DateOptions.msSecond);
    this.dow = (this.idy+3) % 7;
    }

  • vote
    1
    0 starsmike | Shared With: Everyone - Jul 19 2008 | date, formats, iso
    ISO 8601 - Wikipedia, the free encyclopedia

    ISO 8601 Date format spec described here.

1 - 2 of 2 Faves

Related Content from Around Faves

date

VIEW ALL