int tmweek(Tm_t* tm, int type, int week, int day) { int d; if (week < 0) { if ((day = tm->tm_wday - tm->tm_yday % 7) < 0) day += 7; week = (tm->tm_yday + offset[day][type]) / 7; if (type == 2) { if (!week) week = (day > 0 && day < 6 || tmisleapyear(tm->tm_year - 1)) ? 53 : 52; else if (week == 53 && (tm->tm_wday + (31 - tm->tm_mday)) < 4) week = 1; } return week; } if (day < 0) day = type != 0; tm->tm_mon = 0; tm->tm_mday = 1; tmfix(tm); d = tm->tm_wday; tm->tm_mday = week * 7 - offset[d][type] + ((day || type != 2) ? day : 7); tmfix(tm); if (d = tm->tm_wday - day) { tm->tm_mday -= d; tmfix(tm); } return tm->tm_yday; }
int tmequiv(Tm_t* tm) { return tm->tm_year < (2038 - 1900) ? (tm->tm_year + 1900) : equiv[tm->tm_wday + tmisleapyear(tm->tm_year)]; }
Time_t tmxtime(register Tm_t* tm, int west) { register Time_t t; register Tm_leap_t* lp; register int32_t y; int n; int sec; time_t now; struct tm* tl; Tm_t* to; Tm_t ts; ts = *tm; to = tm; tm = &ts; tmset(tm_info.zone); tmfix(tm); y = tm->tm_year; if (y < 69 || y > (TMX_MAXYEAR - 1900)) return TMX_NOTIME; y--; t = y * 365 + y / 4 - y / 100 + (y + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4; if ((n = tm->tm_mon) > 11) n = 11; y += 1901; if (n > 1 && tmisleapyear(y)) t++; t += tm_data.sum[n] + tm->tm_mday - 1; t *= 24; t += tm->tm_hour; t *= 60; t += tm->tm_min; t *= 60; t += sec = tm->tm_sec; if (west != TM_UTCZONE && !(tm_info.flags & TM_UTC)) { /* * time zone adjustments */ if (west == TM_LOCALZONE) { t += tm_info.zone->west * 60; if (!tm_info.zone->daylight) tm->tm_isdst = 0; else { y = tm->tm_year; tm->tm_year = tmequiv(tm) - 1900; now = tmxsec(tmxtime(tm, tm_info.zone->west)); tm->tm_year = y; if (!(tl = tmlocaltime(&now))) return TMX_NOTIME; if (tm->tm_isdst = tl->tm_isdst) t += tm_info.zone->dst * 60; } } else { t += west * 60; if (!tm_info.zone->daylight) tm->tm_isdst = 0; else if (tm->tm_isdst < 0) { y = tm->tm_year; tm->tm_year = tmequiv(tm) - 1900; tm->tm_isdst = 0; now = tmxsec(tmxtime(tm, tm_info.zone->west)); tm->tm_year = y; if (!(tl = tmlocaltime(&now))) return TMX_NOTIME; tm->tm_isdst = tl->tm_isdst; } } } else if (tm->tm_isdst) tm->tm_isdst = 0; *to = *tm; if (tm_info.flags & TM_LEAP) { /* * leap second adjustments */ for (lp = &tm_data.leap[0]; t < lp->time - (lp+1)->total; lp++); t += lp->total; n = lp->total - (lp+1)->total; if (t <= (lp->time + n) && (n > 0 && sec > 59 || n < 0 && sec > (59 + n) && sec <= 59)) t -= n; } return tmxsns(t, tm->tm_nsec); }