diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-03-29 12:30:33 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-03-29 12:30:33 +0100 |
commit | 8f2cb7ab26b9c720c24cdeffb624bfe0c2352353 (patch) | |
tree | 8d76825f24632d338118919b0553f542fdd3a8aa | |
parent | 6c852bfcadd78f53ec7a5f35882636a2a4a66eb0 (diff) | |
download | busybox-w32-8f2cb7ab26b9c720c24cdeffb624bfe0c2352353.tar.gz busybox-w32-8f2cb7ab26b9c720c24cdeffb624bfe0c2352353.tar.bz2 busybox-w32-8f2cb7ab26b9c720c24cdeffb624bfe0c2352353.zip |
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>
-rw-r--r-- | coreutils/stat.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | libbb/login.c | 3 | ||||
-rw-r--r-- | libbb/time.c | 21 | ||||
-rw-r--r-- | networking/ntpd.c | 4 | ||||
-rw-r--r-- | networking/ntpd_simple.c | 2 | ||||
-rw-r--r-- | 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) | |||
127 | /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ | 127 | /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/ |
128 | #define buf bb_common_bufsiz1 | 128 | #define buf bb_common_bufsiz1 |
129 | 129 | ||
130 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t)); | 130 | strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000"); |
131 | return buf; | 131 | return buf; |
132 | #undef buf | 132 | #undef buf |
133 | } | 133 | } |
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 { | |||
523 | 523 | ||
524 | void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; | 524 | void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; |
525 | time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; | 525 | time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; |
526 | 526 | char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; | |
527 | char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; | ||
527 | 528 | ||
528 | int xsocket(int domain, int type, int protocol) FAST_FUNC; | 529 | int xsocket(int domain, int type, int protocol) FAST_FUNC; |
529 | void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC; | 530 | 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 @@ | |||
16 | #define LOGIN " login: " | 16 | #define LOGIN " login: " |
17 | 17 | ||
18 | static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y"; | 18 | static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y"; |
19 | static const char fmtstr_t[] ALIGN1 = "%H:%M:%S"; | ||
20 | 19 | ||
21 | void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) | 20 | void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) |
22 | { | 21 | { |
@@ -73,7 +72,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) | |||
73 | strftime(buf, sizeof(buf), fmtstr_d, localtime(&t)); | 72 | strftime(buf, sizeof(buf), fmtstr_d, localtime(&t)); |
74 | break; | 73 | break; |
75 | case 't': | 74 | case 't': |
76 | strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); | 75 | strftime_HHMMSS(buf, sizeof(buf), &t); |
77 | break; | 76 | break; |
78 | case 'l': | 77 | case 'l': |
79 | outbuf = tty; | 78 | 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) | |||
187 | return t; | 187 | return t; |
188 | } | 188 | } |
189 | 189 | ||
190 | static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt) | ||
191 | { | ||
192 | time_t t; | ||
193 | if (!tp) { | ||
194 | tp = &t; | ||
195 | time(tp); | ||
196 | } | ||
197 | /* Returns pointer to NUL */ | ||
198 | return buf + strftime(buf, len, fmt, localtime(tp)); | ||
199 | } | ||
200 | |||
201 | char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp) | ||
202 | { | ||
203 | return strftime_fmt(buf, len, tp, "%H:%M:%S"); | ||
204 | } | ||
205 | |||
206 | char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) | ||
207 | { | ||
208 | return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S"); | ||
209 | } | ||
210 | |||
190 | #if ENABLE_MONOTONIC_SYSCALL | 211 | #if ENABLE_MONOTONIC_SYSCALL |
191 | 212 | ||
192 | #include <sys/syscall.h> | 213 | #include <sys/syscall.h> |
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) | |||
887 | 887 | ||
888 | VERB2 { | 888 | VERB2 { |
889 | tval = tvc.tv_sec; | 889 | tval = tvc.tv_sec; |
890 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval)); | 890 | strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); |
891 | bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec); | 891 | bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec); |
892 | } | 892 | } |
893 | tval = tvn.tv_sec; | 893 | tval = tvn.tv_sec; |
894 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval)); | 894 | strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); |
895 | bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset); | 895 | bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset); |
896 | 896 | ||
897 | /* Correct various fields which contain time-relative values: */ | 897 | /* 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) | |||
378 | bb_perror_msg_and_die("settimeofday"); | 378 | bb_perror_msg_and_die("settimeofday"); |
379 | 379 | ||
380 | tval = tv.tv_sec; | 380 | tval = tv.tv_sec; |
381 | strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval)); | 381 | strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval); |
382 | 382 | ||
383 | bb_error_msg("setting clock to %s (offset %fs)", buf, offset); | 383 | bb_error_msg("setting clock to %s (offset %fs)", buf, offset); |
384 | 384 | ||
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) | |||
69 | printf("\033[H""\033[J"); | 69 | printf("\033[H""\033[J"); |
70 | if (!(opt & 0x2)) { // no -t | 70 | if (!(opt & 0x2)) { // no -t |
71 | const unsigned time_len = sizeof("1234-67-90 23:56:89"); | 71 | const unsigned time_len = sizeof("1234-67-90 23:56:89"); |
72 | time_t t; | ||
73 | 72 | ||
74 | // STDERR_FILENO is procps3 compat: | 73 | // STDERR_FILENO is procps3 compat: |
75 | // "watch ls 2>/dev/null" does not detect tty size | 74 | // "watch ls 2>/dev/null" does not detect tty size |
@@ -79,10 +78,13 @@ int watch_main(int argc UNUSED_PARAM, char **argv) | |||
79 | free(header); | 78 | free(header); |
80 | header = xasprintf("Every %us: %-*s", period, (int)width, cmd); | 79 | header = xasprintf("Every %us: %-*s", period, (int)width, cmd); |
81 | } | 80 | } |
82 | time(&t); | 81 | if (time_len < width) { |
83 | if (time_len < width) | 82 | strftime_YYYYMMDDHHMMSS( |
84 | strftime(header + width - time_len, time_len, | 83 | header + width - time_len, |
85 | "%Y-%m-%d %H:%M:%S", localtime(&t)); | 84 | time_len, |
85 | /*time_t*:*/ NULL | ||
86 | ); | ||
87 | } | ||
86 | 88 | ||
87 | // compat: empty line between header and cmd output | 89 | // compat: empty line between header and cmd output |
88 | printf("%s\n\n", header); | 90 | printf("%s\n\n", header); |