diff --git a/include/common/standard.h b/include/common/standard.h index 69a38875a..065a4fd4a 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -469,6 +469,11 @@ extern const char *parse_size_err(const char *text, unsigned *ret); #define TIME_UNIT_DAY 0x0005 #define TIME_UNIT_MASK 0x0007 +#define SEC 1 +#define MINUTE (60 * SEC) +#define HOUR (60 * MINUTE) +#define DAY (24 * HOUR) + /* Multiply the two 32-bit operands and shift the 64-bit result right 32 bits. * This is used to compute fixed ratios by setting one of the operands to * (2^32*ratio). @@ -642,4 +647,25 @@ extern void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr); */ extern int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr); +char *human_time(int t, short hz_div); + +extern const char *monthname[]; + +/* date2str_log: write a date in the format : + * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", + * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, + * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); + * + * without using sprintf. return a pointer to the last char written (\0) or + * NULL if there isn't enough space. + */ +char *date2str_log(char *dest, struct tm *tm, struct timeval *date, size_t size); + +/* gmt2str_log: write a date in the format : + * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf + * return a pointer to the last char written (\0) or + * NULL if there isn't enough space. + */ +char *gmt2str_log(char *dst, struct tm *tm, size_t size); + #endif /* _COMMON_STANDARD_H */ diff --git a/include/common/time.h b/include/common/time.h index 14ca58959..588180d20 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -27,11 +27,6 @@ #include #include -#define SEC 1 -#define MINUTE (60 * SEC) -#define HOUR (60 * MINUTE) -#define DAY (24 * HOUR) - /* eternity when exprimed in timeval */ #ifndef TV_ETERNITY #define TV_ETERNITY (~0UL) @@ -68,8 +63,6 @@ extern struct timeval after_poll; /* system date after leaving poll() */ /**** exported functions *************************************************/ - - /* * adds ms to , set the result to and returns a pointer */ @@ -519,8 +512,6 @@ REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const stru tv1; \ }) -char *human_time(int t, short hz_div); - /* Update the idle time value twice a second, to be called after * tv_update_date() when called after poll(). It relies on to be * updated to the system time before calling poll(). diff --git a/src/log.c b/src/log.c index b80969ff1..8f639e0a3 100644 --- a/src/log.c +++ b/src/log.c @@ -48,11 +48,6 @@ const char *log_levels[NB_LOG_LEVELS] = { "warning", "notice", "info", "debug" }; -const char *monthname[12] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; - const char sess_term_cond[10] = "-cCsSPRIDK"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed */ const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */ diff --git a/src/standard.c b/src/standard.c index c790ceb03..d9b585ea1 100644 --- a/src/standard.c +++ b/src/standard.c @@ -22,7 +22,6 @@ #include #include #include -#include /* enough to store 10 integers of : * 2^64-1 = 18446744073709551615 or @@ -1623,6 +1622,113 @@ int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) return 0; } +char *human_time(int t, short hz_div) { + static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" + char *p = rv; + int cnt=2; // print two numbers + + if (unlikely(t < 0 || hz_div <= 0)) { + sprintf(p, "?"); + return rv; + } + + if (unlikely(hz_div > 1)) + t /= hz_div; + + if (t >= DAY) { + p += sprintf(p, "%dd", t / DAY); + cnt--; + } + + if (cnt && t % DAY / HOUR) { + p += sprintf(p, "%dh", t % DAY / HOUR); + cnt--; + } + + if (cnt && t % HOUR / MINUTE) { + p += sprintf(p, "%dm", t % HOUR / MINUTE); + cnt--; + } + + if ((cnt && t % MINUTE) || !t) // also display '0s' + p += sprintf(p, "%ds", t % MINUTE / SEC); + + return rv; +} + +const char *monthname[12] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +}; + +/* date2str_log: write a date in the format : + * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", + * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, + * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); + * + * without using sprintf. return a pointer to the last char written (\0) or + * NULL if there isn't enough space. + */ +char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size) +{ + + if (size < 25) /* the size is fixed: 24 chars + \0 */ + return NULL; + + dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day + *dst++ = '/'; + memcpy(dst, monthname[tm->tm_mon], 3); // month + dst += 3; + *dst++ = '/'; + dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year + *dst++ = ':'; + dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour + *dst++ = ':'; + dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes + *dst++ = ':'; + dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes + *dst++ = '.'; + utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes + dst += 3; // only the 3 first digits + *dst = '\0'; + + return dst; +} + +/* gmt2str_log: write a date in the format : + * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf + * return a pointer to the last char written (\0) or + * NULL if there isn't enough space. + */ +char *gmt2str_log(char *dst, struct tm *tm, size_t size) +{ + if (size < 27) /* the size is fixed: 24 chars + \0 */ + return NULL; + + dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day + *dst++ = '/'; + memcpy(dst, monthname[tm->tm_mon], 3); // month + dst += 3; + *dst++ = '/'; + dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year + *dst++ = ':'; + dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour + *dst++ = ':'; + dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes + *dst++ = ':'; + dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes + *dst++ = ' '; + *dst++ = '+'; + *dst++ = '0'; + *dst++ = '0'; + *dst++ = '0'; + *dst++ = '0'; + *dst = '\0'; + + return dst; +} + + /* * Local variables: * c-indent-level: 8 diff --git a/src/time.c b/src/time.c index 4c4123918..4149316d1 100644 --- a/src/time.c +++ b/src/time.c @@ -208,40 +208,6 @@ REGPRM2 void tv_update_date(int max_wait, int interrupted) return; } -char *human_time(int t, short hz_div) { - static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" - char *p = rv; - int cnt=2; // print two numbers - - if (unlikely(t < 0 || hz_div <= 0)) { - sprintf(p, "?"); - return rv; - } - - if (unlikely(hz_div > 1)) - t /= hz_div; - - if (t >= DAY) { - p += sprintf(p, "%dd", t / DAY); - cnt--; - } - - if (cnt && t % DAY / HOUR) { - p += sprintf(p, "%dh", t % DAY / HOUR); - cnt--; - } - - if (cnt && t % HOUR / MINUTE) { - p += sprintf(p, "%dm", t % HOUR / MINUTE); - cnt--; - } - - if ((cnt && t % MINUTE) || !t) // also display '0s' - p += sprintf(p, "%ds", t % MINUTE / SEC); - - return rv; -} - /* * Local variables: * c-indent-level: 8