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); }
Tm_t* tmxtm(register Tm_t* tm, Time_t t, Tm_zone_t* zone) { register struct tm* tp; register Tm_leap_t* lp; Time_t x; time_t now; int leapsec; int y; uint32_t n; int32_t o; #if TMX_FLOAT Time_t z; uint32_t i; #endif tmset(tm_info.zone); leapsec = 0; if ((tm_info.flags & (TM_ADJUST|TM_LEAP)) == (TM_ADJUST|TM_LEAP) && (n = tmxsec(t))) { for (lp = &tm_data.leap[0]; n < lp->time; lp++); if (lp->total) { if (n == lp->time && (leapsec = (lp->total - (lp+1)->total)) < 0) leapsec = 0; t = tmxsns(n - lp->total, tmxnsec(t)); } } x = tmxsec(t); if (!(tm->tm_zone = zone)) { if (tm_info.flags & TM_UTC) tm->tm_zone = &tm_data.zone[2]; else tm->tm_zone = tm_info.zone; } if ((o = 60 * tm->tm_zone->west) && x > o) { x -= o; o = 0; } #if TMX_FLOAT i = x / (24 * 60 * 60); z = i; n = x - z * (24 * 60 * 60); tm->tm_sec = n % 60 + leapsec; n /= 60; tm->tm_min = n % 60; n /= 60; tm->tm_hour = n % 24; #define x i #else tm->tm_sec = x % 60 + leapsec; x /= 60; tm->tm_min = x % 60; x /= 60; tm->tm_hour = x % 24; x /= 24; #endif tm->tm_wday = (x + 4) % 7; tm->tm_year = (400 * (x + 25202)) / 146097 + 1; n = tm->tm_year - 1; x -= n * 365 + n / 4 - n / 100 + (n + (1900 - 1600)) / 400 - (1970 - 1901) * 365 - (1970 - 1901) / 4; tm->tm_mon = 0; tm->tm_mday = x + 1; tm->tm_nsec = tmxnsec(t); tmfix(tm); n += 1900; tm->tm_isdst = 0; if (tm->tm_zone->daylight) { if ((y = tmequiv(tm) - 1900) == tm->tm_year) now = tmxsec(t); else { Tm_t te; te = *tm; te.tm_year = y; now = tmxsec(tmxtime(&te, tm->tm_zone->west)); } if ((tp = tmlocaltime(&now)) && ((tm->tm_isdst = tp->tm_isdst) || o)) { tm->tm_min -= o / 60 + (tm->tm_isdst ? tm->tm_zone->dst : 0); tmfix(tm); } } return tm; }