X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Fstrftime.c;h=3928a6f01f11a63c922b4080c33c9f11274a6e82;hp=f349bbf4e0f4be4c2b3fac120140f209c65a4a6c;hb=96b0f4aace0deef034a792a08dc2d426cd2b61a4;hpb=c768fa74de0a05c284a73c9dc147c55da65ac416 diff --git a/src/strftime.c b/src/strftime.c index f349bbf..3928a6f 100644 --- a/src/strftime.c +++ b/src/strftime.c @@ -17,6 +17,13 @@ #include /* for struct tm */ #include "strftime.h" +/* Define your own defaults in config.h if necessary */ +#if defined(TZNAME_STD) && defined(TZNAME_DST) +char *tzname_[2] = { TZNAME_STD, TZNAME_DST }; +#else +#define tzname_ tzname +#endif + static char *aday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; @@ -36,11 +43,12 @@ static char *month[] = { "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, ...); +static void strfmt( + char *str, + const char *fmt, + ...); /** * @@ -109,163 +117,161 @@ static void strfmt(char *str, const char *fmt, ...); * **/ -size_t strftime_(char *s, size_t maxs, const char *f, const struct tm *t) +size_t strftime_( + char *s, + size_t maxs, + const char *f, + const struct tm *t) { - int w,d; - 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 'V': - - /* ISO 8601 Week Of Year: - If the week (Monday - Sunday) containing January 1 has four or more - days in the new year, then it is week 1; otherwise it is week 53 of - the previous year and the next week is week one. */ - - w = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) / 7; - d = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) % 7; - - if (d >= 4) { w++; } else if (w == 0) { w = 53; } - 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++; - } + int w, d; + 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 'V': + + /* ISO 8601 Week Of Year: + If the week (Monday - Sunday) containing January 1 has four or more + days in the new year, then it is week 1; otherwise it is week 53 of + the previous year and the next week is week one. */ + + w = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) / 7; + d = (t->tm_yday + 7 - (t->tm_wday ? t->tm_wday - 1 : 6)) % 7; + + if (d >= 4) { + w++; + } else if (w == 0) { + w = 53; + } + 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]; + } else { + if (p == q) { + *q = '\0'; + return 0; } - } - *p = '\0'; - return p - s; + *p++ = f[-1]; + } + } + *p = '\0'; + return p - s; } /* @@ -278,7 +284,7 @@ typedef void *va_list; * */ -static int pow[5] = { 1, 10, 100, 1000, 10000 }; +static int powers[5] = { 1, 10, 100, 1000, 10000 }; /** * static void strfmt(char *str, char *fmt); @@ -293,38 +299,38 @@ static int pow[5] = { 1, 10, 100, 1000, 10000 }; * **/ -static void strfmt(char *str, const char *fmt, ...) +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 %= pow[ilen--]; - *str++ = (char)('0' + ival / pow[ilen]); - } - } + 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 %= powers[ilen--]; + *str++ = (char) ('0' + ival / powers[ilen]); + } } - else *str++ = fmt[-1]; - } - *str = '\0'; - va_end(vp); + } else + *str++ = fmt[-1]; + } + *str = '\0'; + va_end(vp); } #ifdef TEST @@ -332,20 +338,22 @@ static void strfmt(char *str, const char *fmt, ...) #include /* for printf */ #include /* for strftime */ -char test[80]; +char test[80]; -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { - int len; - char *fmt; - time_t now; + int len; + char *fmt; + time_t now; - time(&now); + time(&now); - fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1]; - len = strftime_(test,sizeof test, fmt, localtime(&now)); - printf("%d: %s\n", len, test); - return !len; + fmt = (argc == 1) ? "%I:%M %p\n%c\n" : argv[1]; + len = strftime_(test, sizeof test, fmt, localtime(&now)); + printf("%d: %s\n", len, test); + return !len; } -#endif /* TEST */ +#endif /* TEST */