mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-22 06:11:32 +02:00
MINOR: Date and time fonctions that don't use snprintf
Also move human_time() to standard.c since it's not related to timeval calculations.
This commit is contained in:
parent
e7340ec111
commit
421f5b5882
@ -469,6 +469,11 @@ extern const char *parse_size_err(const char *text, unsigned *ret);
|
|||||||
#define TIME_UNIT_DAY 0x0005
|
#define TIME_UNIT_DAY 0x0005
|
||||||
#define TIME_UNIT_MASK 0x0007
|
#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.
|
/* 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
|
* This is used to compute fixed ratios by setting one of the operands to
|
||||||
* (2^32*ratio).
|
* (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);
|
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 */
|
#endif /* _COMMON_STANDARD_H */
|
||||||
|
@ -27,11 +27,6 @@
|
|||||||
#include <common/config.h>
|
#include <common/config.h>
|
||||||
#include <common/standard.h>
|
#include <common/standard.h>
|
||||||
|
|
||||||
#define SEC 1
|
|
||||||
#define MINUTE (60 * SEC)
|
|
||||||
#define HOUR (60 * MINUTE)
|
|
||||||
#define DAY (24 * HOUR)
|
|
||||||
|
|
||||||
/* eternity when exprimed in timeval */
|
/* eternity when exprimed in timeval */
|
||||||
#ifndef TV_ETERNITY
|
#ifndef TV_ETERNITY
|
||||||
#define TV_ETERNITY (~0UL)
|
#define TV_ETERNITY (~0UL)
|
||||||
@ -68,8 +63,6 @@ extern struct timeval after_poll; /* system date after leaving poll() */
|
|||||||
|
|
||||||
|
|
||||||
/**** exported functions *************************************************/
|
/**** exported functions *************************************************/
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
|
* adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
|
||||||
*/
|
*/
|
||||||
@ -519,8 +512,6 @@ REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const stru
|
|||||||
tv1; \
|
tv1; \
|
||||||
})
|
})
|
||||||
|
|
||||||
char *human_time(int t, short hz_div);
|
|
||||||
|
|
||||||
/* Update the idle time value twice a second, to be called after
|
/* Update the idle time value twice a second, to be called after
|
||||||
* tv_update_date() when called after poll(). It relies on <before_poll> to be
|
* tv_update_date() when called after poll(). It relies on <before_poll> to be
|
||||||
* updated to the system time before calling poll().
|
* updated to the system time before calling poll().
|
||||||
|
@ -48,11 +48,6 @@ const char *log_levels[NB_LOG_LEVELS] = {
|
|||||||
"warning", "notice", "info", "debug"
|
"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_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 */
|
const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
|
||||||
|
|
||||||
|
108
src/standard.c
108
src/standard.c
@ -22,7 +22,6 @@
|
|||||||
#include <common/config.h>
|
#include <common/config.h>
|
||||||
#include <common/standard.h>
|
#include <common/standard.h>
|
||||||
#include <eb32tree.h>
|
#include <eb32tree.h>
|
||||||
#include <proto/log.h>
|
|
||||||
|
|
||||||
/* enough to store 10 integers of :
|
/* enough to store 10 integers of :
|
||||||
* 2^64-1 = 18446744073709551615 or
|
* 2^64-1 = 18446744073709551615 or
|
||||||
@ -1623,6 +1622,113 @@ int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
|
|||||||
return 0;
|
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:
|
* Local variables:
|
||||||
* c-indent-level: 8
|
* c-indent-level: 8
|
||||||
|
34
src/time.c
34
src/time.c
@ -208,40 +208,6 @@ REGPRM2 void tv_update_date(int max_wait, int interrupted)
|
|||||||
return;
|
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:
|
* Local variables:
|
||||||
* c-indent-level: 8
|
* c-indent-level: 8
|
||||||
|
Loading…
x
Reference in New Issue
Block a user