From 8f2cb7ab26b9c720c24cdeffb624bfe0c2352353 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 29 Mar 2013 12:30:33 +0100 Subject: [PATCH] libbb: introduce and use strftime_[YYYYMMDD]HHMMSS() function old new delta strftime_fmt - 53 +53 strftime_YYYYMMDDHHMMSS - 12 +12 strftime_HHMMSS - 12 +12 human_time 44 43 -1 fmtstr_t 9 - -9 step_time 361 345 -16 watch_main 261 232 -29 ------------------------------------------------------------------------------ (add/remove: 3/1 grow/shrink: 0/3 up/down: 77/-55) Total: 22 bytes text data bss dec hex filename 919203 932 17692 937827 e4f63 busybox_old 919209 932 17692 937833 e4f69 busybox_unstripped Signed-off-by: Denys Vlasenko --- coreutils/stat.c | 2 +- include/libbb.h | 3 ++- libbb/login.c | 3 +-- libbb/time.c | 21 +++++++++++++++++++++ networking/ntpd.c | 4 ++-- networking/ntpd_simple.c | 2 +- procps/watch.c | 12 +++++++----- 7 files changed, 35 insertions(+), 12 deletions(-) diff --git a/coreutils/stat.c b/coreutils/stat.c index c8677ebaa..dc9d81c35 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -127,7 +127,7 @@ static const char *human_time(time_t t) /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ #define buf bb_common_bufsiz1 - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t)); + strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000"); return buf; #undef buf } diff --git a/include/libbb.h b/include/libbb.h index 79a37a759..6dd4d7cae 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -523,7 +523,8 @@ struct BUG_too_small { void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; - +char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; +char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; int xsocket(int domain, int type, int protocol) FAST_FUNC; void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC; diff --git a/libbb/login.c b/libbb/login.c index 8a82c6add..8f080b775 100644 --- a/libbb/login.c +++ b/libbb/login.c @@ -16,7 +16,6 @@ #define LOGIN " login: " static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y"; -static const char fmtstr_t[] ALIGN1 = "%H:%M:%S"; void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) { @@ -73,7 +72,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) strftime(buf, sizeof(buf), fmtstr_d, localtime(&t)); break; case 't': - strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); + strftime_HHMMSS(buf, sizeof(buf), &t); break; case 'l': outbuf = tty; diff --git a/libbb/time.c b/libbb/time.c index e2b938471..57e14b66c 100644 --- a/libbb/time.c +++ b/libbb/time.c @@ -187,6 +187,27 @@ time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) return t; } +static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt) +{ + time_t t; + if (!tp) { + tp = &t; + time(tp); + } + /* Returns pointer to NUL */ + return buf + strftime(buf, len, fmt, localtime(tp)); +} + +char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp) +{ + return strftime_fmt(buf, len, tp, "%H:%M:%S"); +} + +char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) +{ + return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S"); +} + #if ENABLE_MONOTONIC_SYSCALL #include diff --git a/networking/ntpd.c b/networking/ntpd.c index 79b7c3793..0f4319ef2 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -887,11 +887,11 @@ step_time(double offset) VERB2 { tval = tvc.tv_sec; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval)); + strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec); } tval = tvn.tv_sec; - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval)); + strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset); /* Correct various fields which contain time-relative values: */ diff --git a/networking/ntpd_simple.c b/networking/ntpd_simple.c index 55bded8ff..3e7fc4719 100644 --- a/networking/ntpd_simple.c +++ b/networking/ntpd_simple.c @@ -378,7 +378,7 @@ step_time_once(double offset) bb_perror_msg_and_die("settimeofday"); tval = tv.tv_sec; - strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval)); + strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); bb_error_msg("setting clock to %s (offset %fs)", buf, offset); diff --git a/procps/watch.c b/procps/watch.c index 36af1cca7..0397f21bf 100644 --- a/procps/watch.c +++ b/procps/watch.c @@ -69,7 +69,6 @@ int watch_main(int argc UNUSED_PARAM, char **argv) printf("\033[H""\033[J"); if (!(opt & 0x2)) { // no -t const unsigned time_len = sizeof("1234-67-90 23:56:89"); - time_t t; // STDERR_FILENO is procps3 compat: // "watch ls 2>/dev/null" does not detect tty size @@ -79,10 +78,13 @@ int watch_main(int argc UNUSED_PARAM, char **argv) free(header); header = xasprintf("Every %us: %-*s", period, (int)width, cmd); } - time(&t); - if (time_len < width) - strftime(header + width - time_len, time_len, - "%Y-%m-%d %H:%M:%S", localtime(&t)); + if (time_len < width) { + strftime_YYYYMMDDHHMMSS( + header + width - time_len, + time_len, + /*time_t*:*/ NULL + ); + } // compat: empty line between header and cmd output printf("%s\n\n", header); -- 2.25.1