libs/useful/datetime.c
void ut2dt (uint32_t unixtime, datetime_t *dt)
{
    /* First take out the hour/minutes/seconds - this part is easy. */

    dt->sec = unixtime % 60;
    unixtime /= 60;

    dt->min = unixtime % 60;
    unixtime /= 60;

    dt->hour = unixtime % 24;
    unixtime /= 24;

    /* day 0 was a thursday */
    dt->wday = (unixtime + 4) % 7;

    /* unixtime is now days since 01/01/1970 UTC
     * Rebaseline to the Common Era */

    unixtime += 719499;

    /* Roll forward looking for the year.  This could be done more efficiently
     * but this will do.  We have to start at 1969 because the year we calculate here
     * runs from March - so January and February 1970 will come out as 1969 here.
     */
    unsigned year;
    for (year = 1969; unixtime > YEAR_TO_DAYS (year + 1) + 30; year++)
        ;

    /* OK we have our "year", so subtract off the days accounted for by full years. */
    unixtime -= YEAR_TO_DAYS (year);

    /* unixtime is now number of days we are into the year (remembering that March 1
     * is the first day of the "year" still). */

    /* Roll forward looking for the month.  1 = March through to 12 = February. */
    unsigned mon;
    for (mon = 1; mon < 12 && unixtime > 367 * (mon + 1) / 12; mon++)
        ;

    /* Subtract off the days accounted for by full months */
    unixtime -= 367 * mon / 12;

    /* unixtime is now number of days we are into the month */

    /* Adjust the month/year so that 0 = January, and years start where we
     * usually expect them to. */
    mon++;
    if (mon > 11)
    {
        mon -= 12;
        year++;
    }

    dt->year = year;
    dt->mon = mon;
    dt->day = unixtime;
}
/*
 * My apologies for all the magic numbers. 367*month/12 is a neat trick to generate
 * the 30/31 day sequence of the calendar. The calculation works with years that
 * start in March until the fixup at the end, which makes things easy because
 * then the leap day falls at the end of a "year".
 */
My apologies for all the magic numbers. 367*month/12 is a neat trick to generate
the 30/31 day sequence of the calendar. The calculation works with years that
start in March until the fixup at the end, which makes things easy because
then the leap day falls at the end of a "year".
My apologies for all the magic numbers . 367 * month / 12 is a neat trick to generate the 30 / 31 day sequence of the calendar . The calculation works with years that start in March until the fixup at the end which makes things easy because then the leap day falls at the end of a " year " .
void ut2dt (uint32_t unixtime, datetime_t *dt)
{
    dt->sec = unixtime % 60;
    unixtime /= 60;
    dt->min = unixtime % 60;
    unixtime /= 60;
    dt->hour = unixtime % 24;
    unixtime /= 24;
    dt->wday = (unixtime + 4) % 7;
    unixtime += 719499;
    unsigned year;
    for (year = 1969; unixtime > YEAR_TO_DAYS (year + 1) + 30; year++)
        ;
    unixtime -= YEAR_TO_DAYS (year);
    unsigned mon;
    for (mon = 1; mon < 12 && unixtime > 367 * (mon + 1) / 12; mon++)
        ;
    unixtime -= 367 * mon / 12;
    mon++;
    if (mon > 11)
    {
        mon -= 12;
        year++;
    }
    dt->year = year;
    dt->mon = mon;
    dt->day = unixtime;
}
void ut2dt ( uint32_t unixtime , datetime_t * dt ) { dt -> sec = unixtime % 60 ; unixtime /= 60 ; dt -> min = unixtime % 60 ; unixtime /= 60 ; dt -> hour = unixtime % 24 ; unixtime /= 24 ; dt -> wday = ( unixtime + 4 ) % 7 ; unixtime += 719499 ; unsigned year ; for ( year = 1969 ; unixtime > YEAR_TO_DAYS ( year + 1 ) + 30 ; year ++ ) ; unixtime -= YEAR_TO_DAYS ( year ) ; unsigned mon ; for ( mon = 1 ; mon < 12 && unixtime > 367 * ( mon + 1 ) / 12 ; mon ++ ) ; unixtime -= 367 * mon / 12 ; mon ++ ; if ( mon > 11 ) { mon -= 12 ; year ++ ; } dt -> year = year ; dt -> mon = mon ; dt -> day = unixtime ; }
My apologies for all the magic numbers.
My apologies for all the magic numbers .
/* First take out the hour/minutes/seconds - this part is easy. */ /* day 0 was a thursday */ /* unixtime is now days since 01/01/1970 UTC
     * Rebaseline to the Common Era */ /* Roll forward looking for the year.  This could be done more efficiently
     * but this will do.  We have to start at 1969 because the year we calculate here
     * runs from March - so January and February 1970 will come out as 1969 here.
     */ /* OK we have our "year", so subtract off the days accounted for by full years. */ /* unixtime is now number of days we are into the year (remembering that March 1
     * is the first day of the "year" still). */ /* Roll forward looking for the month.  1 = March through to 12 = February. */ /* Subtract off the days accounted for by full months */ /* unixtime is now number of days we are into the month */ /* Adjust the month/year so that 0 = January, and years start where we
     * usually expect them to. */