做门禁的来看。
PICC提供的库文件TIME不能对闰年的2月29日作调整,造成闰年有2个28日。我做了修改,外加上了根据日期求从1970年开始的总秒数函数。代码如下,经过严格验证,没问题:
const unsigned char moninit[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static
put2d( char * cp, chari, charcl, charct)
{
*cp = ct;
*--cp = i%10 + '0';
if(i /= 10)
*--cp = i + '0';
else
*--cp = cl;
}
static
dylen(unsigned yr)
{
if(yr%4 || (yr % 100) == 0 && ((yr+300)%400) != 0)
return(365);
return(366);
}
/*
struct tm *
localtime(time_t * tp)
{
time_t t;
t = *tp - time_zone*60L;
return gmtime(&t);
}
*/
struct tm *
gmtime(time_t * tp)
{
time_t t;
staticstruct tm tim;
unsigned int tolday;
unsigned char vv;
t = *tp;
tim.tm_sec = t % 60L;
t /= 60L;
tim.tm_min = t % 60L;
t /= 60L;
tim.tm_hour = t % 24L;
tolday=t /24L;
tim.tm_wday = ((long)(tolday + 4)) % 7;
tim.tm_year = 70;
while(tolday >= dylen(tim.tm_year)) {
tolday -= dylen(tim.tm_year);
tim.tm_year++;
}
tim.tm_yday=tolday;
// if(tim.tm_mday >= 59 && dylen(tim.tm_year) == 366)
//tim.tm_mday--;
tim.tm_mon = 0;
vv=31;
while(tolday >= vv) {
tolday -= vv;
tim.tm_mon++;
vv=moninit;
if(tim.tm_mon==1 && dylen(tim.tm_year)==366)
vv++;
}
tolday++;
tim.tm_mday=tolday;
return &tim;
}
//从时间结构体中求的总秒数
time_t totalsec(struct tm * tim){
char i;
time_t j;
j=0;
for(i=70; i<(tim->tm_year); i++){
j+=dylen(i);
}
for(i=0; i<(tim->tm_mon); i++){
j+=moninit;
}
if(tim->tm_mon >= 2 && dylen(tim->tm_year) == 366)
j++;
j+=(tim->tm_mday-1);
j*=86400L;
j+=(tim->tm_hour*3600L);
j+=(tim->tm_min*60L);
j+=tim->tm_sec;
return j;
}
char *
asctime(struct tm * tim)
{
register const char * s;
char * cp;
staticchar buf;
s = &"SunMonTueWedThuFriSat";
cp = buf;
*cp++ = *s++;
*cp++ = *s++;
*cp++ = *s;
*cp++ = ' ';
s = &"JanFebMarAprMayJunJulAugSepOctNovDec";
*cp++ = *s++;
*cp++ = *s++;
*cp = *s;
buf = ' ';
put2d(buf+10, tim->tm_mday, ' ', ' ');
put2d(buf+13, tim->tm_hour, '0', ':');
put2d(buf+16, tim->tm_min, '0', ':');
put2d(buf+19, tim->tm_sec, '0', ' ');
put2d(buf+22, tim->tm_year/100 + 19, ' ', 0);
put2d(buf+24, tim->tm_year%100, '0', ' ');
return(buf);
}
另:库文件中把时间结构体定义成INT,造成RAM浪费。
struct tm {
char tm_sec;
char tm_min;
char tm_hour;
char tm_mday;
char tm_mon;
char tm_year;
char tm_wday;
int tm_yday;
char tm_isdst;
};
修改如上。
页:
[1]