我使用的是PICC自带的time函数,使用time_t mktime(struct tm *ptr)函数来转换时间。仿真时发现指向事先定义的struct tm CurTime的结构体指针传进去之后,而在local窗口中发现显示的值不正确,不是CurTime的值。
测试程序如下,请各位帮忙分析。其中time.h和mktime.c:为PICC自带库函数。
//main.c
#include "pic18f67k22.h"
#include "time.h"
struct tm CurTime;
main()
{
time_t tt;
while(1)
{
CurTime.tm_sec = 20;
CurTime.tm_min = 0;
CurTime.tm_hour = 0;
CurTime.tm_mday = 1;
CurTime.tm_mon = 1;
CurTime.tm_year = 70;
tt = mktime(&CurTime);
}
}
//time.h
#ifndef _TIME
#ifndef _STDDEF
typedef int ptrdiff_t; /* result type of pointer difference */
typedef unsigned size_t; /* type yielded by sizeof */
typedef unsigned short wchar_t; /* wide char type */
#define _STDDEF
#define offsetof(ty, mem) ((int)&(((ty *)0)->mem))
#endif /* _STDDEF */
typedef long time_t; /* for representing times in seconds */
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
#define CLOCKS_PER_SEC 1
#define clock() (-1L)
#define difftime(t1, t0) ((double)((time_t)(t1)-(time_t)(t0)))
#define _TIME
#endif /* _TIME */
extern int time_zone; /* minutes WESTWARD of Greenwich */
/* this value defaults to 0 since with
operating systems like MS-DOS there is
no time zone information available */
extern time_t time(time_t *); /* seconds since 00:00:00 Jan 1 1970 */
extern int stime(time_t *); /* set time */
extern char * asctime(const struct tm *); /* converts struct tm to ascii time */
extern char * ctime(const time_t *); /* current local time in ascii form */
extern struct tm * gmtime(const time_t *); /* Universal time */
extern struct tm * localtime(const time_t *); /* local time */
extern time_t mktime(struct tm *);
extern size_t strftime(char *, size_t, const char *, const struct tm *);
//mktime.c
#include <time.h>
#include <stdlib.h>
#include <string.h>
static int
isleap(unsigned yr)
{
return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);
}
static unsigned
months_to_days(unsigned month)
{
return (month * 3057 - 3007) / 100;
}
static long
years_to_days (unsigned yr)
{
return yr * 365L + yr / 4 - yr / 100 + yr / 400;
}
/*
* Return the number of days. The function takes
* into account leap years.
*
* yr : must be greater than 0
* mo : must be between 1 and 12
* day: must be between 1 and 32
* */
static long
ymd_to_scalar(unsigned yr, unsigned mo, unsigned day)
{
long scalar;
scalar = day + months_to_days(mo);
if ( mo > 2 ) /* adjust if past February */
scalar -= isleap(yr) ? 1 : 2;
yr--;
scalar += years_to_days(yr);
return scalar;
}
time_t
mktime(struct tm *timeptr)
{
time_t tt;
if ((timeptr->tm_year < 70) || (timeptr->tm_year > 138))
{
tt = (time_t)-1;
}
else
{
tt = ymd_to_scalar(timeptr->tm_year + 1900,
timeptr->tm_mon + 1,
timeptr->tm_mday)
- ymd_to_scalar(1970, 1, 1);
tt = tt * 24 + timeptr->tm_hour;
tt = tt * 60 + timeptr->tm_min;
tt = tt * 60 + timeptr->tm_sec;
}
return tt;
}
|