📄 time.inl
字号:
if (cyg_libc_time_year_is_leap(1900 + __result->tm_year)) {
// But if this is a leap year, its possible that we are in the
// middle of the last "extra" day
if (_tim < CYGNUM_LIBC_TIME_SECSPERLEAPYEAR)
break;
_tim -= CYGNUM_LIBC_TIME_SECSPERLEAPYEAR;
__result->tm_wday += 366;
} // if
else {
_tim -= CYGNUM_LIBC_TIME_SECSPERYEAR;
__result->tm_wday += 365;
}
++__result->tm_year;
} // while
// Day of the year. We know _tim is +ve now
CYG_ASSERT(_tim >= 0,
"Number of seconds since start of year is negative!");
__result->tm_yday = _tim / CYGNUM_LIBC_TIME_SECSPERDAY;
// Day of the week. Normalize to be 0..6, and note that it might
// be negative, so we have to deal with the modulus being
// implementation-defined for -ve numbers (ISO C 6.3.5)
__result->tm_wday = (((__result->tm_wday + __result->tm_yday)%7)+7)%7;
// Month and Day of the month
_months_p = cyg_libc_time_month_lengths[
cyg_libc_time_year_is_leap(1900 + __result->tm_year) ? 1 : 0 ];
__result->tm_mday = __result->tm_yday+1;
for (__result->tm_mon = 0;
__result->tm_mday > _months_p[__result->tm_mon];
++__result->tm_mon) {
__result->tm_mday -= _months_p[__result->tm_mon];
} // for
_tim -= __result->tm_yday*CYGNUM_LIBC_TIME_SECSPERDAY;
// hours, mins secs
__result->tm_hour = (int) (_tim / 3600);
_tim %= 3600;
__result->tm_min = (int) (_tim / 60);
__result->tm_sec = (int) (_tim % 60);
__result->tm_isdst = 0; // gmtime always returns non-DST
CYG_REPORT_RETVAL(__result);
return __result;
} // gmtime_r()
#endif // ifdef CYGIMP_LIBC_TIME_GMTIME_R_INLINE
///////////////////////////////////
// localtime_r() - POSIX.1 8.3.7 //
///////////////////////////////////
//
// This converts a time_t into a struct tm expressed in local time, and
// stores the result in the space occupied by __result
//
#ifdef CYGFUN_LIBC_TIME_POSIX
# define __localtime_r localtime_r
#else
// prototype internal function
__externC struct tm *
__localtime_r( const time_t *__timer, struct tm *__result );
#endif
#ifdef CYGIMP_LIBC_TIME_LOCALTIME_R_INLINE
#include <cyg/libc/time/timeutil.h> // for cyg_libc_time_normalize_structtm()
CYGPRI_LIBC_TIME_LOCALTIME_R_INLINE struct tm *
__localtime_r( const time_t *__timer, struct tm *__result )
{
time_t __stdoffset, __dstoffset;
CYG_REPORT_FUNCNAMETYPE("localtime_r", "returning %08x");
CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
CYG_CHECK_DATA_PTR(__result, "__result is not a valid pointer!");
CYG_REPORT_FUNCARG2("*__timer=%d, __result is at %08x",
*__timer, __result);
__gmtime_r(__timer, __result);
// Adjust for STD/DST
__result->tm_isdst = cyg_libc_time_getzoneoffsets(&__stdoffset,
&__dstoffset);
if (__result->tm_isdst == 0) { // STD
__result->tm_sec += __stdoffset;
cyg_libc_time_normalize_structtm( __result );
} // if
else if (__result->tm_isdst > 0) { // DST
__result->tm_sec += __dstoffset;
cyg_libc_time_normalize_structtm( __result );
} // if
// Don't do anything for tm_isdst == -1
CYG_REPORT_RETVAL(__result);
return __result;
} // localtime_r()
#endif // ifdef CYGIMP_LIBC_TIME_LOCALTIME_R_INLINE
///////////////////////////////
// ctime_r() - POSIX.1 8.3.5 //
///////////////////////////////
//
// This returns the equivalent of ctime() but writes to __buf
// to store the returned string
//
#ifdef CYGFUN_LIBC_TIME_POSIX
# define __ctime_r ctime_r
#else
// prototype internal function
__externC char *
__ctime_r( const time_t *__timer, char *__buf );
#endif
#ifdef CYGIMP_LIBC_TIME_CTIME_R_INLINE
CYGPRI_LIBC_TIME_CTIME_R_INLINE char *
__ctime_r( const time_t *__timer, char *__buf )
{
struct tm _mytm;
CYG_REPORT_FUNCNAMETYPE("ctime_r", "returning \"%s\"");
CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
CYG_CHECK_DATA_PTR(__buf, "__buf is not a valid pointer!");
CYG_REPORT_FUNCARG2("*__timer = %d, __buf=%08x", *__timer, __buf);
__localtime_r( __timer, &_mytm );
__asctime_r(&_mytm, __buf);
CYG_REPORT_RETVAL(__buf);
return __buf;
} // ctime_r()
#endif // ifdef CYGIMP_LIBC_TIME_CTIME_R_INLINE
//===========================================================================
//
// ISO C functions
// Time manipulation functions - ISO C 7.12.2
/////////////////////////////////
// difftime() - ISO C 7.12.2.2 //
/////////////////////////////////
//
// This returns (__time1 - __time0) in seconds
//
#ifdef CYGIMP_LIBC_TIME_DIFFTIME_INLINE
CYGPRI_LIBC_TIME_DIFFTIME_INLINE double
difftime( time_t __time1, time_t __time0 )
{
double _ret;
CYG_REPORT_FUNCNAMETYPE("difftime", "returning %f");
CYG_REPORT_FUNCARG2("__time1=%d, __time0=%d", __time1, __time0);
_ret = (double)(__time1 - __time0);
CYG_REPORT_RETVAL(_ret);
return _ret;
} // difftime()
#endif // ifdef CYGIMP_LIBC_TIME_DIFFTIME_INLINE
///////////////////////////////
// mktime() - ISO C 7.12.2.3 //
///////////////////////////////
//
// This converts a "struct tm" to a "time_t"
//
#ifdef CYGIMP_LIBC_TIME_MKTIME_INLINE
#include <cyg/libc/time/timeutil.h> // for cyg_libc_time_normalize_structtm()
// and cyg_libc_time_month_lengths
CYGPRI_LIBC_TIME_MKTIME_INLINE time_t
mktime( struct tm *__timeptr )
{
time_t _ret;
cyg_count16 _i;
cyg_count32 _daycount;
cyg_bool _leap;
CYG_REPORT_FUNCNAMETYPE("mktime", "returning %d");
CYG_REPORT_FUNCARG1( "__timeptr is at address %08x", __timeptr);
CYG_CHECK_DATA_PTR(__timeptr, "__timeptr is not a valid pointer!");
// First deal with STD/DST. If tm_isdst==-1 (the "autodetect" value)
// we assume its already in UTC. FIXME: is this correct behaviour? Hmm....
#if 0
// FIXME: This doesn't seem to be the way to go
if (__timeptr->tm_isdst == 0) { // STD
// take _off_ the std offset to get us back to UTC from localtime
__timeptr->tm_sec -= (int)cyg_libc_time_current_std_offset;
} // if
else if (__timeptr->tm_isdst > 0) { // DST
// take _off_ the dst offset to get us back to UTC from localtime
__timeptr->tm_sec -= (int)cyg_libc_time_current_dst_offset;
} // if
#endif
cyg_libc_time_normalize_structtm(__timeptr);
// check if a time_t can hold the year. FIXME: we assume it is
// 32 bits which gives the year range 1902 - 2038
if ( (__timeptr->tm_year <= 2) || (__timeptr->tm_year >= 138) ) {
CYG_REPORT_RETVAL(-1);
return (time_t)-1;
}
// fill in the rest of the struct tm i.e. tm_wday and tm_yday
_leap = cyg_libc_time_year_is_leap(1900 + __timeptr->tm_year);
for (_i=0, _daycount=0; _i<12; ++_i) {
if (_i == __timeptr->tm_mon) {
_daycount += __timeptr->tm_mday - 1;
break;
} // if
else {
_daycount += cyg_libc_time_month_lengths[_leap][_i];
} // else
} // for
CYG_ASSERT(_i<12, "Reached end of year. __timeptr->tm_mon must be bad");
__timeptr->tm_yday = _daycount;
// now tm_wday
if (__timeptr->tm_year > 70) {
for (_i=70; _i < __timeptr->tm_year; ++_i)
_daycount += (cyg_libc_time_year_is_leap(1900 + _i) ? 366 : 365);
} // if
else if (__timeptr->tm_year < 70) {
for (_i=70; _i > __timeptr->tm_year; --_i)
_daycount -= (cyg_libc_time_year_is_leap(1900 + _i-1) ? 366 : 365);
} // else if
__timeptr->tm_wday = (_daycount + CYGNUM_LIBC_TIME_EPOCH_WDAY) % 7;
// if _daycount was negative, on some targets the modulo operator will
// return negative, so we adjust for that
if (__timeptr->tm_wday < 0)
__timeptr->tm_wday += 7;
// now finally work out return value
_ret = __timeptr->tm_sec + 60*__timeptr->tm_min + 60*60*__timeptr->tm_hour;
_ret += _daycount*24*60*60;
CYG_REPORT_RETVAL(_ret);
return _ret;
} // mktime()
#endif // ifdef CYGIMP_LIBC_TIME_MKTIME_INLINE
// Time conversion functions - ISO C 7.12.3
////////////////////////////////
// asctime() - ISO C 7.12.3.1 //
////////////////////////////////
//
// This returns a textual representation of a struct tm
//
#ifdef CYGIMP_LIBC_TIME_ASCTIME_INLINE
extern char cyg_libc_time_asctime_buf[];
CYGPRI_LIBC_TIME_ASCTIME_INLINE char *
asctime( const struct tm *__timeptr )
{
CYG_REPORT_FUNCNAMETYPE("__asctime", "returning \"%s\"");
CYG_REPORT_FUNCARG1("__timeptr = %08x", __timeptr);
// paranoia
CYG_CHECK_DATA_PTR(__timeptr, "__timeptr is not a valid pointer!");
(void)__asctime_r( __timeptr, cyg_libc_time_asctime_buf );
CYG_REPORT_RETVAL(cyg_libc_time_asctime_buf);
return cyg_libc_time_asctime_buf;
} // asctime()
#endif // ifdef CYGIMP_LIBC_TIME_ASCTIME_INLINE
///////////////////////////////
// gmtime() - ISO C 7.12.3.3 //
///////////////////////////////
//
// This converts a time_t into a struct tm expressed in UTC
//
#ifdef CYGIMP_LIBC_TIME_GMTIME_INLINE
extern struct tm cyg_libc_time_gmtime_buf;
CYGPRI_LIBC_TIME_GMTIME_INLINE struct tm *
gmtime( const time_t *__timer )
{
CYG_REPORT_FUNCNAMETYPE("gmtime", "returning %08x");
CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
CYG_REPORT_FUNCARG1("*__timer=%d", *__timer);
__gmtime_r(__timer, &cyg_libc_time_gmtime_buf);
CYG_REPORT_RETVAL(&cyg_libc_time_gmtime_buf);
return &cyg_libc_time_gmtime_buf;
} // gmtime()
#endif // ifdef CYGIMP_LIBC_TIME_GMTIME_INLINE
//////////////////////////////////
// localtime() - ISO C 7.12.3.4 //
//////////////////////////////////
//
// This converts a time_t into a struct tm expressed in local time
//
#ifdef CYGIMP_LIBC_TIME_LOCALTIME_INLINE
extern struct tm cyg_libc_time_localtime_buf;
CYGPRI_LIBC_TIME_LOCALTIME_INLINE struct tm *
localtime( const time_t *__timer )
{
CYG_REPORT_FUNCNAMETYPE("localtime", "returning %08x");
CYG_CHECK_DATA_PTR(__timer, "__timer is not a valid pointer!");
CYG_REPORT_FUNCARG1("*__timer=%d", *__timer);
__localtime_r(__timer, &cyg_libc_time_localtime_buf);
CYG_REPORT_RETVAL(&cyg_libc_time_localtime_buf);
return &cyg_libc_time_localtime_buf;
} // localtime()
#endif // ifdef CYGIMP_LIBC_TIME_LOCALTIME_INLINE
//////////////////////////////
// ctime() - ISO C 7.12.3.2 //
//////////////////////////////
//
// This returns asctime(localtime(__timeptr))
//
#ifdef CYGIMP_LIBC_TIME_CTIME_INLINE
CYGPRI_LIBC_TIME_CTIME_INLINE char *
ctime( const time_t *__timer )
{
char *_str;
CYG_REPORT_FUNCNAMETYPE("ctime", "returning \"%s\"");
CYG_CHECK_DATA_PTR( __timer, "__timer is not a valid pointer!");
CYG_REPORT_FUNCARG1("*__timer = %d", *__timer);
_str = asctime(localtime(__timer));
CYG_REPORT_RETVAL(_str);
return _str;
} // ctime()
#endif // ifdef CYGIMP_LIBC_TIME_CTIME_INLINE
#ifdef __cplusplus
} // extern "C"
#endif
#endif // CYGONCE_LIBC_TIME_INL multiple inclusion protection
// EOF time.inl
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -