📄 rvtm.c
字号:
return RvGmtime_r(t, tm);
}
/*************************************************************************/
#endif /* Nucleus */ /* WinCE */
#if (RV_OS_TYPE == RV_OS_TYPE_WINCE)
struct tm * __cdecl localtime(const time_t * ptime)
{
SYSTEMTIME stime;
static struct tm t;
if (ptime);
GetSystemTime(&stime);
t.tm_year = stime.wYear;
t.tm_mon = stime.wMonth;
t.tm_wday = stime.wDayOfWeek;
t.tm_mday = stime.wDay;
t.tm_hour = stime.wHour;
t.tm_min = stime.wMinute;
t.tm_sec = stime.wSecond;
t.tm_yday = 0;
t.tm_isdst = 0;
return &t;
}
/* =========================================== */
/* =========================================== */
/* Effi */
/* Implementation of strftime(). */
/* found on the internet, needs checking */
static char *aday[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static char *day[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
static char *amonth[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char *month[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
char *tzname_[2] = {"CST", "CDT"}; /* Add your own defaults here */
static char buf[26];
static void strfmt(char *str, const char *fmt, ...);
/**
*
* size_t strftime(char *str,
* size_t maxs,
* const char *fmt,
* const struct tm *t)
*
* this functions acts much like a sprintf for time/date output.
* given a pointer to an output buffer, a format string and a
* time, it copies the time to the output buffer formatted in
* accordance with the format string. the parameters are used
* as follows:
*
* str is a pointer to the output buffer, there should
* be at least maxs characters available at the address
* pointed to by str.
*
* maxs is the maximum number of characters to be copied
* into the output buffer, included the '\0' terminator
*
* fmt is the format string. a percent sign (%) is used
* to indicate that the following character is a special
* format character. the following are valid format
* characters:
*
* %A full weekday name (Monday)
* %a abbreviated weekday name (Mon)
* %B full month name (January)
* %b abbreviated month name (Jan)
* %c standard date and time representation
* %d day-of-month (01-31)
* %H hour (24 hour clock) (00-23)
* %I hour (12 hour clock) (01-12)
* %j day-of-year (001-366)
* %M minute (00-59)
* %m month (01-12)
* %p local equivalent of AM or PM
* %S second (00-59)
* %U week-of-year, first day sunday (00-53)
* %W week-of-year, first day monday (00-53)
* %w weekday (0-6, sunday is 0)
* %X standard time representation
* %x standard date representation
* %Y year with century
* %y year without century (00-99)
* %Z timezone name
* %% percent sign
*
* the standard date string is equivalent to:
*
* %a %b %d %Y
*
* the standard time string is equivalent to:
*
* %H:%M:%S
*
* the standard date and time string is equivalent to:
*
* %a %b %d %H:%M:%S %Y
*
* strftime() returns the number of characters placed in the
* buffer, not including the terminating \0, or zero if more
* than maxs characters were produced.
*
**/
size_t strftime(char *s, size_t maxs, const char *f, const struct tm *t)
{
int w;
char *p, *q, *r;
p = s;
q = s + maxs - 1;
while ((*f != '\0'))
{
if (*f++ == '%')
{
r = buf;
switch (*f++)
{
case '%' :
r = "%";
break;
case 'a' :
r = aday[t->tm_wday];
break;
case 'A' :
r = day[t->tm_wday];
break;
case 'b' :
r = amonth[t->tm_mon];
break;
case 'B' :
r = month[t->tm_mon];
break;
case 'c' :
strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
aday[t->tm_wday], amonth[t->tm_mon],
t->tm_mday,t->tm_hour, t->tm_min,
t->tm_sec, t->tm_year+1900);
break;
case 'd' :
strfmt(r,"%2",t->tm_mday);
break;
case 'H' :
strfmt(r,"%2",t->tm_hour);
break;
case 'I' :
strfmt(r,"%2",(t->tm_hour%12)?t->tm_hour%12:12);
break;
case 'j' :
strfmt(r,"%3",t->tm_yday+1);
break;
case 'm' :
strfmt(r,"%2",t->tm_mon+1);
break;
case 'M' :
strfmt(r,"%2",t->tm_min);
break;
case 'p' :
r = (t->tm_hour>11)?"PM":"AM";
break;
case 'S' :
strfmt(r,"%2",t->tm_sec);
break;
case 'U' :
w = t->tm_yday/7;
if (t->tm_yday%7 > t->tm_wday)
w++;
strfmt(r, "%2", w);
break;
case 'W' :
w = t->tm_yday/7;
if (t->tm_yday%7 > (t->tm_wday+6)%7)
w++;
strfmt(r, "%2", w);
break;
case 'w' :
strfmt(r,"%1",t->tm_wday);
break;
case 'x' :
strfmt(r, "%3s %3s %2 %4", aday[t->tm_wday],
amonth[t->tm_mon], t->tm_mday, t->tm_year+1900);
break;
case 'X' :
strfmt(r, "%2:%2:%2", t->tm_hour,
t->tm_min, t->tm_sec);
break;
case 'y' :
strfmt(r,"%2",t->tm_year%100);
break;
case 'Y' :
strfmt(r,"%4",t->tm_year+1900);
break;
case 'Z' :
r = (t->tm_isdst && tzname_[1][0]) ?
tzname_[1] : tzname_[0];
break;
default:
buf[0] = '%'; /* reconstruct the format */
buf[1] = f[-1];
buf[2] = '\0';
if (buf[1] == 0)
f--; /* back up if at end of string */
}
while (*r)
{
if (p == q)
{
*q = '\0';
return 0;
}
*p++ = *r++;
}
}
else
{
if (p == q)
{
*q = '\0';
return 0;
}
*p++ = f[-1];
}
}
*p = '\0';
return p - s;
}
static int power[5] = { 1, 10, 100, 1000, 10000 };
/**
* static void strfmt(char *str, char *fmt);
*
* simple sprintf for strftime
*
* each format descriptor is of the form %n
* where n goes from zero to four
*
* 0 -- string %s
* 1..4 -- int %?.?d
*
**/
static void strfmt(char *str, const char *fmt, ...)
{
int ival, ilen;
char *sval;
va_list vp;
va_start(vp, fmt);
while (*fmt)
{
if (*fmt++ == '%')
{
ilen = *fmt++ - '0';
if (ilen == 0) /* zero means string arg */
{
sval = va_arg(vp, char*);
while (*sval)
*str++ = *sval++;
}
else /* always leading zeros */
{
ival = va_arg(vp, int);
while (ilen)
{
ival %= power[ilen--];
*str++ = (char)('0' + ival / power[ilen]);
}
}
}
else *str++ = fmt[-1];
}
*str = '\0';
va_end(vp);
}
#endif /* WINCE */
#if defined(RV_TEST_CODE)
#define TMNSECS 100000
#define TMSEC 30
#define TMMIN 45
#define TMHOUR 15
#define TMMDAY 20
#define TMMON 8
#define TMYEAR 2001
#define TMISDST RV_TM_DST
#define TIMEVAL 997380342
void RvTmTest(void)
{
RvStatus result;
RvChar buf[RV_TM_ASCTIME_BUFSIZE], buf2[80], *cresult;
RvInt32 numresult;
RvTime t1, t2;
RvTm tm1, tm2, tm3, *tmresult;
RvPrintf("Starting test of rvtm.\n");
result = RvTmInit();
if(result == RV_OK) {
RvPrintf("RvTmInit successful.\n");
} else RvPrintf("RvTmInit failed.\n");
tmresult = RvTmConstruct(&tm1);
if(tmresult != NULL) {
RvPrintf("RvTmConstruct OK\n");
} else RvPrintf("RvTmConstruct Failed\n");
RvTmSetNsec(&tm1, TMNSECS);
RvTmSetSec(&tm1, TMSEC);
RvTmSetMin(&tm1, TMMIN);
RvTmSetHour(&tm1, TMHOUR);
RvTmSetMday(&tm1, TMMDAY);
RvTmSetMon(&tm1, TMMON);
RvTmSetYear(&tm1, TMYEAR);
RvTmSetIsdst(&tm1, TMISDST);
RvPrintf("Inital tm:\n");
RvPrintf(" nanoseconds = %d\n", RvTmGetNsec(&tm1));
RvPrintf(" seconds = %d\n", RvTmGetSec(&tm1));
RvPrintf(" minutes = %d\n", RvTmGetMin(&tm1));
RvPrintf(" hours = %d\n", RvTmGetHour(&tm1));
RvPrintf(" day of month = %d\n", RvTmGetMday(&tm1));
RvPrintf(" month = %d\n", RvTmGetMon(&tm1));
RvPrintf(" year = %d\n", RvTmGetYear(&tm1));
RvPrintf(" weekday = %d\n", RvTmGetWday(&tm1));
RvPrintf(" day of year = %d\n", RvTmGetYday(&tm1));
RvPrintf(" daylight savings = %d\n", RvTmGetIsdst(&tm1));
RvPrintf("RvTmConvertToTime: ");
result = RvTmConvertToTime(&tm1, &t1);
if(result == RV_OK) {
RvPrintf("%d %d\n", RvTimeGetSecs(&t1), RvTimeGetNsecs(&t1));
RvPrintf("Updated tm:\n");
RvPrintf(" nanoseconds = %d\n", RvTmGetNsec(&tm1));
RvPrintf(" seconds = %d\n", RvTmGetSec(&tm1));
RvPrintf(" minutes = %d\n", RvTmGetMin(&tm1));
RvPrintf(" hours = %d\n", RvTmGetHour(&tm1));
RvPrintf(" day of month = %d\n", RvTmGetMday(&tm1));
RvPrintf(" month = %d\n", RvTmGetMon(&tm1));
RvPrintf(" year = %d\n", RvTmGetYear(&tm1));
RvPrintf(" weekday = %d\n", RvTmGetWday(&tm1));
RvPrintf(" day of year = %d\n", RvTmGetYday(&tm1));
RvPrintf(" daylight savings = %d\n", RvTmGetIsdst(&tm1));
} else RvPrintf("ERROR! Code = %d\n", result);
RvPrintf("RvTmAsctime: ");
cresult = RvTmAsctime(&tm1, buf, RV_TM_ASCTIME_BUFSIZE);
if(cresult != NULL) {
RvPrintf("%s", buf);
} else RvPrintf("ERROR!\n");
RvPrintf("RvTmStrftime: ");
numresult = RvTmStrftime(&tm1, "%I:%M:%S%p %Z %m/%d/%Y", buf2, sizeof(buf2));
if(numresult > 0) {
RvPrintf("Result = %d, String = %s\n", numresult, buf2);
} else RvPrintf("ERROR!\n");
RvTimeConstruct(&t2, TIMEVAL, TMNSECS);
RvPrintf("Timeval: %d %d\n", RvTimeGetSecs(&t2), RvTimeGetNsecs(&t2));
RvPrintf("RvTmConstructUtc: ");
result = RvTmConstructUtc(&tm2, &t2);
if(result == RV_OK) {
RvPrintf("\n");
RvPrintf(" nanoseconds = %d\n", RvTmGetNsec(&tm2));
RvPrintf(" seconds = %d\n", RvTmGetSec(&tm2));
RvPrintf(" minutes = %d\n", RvTmGetMin(&tm2));
RvPrintf(" hours = %d\n", RvTmGetHour(&tm2));
RvPrintf(" day of month = %d\n", RvTmGetMday(&tm2));
RvPrintf(" month = %d\n", RvTmGetMon(&tm2));
RvPrintf(" year = %d\n", RvTmGetYear(&tm2));
RvPrintf(" weekday = %d\n", RvTmGetWday(&tm2));
RvPrintf(" day of year = %d\n", RvTmGetYday(&tm2));
RvPrintf(" daylight savings = %d\n", RvTmGetIsdst(&tm2));
} else RvPrintf("ERROR! Code = %d\n", result);
RvPrintf("RvTmConstructLocal: ");
result = RvTmConstructLocal(&tm3, &t2);
if(result == RV_OK) {
RvPrintf("\n");
RvPrintf(" nanoseconds = %d\n", RvTmGetNsec(&tm3));
RvPrintf(" seconds = %d\n", RvTmGetSec(&tm3));
RvPrintf(" minutes = %d\n", RvTmGetMin(&tm3));
RvPrintf(" hours = %d\n", RvTmGetHour(&tm3));
RvPrintf(" day of month = %d\n", RvTmGetMday(&tm3));
RvPrintf(" month = %d\n", RvTmGetMon(&tm3));
RvPrintf(" year = %d\n", RvTmGetYear(&tm3));
RvPrintf(" weekday = %d\n", RvTmGetWday(&tm3));
RvPrintf(" day of year = %d\n", RvTmGetYday(&tm3));
RvPrintf(" daylight savings = %d\n", RvTmGetIsdst(&tm3));
} else RvPrintf("ERROR! Code = %d\n", result);
result = RvTmEnd();
if(result == RV_OK) {
RvPrintf("RvTmEnd successful.\n");
} else RvPrintf("RvTmEnd failed.\n");
}
#endif /* RV_TEST_CODE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -