Date computations

/* Returns the day number of a date */
int dayNumber(Date d) {
    d.month = (d.month + 9) % 12;
    d.year = d.year - d.month / 10;
    return 365 * d.year + d.year / 4 - d.year / 100 + d.year / 400 + (d.month * 306 + 5) / 10 + (d.day - 1);
}

/* Returns the date of a day number (g) */
Date dayFromNumber(int g) {
    Date d;
    d.year = (10000 * g + 14780) / 3652425;
    int ddd = g - (365 * d.year + d.year / 4 - d.year / 100 + d.year / 400);
    if (ddd < 0) {
        d.year = d.year - 1;
        ddd = g - (365 * d.year + d.year / 4 - d.year / 100 + d.year / 400);
    }
    int mi = (100 * ddd + 52) / 3060;
    d.month = (mi + 2) % 12 + 1;
    d.year = d.year + (mi + 2) / 12;
    d.day = ddd - (mi * 306 + 5) / 10 + 1;
    return d;
}

Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2018-03-29 - JimSkon
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback