libbb: introduce and use strftime_[YYYYMMDD]HHMMSS()
No binary (build failed)
—
(log yok)
| @@ -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 | |
| } | |
| @@ -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; | |
| @@ -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; | |
| @@ -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 <sys/syscall.h> | |
| @@ -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: */ | |
| @@ -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); | |
| @@ -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); | |