struct tm __time_tm; /* Global shared by gmtime() and localtime(). */
#define time_t uint32_t
struct tm *_time_t2tm(const time_t * timer,
int offset, struct tm * result)
{
int *p;
time_t t1, t, v;
int wday = wday; /* ok to be uninitialized, shutting up warning */
{
const uint16_t *vp;
t = *timer;
p = (int *) result;
p[7] = 0;
vp = _vals;
do {
if ((v = *vp) == 7) {
/* Overflow checking, assuming time_t is long int... */
/* We have days since the epoch, so caluclate the weekday. */
wday = ((int)((t % (*vp)) + 11)) % ((int)(*vp)); /* help bcc */
/* Set divisor to days in 400 years. Be kind to bcc... */
v = ((time_t)(vp[1])) << 2;
++v;
/* Change to days since 1/1/1601 so that for 32 bit time_t
* values, we'll have t >= 0. This should be changed for
* archs with larger time_t types.
* Also, correct for offset since a multiple of 7. */
/* TODO: Does this still work on archs with time_t > 32 bits? */
t += (135140L - 366) + offset; /* 146097 - (365*30 + 7) -366 */
}
if ((t -= ((t1 = t / v) * v)) < 0) {
t += v;
--t1;
}
if ((*vp == 7) && (t == v-1)) {
--t; /* Correct for 400th year leap case */
++p[4]; /* Stash the extra day... */
}
if (v <= 60) {
*p++ = t;
t = t1;
} else {
*p++ = t1;
}
} while (*++vp);
}
if (p[-1] == 4) {
--p[-1];
t = 365;
}
*p += ((int) t); /* result[7] .. tm_yday */
p -= 2; /* at result[5] */
*p = ((((p[-2]<<2) + p[-1])*25 + p[0])<< 2) + p[1] - 299; /* tm_year */
p[1] = wday; /* result[6] .. tm_wday */
{
const unsigned char *d = days;
wday = 1900 + *p;
if (isleap(wday)) {
d += 11;
}
wday = p[2] + 1; /* result[7] .. tm_yday */
*--p = 0; /* at result[4] .. tm_mon */
while (wday > *d) {
wday -= *d;
if (*d == 29) {
d -= 11; /* Backup to non-leap Feb. */
}
++d;
++*p; /* Increment tm_mon. */
}
p[-1] = wday; /* result[3] .. tm_mday */
}
/* TODO -- should this be 0? */
p[4] = 0; /* result[8] .. tm_isdst */
return result;
}
#endif
struct tm *gmtime(int32_t *timer)
{
struct tm *ptm = &__time_tm;
_time_t2tm(timer, 0, ptm); /* Can return NULL... */
return ptm;
}
开源库代码,程序在32位mcu上跑通。