aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbb/time.c47
1 files changed, 36 insertions, 11 deletions
diff --git a/libbb/time.c b/libbb/time.c
index cf5f2e5c8..365b1df02 100644
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -11,13 +11,45 @@
11void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) 11void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
12{ 12{
13 char end = '\0'; 13 char end = '\0';
14#if ENABLE_DESKTOP
15/*
16 * strptime is BIG: ~1k in uclibc, ~10k in glibc
17 * We need it for 'month_name d HH:MM:SS YYYY', supported by GNU date,
18 * but if we've linked it we might as well use it for everything.
19 */
20 static const char fmt_str[] ALIGN1 =
21 "%R" "\0" /* HH:MM */
22 "%T" "\0" /* HH:MM:SS */
23 "%m.%d-%R" "\0" /* mm.dd-HH:MM */
24 "%m.%d-%T" "\0" /* mm.dd-HH:MM:SS */
25 "%Y.%m.%d-%R" "\0" /* yyyy.mm.dd-HH:MM */
26 "%Y.%m.%d-%T" "\0" /* yyyy.mm.dd-HH:MM:SS */
27 "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */
28 "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */
29 "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */
30 "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */
31 "%Y-%m-%d" "\0" /* yyyy-mm-dd */
32 /* extra NUL */;
33 struct tm save;
34 const char *fmt;
35 char *endp;
36
37 save = *ptm;
38 fmt = fmt_str;
39 while (*fmt) {
40 endp = strptime(date_str, fmt, ptm);
41 if (endp && *endp == '\0')
42 return;
43 *ptm = save;
44 while (*++fmt)
45 continue;
46 ++fmt;
47 }
48#else
14 const char *last_colon = strrchr(date_str, ':'); 49 const char *last_colon = strrchr(date_str, ':');
15 50
16 if (last_colon != NULL) { 51 if (last_colon != NULL) {
17 /* Parse input and assign appropriately to ptm */ 52 /* Parse input and assign appropriately to ptm */
18#if ENABLE_DESKTOP
19 const char *endp;
20#endif
21 53
22 /* HH:MM */ 54 /* HH:MM */
23 if (sscanf(date_str, "%u:%u%c", 55 if (sscanf(date_str, "%u:%u%c",
@@ -50,14 +82,6 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
50 ptm->tm_year -= 1900; /* Adjust years */ 82 ptm->tm_year -= 1900; /* Adjust years */
51 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ 83 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
52 } else 84 } else
53#if ENABLE_DESKTOP /* strptime is BIG: ~1k in uclibc, ~10k in glibc */
54 /* month_name d HH:MM:SS YYYY. Supported by GNU date */
55 if ((endp = strptime(date_str, "%b %d %T %Y", ptm)) != NULL
56 && *endp == '\0'
57 ) {
58 return; /* don't fall through to end == ":" check */
59 } else
60#endif
61 { 85 {
62 bb_error_msg_and_die(bb_msg_invalid_date, date_str); 86 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
63 } 87 }
@@ -89,6 +113,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
89 ptm->tm_year -= 1900; /* Adjust years */ 113 ptm->tm_year -= 1900; /* Adjust years */
90 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ 114 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
91 } else 115 } else
116#endif /* ENABLE_DESKTOP */
92 if (date_str[0] == '@') { 117 if (date_str[0] == '@') {
93 time_t t; 118 time_t t;
94 if (sizeof(t) <= sizeof(long)) 119 if (sizeof(t) <= sizeof(long))