/* mktime — struct tm → секунды Unix-эпохи (без таймзон/DST).
 * Попутно заполняет tm_wday/tm_yday, чтобы клиент мог их читать. */

#include <time.h>
#include "_time.h"

time_t mktime(struct tm *tm)
{
    unsigned int y = (unsigned int)(tm->tm_year + 1900);
    unsigned long days = _tm_year_days(y)
                       + _tm_month_days(y, (unsigned int)tm->tm_mon + 1)
                       + (unsigned int)(tm->tm_mday - 1);
    time_t epoch = days * 86400UL
                 + (unsigned long)tm->tm_hour * 3600UL
                 + (unsigned long)tm->tm_min  * 60UL
                 + tm->tm_sec;
    /* Дозаполнить wday/yday для клиента. */
    tm->tm_wday = (unsigned char)((4 + days) % 7);
    tm->tm_yday = (int)_tm_month_days(y, (unsigned int)tm->tm_mon + 1)
                + (tm->tm_mday - 1);
    return epoch;
}
