📄 tm2time.c
字号:
#include <stdlib.h>#include <sys/time.h>static voidvalidate_structure(struct tm *tm){ div_t res; /* Calculate time & date to account for out of range values */ if (tm->tm_sec < 0 || tm->tm_sec > 59) { res = div(tm->tm_sec, 60); tm->tm_min += res.quot; if ((tm->tm_sec = res.rem) < 0) tm->tm_sec += 60; } if (tm->tm_min < 0 || tm->tm_min > 59) { res = div(tm->tm_min, 60); tm->tm_hour += res.quot; if ((tm->tm_min = res.rem) < 0) tm->tm_min += 60; } if (tm->tm_hour < 0 || tm->tm_hour > 23) { res = div(tm->tm_hour, 24); tm->tm_mday += res.quot; if ((tm->tm_hour = res.rem) < 0) tm->tm_hour += 24; } if (tm->tm_mon > 11) { res = div(tm->tm_mon, 12); tm->tm_year += res.quot; if ((tm->tm_mon = res.rem) < 0) tm->tm_mon += 12; } if (_DAYS_IN_YEAR(tm->tm_year) == 366) days_in_feb = 29; if (tm->tm_mday < 0) { while (tm->tm_mday < 0) { tm->tm_mday += _DAYS_IN_MONTH(tm->tm_mon); if (--tm->tm_mon == -1) { tm->tm_year--; tm->tm_mon = 12; days_in_feb = ((_DAYS_IN_YEAR(tm->tm_year) == 366) ? 29 : 28); } } } else { while (tm->tm_mday > _DAYS_IN_MONTH(tm->tm_mon)) { tm->tm_mday -= _DAYS_IN_MONTH(tm->tm_mon); if (++tm->tm_mon == 12) { tm->tm_year++; tm->tm_mon = 0; days_in_feb = ((_DAYS_IN_YEAR(tm->tm_year) == 366) ? 29 : 28); } } }}time_ttm2time(struct tm *tm){ time_t tim = 0; long days = 0; int year; /* Validate structure */ validate_structure(tm); /* Compute hours, minutes, seconds */ tim += tm->tm_sec + (tm->tm_min * _SEC_IN_MINUTE) + (tm->tm_hour * _SEC_IN_HOUR); /* Compute days in year */ days += tm->tm_mday - 1; days += _DAYS_BEFORE_MONTH[tm->tm_mon]; if (tm->tm_mon > 1 && _DAYS_IN_YEAR(tm->tm_year) == 366) days++; /* Compute day of the year */ tm->tm_yday = days; if (tm->tm_year > 10000 || tm->tm_year < -10000) { return (time_t) - 1; } /* Compute days in other years */ if (tm->tm_year > 1970) { for (year = 1970; year < tm->tm_year; year++) days += _DAYS_IN_YEAR(year); } else if (tm->tm_year < 1970) { for (year = 1969; year > tm->tm_year; year--) days -= _DAYS_IN_YEAR(year); days -= _DAYS_IN_YEAR(year); } /* Compute day of the week */ if ((tm->tm_wday = (days + 4) % 7) < 0) tm->tm_wday += 7; /* Compute total seconds */ tim += (days * _SEC_IN_DAY); return tim;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -