📄 timedate.c
字号:
//=============================================================================
// File: TIMEDATE.C - V1.00
// Rem.: The ACPRD Project Page on the Web -> http://hc12web.de/acprd
//=============================================================================
//-- Includes -----------------------------------------------------------------
#include <stdio.h>
#include "datatypes.h"
#include "timedate.h"
//-- Static Vars --------------------------------------------------------------
const UINT16 days_until_month[24] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, // normal year
0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 // leap year
};
const UINT8 * const wday_name[7] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
const UINT8 * const month_name[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
volatile time_t systime; // system timer, incremented every second
struct tm systime_tm; // static buffer used by gmtime()
#ifdef __USE_ASCTIME
UINT8 systime_str[32]; // static buffer used by asctime() and ctime()
#endif //__USE_ASCTIME
//-- Code ---------------------------------------------------------------------
time_t time(time_t *tptr) {
time_t t;
DISABLE_INTERRUPTS();
t = systime;
ENABLE_INTERRUPTS();
if(tptr != NULL) *tptr = t;
return t;
}
//-----------------------------------------------------------------------------
UINT16 stime(time_t *tptr) {
if(tptr != NULL) {
DISABLE_INTERRUPTS();
systime = *tptr;
ENABLE_INTERRUPTS();
}
return 0;
}
//-----------------------------------------------------------------------------
// convert timestamp (seconds since 1970)
// to structured time/date info
//
struct tm *gmtime(const time_t *tptr) {
UINT32 time;
UINT16 days, days_p_y, n;
const UINT16 *days_u_m;
time = *tptr; // time in seconds
systime_tm.tm_sec = (UINT8)(time % 60);
time /= 60; // time in minutes
systime_tm.tm_min = (UINT8)(time % 60);
time /= 60; // time in hours
systime_tm.tm_hour = (UINT8)(time % 24);
time /= 24; // time in days since 1970
systime_tm.tm_wday = (time+4) % 7; // 01/01/1970 was a Thursday
//---- start year calculation ----
systime_tm.tm_year = 70; // tm_year is "1900-based"
n = 0x02; // 1970 is 2 years after a leap year
days = time;
days_p_y = 365;
while(days >= days_p_y) {
days -= days_p_y;
systime_tm.tm_year++;
n++; // if (n%4) == 0 then leap year
days_p_y = (n & 0x03) ? 365 : 366;
}
systime_tm.tm_yday = days; // day of (this) year
//---- start month/mday calculation ----
days_u_m = &days_until_month[12];
// if leap year then use 2nd half of table
if((systime_tm.tm_year & 3) == 0) days_u_m += 12;
n = 12;
do {
days_u_m--;
n--;
if(days >= *days_u_m) {
systime_tm.tm_mday = days - *days_u_m + 1;
systime_tm.tm_mon = n;
n = 0; // signal end of loop
}
} while(n);
systime_tm.tm_isdst = 0; // not used
return(&systime_tm);
}
//-----------------------------------------------------------------------------
// calculate timestamp from tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec
// caution: no input data check will be performed!
//
time_t mktime(struct tm *tmptr) {
UINT16 year, leaps;
time_t days, seconds;
struct tm tmx;
tmx = *tmptr;
year = tmx.tm_year - 70; // + 1900 - 1970
leaps = (year + 2) / 4;
if(((tmx.tm_year & 3) == 0) && (tmx.tm_mon < 2)) {
leaps--;
}
days = days_until_month[tmx.tm_mon] + tmx.tm_mday - 1;
tmx.tm_yday = days;
days += year * (time_t)365L + leaps;
tmx.tm_wday = days % 7 + 4; // 01/01/1970 was a Thursday
seconds = days * (time_t)86400L
+ tmx.tm_hour * (time_t)3600L
+ tmx.tm_min * (time_t)60L
+ tmx.tm_sec;
return seconds;
}
//-----------------------------------------------------------------------------
#ifdef __USE_ASCTIME
// output format:
// Fri Nov 01 12:34:56 2002\n\0
// (all fields have fixed length)
//
UINT8 *asctime(struct tm *tmptr) {
sprintf(systime_str, "%s %s %02d %02d:%02d:%02d %4d\n",
wday_name[tmptr->tm_wday], month_name[tmptr->tm_mon],
(UINT16)(tmptr->tm_mday), (UINT16)(tmptr->tm_hour),
(UINT16)(tmptr->tm_min), (UINT16)(tmptr->tm_sec),
(UINT16)(tmptr->tm_year + 1900));
return systime_str;
}
UINT8 *ctime(time_t *tptr) {
return asctime(gmtime(tptr));
}
#endif //__USE_ASCTIME
//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -