void __tmValidate( struct tm *pTime ) { struct tm t; int jday; int mon; /* Set local structure */ t = *pTime; /* Normalize */ __tmNormalize(&t.tm_min, &t.tm_sec, SECSPERMIN); __tmNormalize(&t.tm_hour, &t.tm_min, MINSPERHOUR); __tmNormalize(&t.tm_mday, &t.tm_hour, HOURSPERDAY); __tmNormalize(&t.tm_year, &t.tm_mon, MONSPERYEAR); /* Calulcate julian day */ jday = __julday(t.tm_year, t.tm_mon, t.tm_mday); /* Calulcate month and day */ for (mon = 0; (jday > __julday(t.tm_year, mon + 1, 0)) && (mon < 11); mon++); t.tm_mon = mon; t.tm_mday = jday - __julday(t.tm_year, mon, 0); t.tm_wday = 0; t.tm_yday = 0; *pTime = t; }
int __getTime( const time_t timer, struct tm *tmp ) { int days; int timeOfDay; int year; int mon; ldiv_t result; /* Calculate days since epoch */ days = timer / SECSPERDAY; timeOfDay = timer % SECSPERDAY; if (timeOfDay < 0) { timeOfDay += SECSPERDAY; days -= 1; } /* Calculate years since epoch */ year = days / DAYSPERYEAR; while (__daysSinceEpoch(year, 0) > days) { year--; } /* Calculcate weekday */ tmp->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; if (tmp->tm_wday < 0) { tmp->tm_wday += DAYSPERWEEK; } /* Find year and days left */ days -= __daysSinceEpoch(year, 0); year += EPOCH_YEAR; /* Calculate month */ for (mon = 0; (days >= __julday(year - TM_YEAR_BASE, mon + 1, 0)) && (mon < 11); mon++); /* Setup time structure */ tmp->tm_year = year - TM_YEAR_BASE; tmp->tm_mon = mon; tmp->tm_mday = (days - __julday(tmp->tm_year, mon, 0)) + 1; tmp->tm_yday = __julday(tmp->tm_year, mon, tmp->tm_mday) - 1; tmp->tm_hour = timeOfDay / SECSPERHOUR; timeOfDay %= SECSPERHOUR; ldiv_r((long) timeOfDay, (long) SECSPERMIN, &result); tmp->tm_min = result.quot; tmp->tm_sec = result.rem; return OK; }
/******************************************************************************* * * __tmValidate - validate the broken-down structure, tmptr. * * RETURNS: the validated structure. * NOMANUAL */ LOCAL void __tmValidate ( struct tm * tmptr /* pointer to broken-down structure */ ) { struct tm tmStruct; int jday; int mon; /* Adjust timeptr to reflect a legal time * Is it within range 1970-2038? */ tmStruct = *tmptr; __tmNormalize (&tmStruct.tm_min, &tmStruct.tm_sec, SECSPERMIN); __tmNormalize (&tmStruct.tm_hour, &tmStruct.tm_min, MINSPERHOUR); __tmNormalize (&tmStruct.tm_mday, &tmStruct.tm_hour, HOURSPERDAY); __tmNormalize (&tmStruct.tm_year, &tmStruct.tm_mon, MONSPERYEAR); /* tm_mday may not be in the correct range - check */ jday = __julday (tmStruct.tm_year, tmStruct.tm_mon , tmStruct.tm_mday); if (jday < 0) { tmStruct.tm_year--; jday += DAYSPERYEAR; } /* Calulate month and day */ for (mon = 0; (jday > __julday (tmStruct.tm_year, mon+1, 0)) && (mon < 11); mon++ ) ; tmStruct.tm_mon = mon; tmStruct.tm_mday = jday - __julday (tmStruct.tm_year, mon, 0); tmStruct.tm_wday = 0; tmStruct.tm_yday = 0; *tmptr = tmStruct; }
time_t mktime ( struct tm * timeptr /* pointer to broken-down structure */ ) { time_t timeIs = 0; int days = 0; char zoneBuf [sizeof (ZONEBUFFER)]; /* Validate tm structure */ __tmValidate (timeptr); /* Calulate time_t value */ /* time */ timeIs += (timeptr->tm_sec + (timeptr->tm_min * SECSPERMIN) + (timeptr->tm_hour * SECSPERHOUR)); /* date */ days += __julday (timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday); timeptr->tm_yday = (days - 1); if ((timeptr->tm_year + TM_YEAR_BASE) < EPOCH_YEAR ) return ((time_t) ERROR); /* days in previous years */ days = __daysSinceEpoch (timeptr->tm_year - (EPOCH_YEAR - TM_YEAR_BASE), timeptr->tm_yday ); timeptr->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; timeIs += (days * SECSPERDAY); /* correct for day light saving */ /* validate again for the extra DST hour */ if ((timeptr->tm_isdst = __getDstInfo (timeptr, __loctime))) { timeIs -= SECSPERHOUR; __tmValidate (timeptr); } /* correct for zone offset from UTC */ __getZoneInfo (zoneBuf, TIMEOFFSET, __loctime); timeIs += (atoi (zoneBuf) * SECSPERMIN); return(timeIs); }
int __getTime ( const time_t timer, /* time represented as seconds from epoch */ struct tm * tmp /* pointer to broken-down time structure */ ) { long days; long timeOfDay; long year; long mon; ldiv_t result; /* Calulate number of days since epoch */ days = timer / SECSPERDAY; timeOfDay = timer % SECSPERDAY; /* If time of day is negative, subtract one day, and add SECSPERDAY * to make it positive. */ if(timeOfDay<0) { timeOfDay+=SECSPERDAY; days-=1; } /* Calulate number of years since epoch */ year = days / DAYSPERYEAR; while ( __daysSinceEpoch (year, 0) > days ) year--; /* Calulate the day of the week */ tmp->tm_wday = (days + EPOCH_WDAY) % DAYSPERWEEK; /* * If there is a negative weekday, add DAYSPERWEEK to make it positive */ if(tmp->tm_wday<0) tmp->tm_wday+=DAYSPERWEEK; /* Find year and remaining days */ days -= __daysSinceEpoch (year, 0); year += EPOCH_YEAR; /* Find month */ /* __jullday needs years since TM_YEAR_BASE (SPR 4251) */ for ( mon = 0; (days >= __julday (year - TM_YEAR_BASE, mon + 1, 0)) && (mon < 11); mon++ ) ; /* Initialise tm structure */ tmp->tm_year = year - TM_YEAR_BASE; /* years since 1900 */ tmp->tm_mon = mon; tmp->tm_mday = (days - __julday (tmp->tm_year, mon, 0)) + 1; tmp->tm_yday = __julday (tmp->tm_year, mon, tmp->tm_mday) - 1; tmp->tm_hour = timeOfDay / SECSPERHOUR; timeOfDay %= SECSPERHOUR; ldiv_r (timeOfDay, SECSPERMIN, &result); tmp->tm_min = result.quot; tmp->tm_sec = result.rem; return(OK); }