]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: Date and time fonctions that don't use snprintf
authorWilliam Lallemand <wlallemand@exceliance.fr>
Mon, 6 Feb 2012 17:15:57 +0000 (18:15 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 9 Feb 2012 16:03:28 +0000 (17:03 +0100)
Also move human_time() to standard.c since it's not related to
timeval calculations.

include/common/standard.h
include/common/time.h
src/log.c
src/standard.c
src/time.c

index 69a38875a5de15b0c9e0814fd479854f60d90381..065a4fd4a36a2fd771a1caefa2735b9f79cc8621 100644 (file)
@@ -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 */
index 14ca58959d7243c9604bad96742e15f86e3d9ddc..588180d20a5aef2ff3518c6ec77239959bba558b 100644 (file)
 #include <common/config.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 */
 #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> 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;                       \
 })
 
-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 <before_poll> to be
  * updated to the system time before calling poll().
index b80969ff1d1bc8d42fbd7228af466007150a08fd..8f639e0a36926ba631207dc822f3b032a02db596 100644 (file)
--- 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 */
 
index c790ceb03cbf82d03b837ba61c759d3311693f93..d9b585ea15e9e9d5117dec26ba847c15d7d266a3 100644 (file)
@@ -22,7 +22,6 @@
 #include <common/config.h>
 #include <common/standard.h>
 #include <eb32tree.h>
-#include <proto/log.h>
 
 /* 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
index 4c412391828e262e127604e6ecca59e43dcc9518..4149316d191e80a69d504db2ea4f8c653f368bb0 100644 (file)
@@ -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