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 <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-03-29 12:30:33 +01:00
parent 6c852bfcad
commit 8f2cb7ab26
7 changed files with 35 additions and 12 deletions

View File

@ -127,7 +127,7 @@ static const char *human_time(time_t t)
/*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
#define buf bb_common_bufsiz1 #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; return buf;
#undef buf #undef buf
} }

View File

@ -523,7 +523,8 @@ struct BUG_too_small {
void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; 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; 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; int xsocket(int domain, int type, int protocol) FAST_FUNC;
void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC; void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;

View File

@ -16,7 +16,6 @@
#define LOGIN " login: " #define LOGIN " login: "
static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y"; 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) 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)); strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
break; break;
case 't': case 't':
strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); strftime_HHMMSS(buf, sizeof(buf), &t);
break; break;
case 'l': case 'l':
outbuf = tty; outbuf = tty;

View File

@ -187,6 +187,27 @@ time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
return t; 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 #if ENABLE_MONOTONIC_SYSCALL
#include <sys/syscall.h> #include <sys/syscall.h>

View File

@ -887,11 +887,11 @@ step_time(double offset)
VERB2 { VERB2 {
tval = tvc.tv_sec; 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); bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec);
} }
tval = tvn.tv_sec; 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); bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset);
/* Correct various fields which contain time-relative values: */ /* Correct various fields which contain time-relative values: */

View File

@ -378,7 +378,7 @@ step_time_once(double offset)
bb_perror_msg_and_die("settimeofday"); bb_perror_msg_and_die("settimeofday");
tval = tv.tv_sec; 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); bb_error_msg("setting clock to %s (offset %fs)", buf, offset);

View File

@ -69,7 +69,6 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
printf("\033[H""\033[J"); printf("\033[H""\033[J");
if (!(opt & 0x2)) { // no -t if (!(opt & 0x2)) { // no -t
const unsigned time_len = sizeof("1234-67-90 23:56:89"); const unsigned time_len = sizeof("1234-67-90 23:56:89");
time_t t;
// STDERR_FILENO is procps3 compat: // STDERR_FILENO is procps3 compat:
// "watch ls 2>/dev/null" does not detect tty size // "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); free(header);
header = xasprintf("Every %us: %-*s", period, (int)width, cmd); header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
} }
time(&t); if (time_len < width) {
if (time_len < width) strftime_YYYYMMDDHHMMSS(
strftime(header + width - time_len, time_len, header + width - time_len,
"%Y-%m-%d %H:%M:%S", localtime(&t)); time_len,
/*time_t*:*/ NULL
);
}
// compat: empty line between header and cmd output // compat: empty line between header and cmd output
printf("%s\n\n", header); printf("%s\n\n", header);