diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Config.src | 11 | ||||
-rw-r--r-- | libbb/time.c | 78 |
2 files changed, 76 insertions, 13 deletions
diff --git a/libbb/Config.src b/libbb/Config.src index f97de8ef7..58c5fad50 100644 --- a/libbb/Config.src +++ b/libbb/Config.src | |||
@@ -395,3 +395,14 @@ config FEATURE_HWIB | |||
395 | default y | 395 | default y |
396 | help | 396 | help |
397 | Support for printing infiniband addresses in network applets. | 397 | Support for printing infiniband addresses in network applets. |
398 | |||
399 | config FEATURE_TIMEZONE | ||
400 | bool "Allow timezone in dates" | ||
401 | default y | ||
402 | depends on DESKTOP | ||
403 | help | ||
404 | Permit the use of timezones when parsing user-provided data | ||
405 | strings, e.g. '1996-04-09 12:45:00 -0500'. | ||
406 | |||
407 | This requires support for the '%z' extension to strptime() which | ||
408 | may not be available in all implementations. | ||
diff --git a/libbb/time.c b/libbb/time.c index cf5f2e5c8..41a69c754 100644 --- a/libbb/time.c +++ b/libbb/time.c | |||
@@ -8,16 +8,74 @@ | |||
8 | */ | 8 | */ |
9 | #include "libbb.h" | 9 | #include "libbb.h" |
10 | 10 | ||
11 | void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | 11 | /* Returns 0 if the time structure contains an absolute UTC time which |
12 | * should not be subject to DST adjustment by the caller. */ | ||
13 | int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | ||
12 | { | 14 | { |
13 | char end = '\0'; | 15 | char end = '\0'; |
16 | #if ENABLE_DESKTOP | ||
17 | /* | ||
18 | * strptime is BIG: ~1k in uclibc, ~10k in glibc | ||
19 | * We need it for 'month_name d HH:MM:SS YYYY', supported by GNU date, | ||
20 | * but if we've linked it we might as well use it for everything. | ||
21 | */ | ||
22 | static const char fmt_str[] ALIGN1 = | ||
23 | "%R" "\0" /* HH:MM */ | ||
24 | "%T" "\0" /* HH:MM:SS */ | ||
25 | "%m.%d-%R" "\0" /* mm.dd-HH:MM */ | ||
26 | "%m.%d-%T" "\0" /* mm.dd-HH:MM:SS */ | ||
27 | "%Y.%m.%d-%R" "\0" /* yyyy.mm.dd-HH:MM */ | ||
28 | "%Y.%m.%d-%T" "\0" /* yyyy.mm.dd-HH:MM:SS */ | ||
29 | "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */ | ||
30 | "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */ | ||
31 | "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */ | ||
32 | #if ENABLE_FEATURE_TIMEZONE | ||
33 | "%Y-%m-%d %R %z" "\0" /* yyyy-mm-dd HH:MM TZ */ | ||
34 | "%Y-%m-%d %T %z" "\0" /* yyyy-mm-dd HH:MM:SS TZ */ | ||
35 | #endif | ||
36 | "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */ | ||
37 | "%Y-%m-%d" "\0" /* yyyy-mm-dd */ | ||
38 | /* extra NUL */; | ||
39 | struct tm save; | ||
40 | const char *fmt; | ||
41 | char *endp; | ||
42 | |||
43 | save = *ptm; | ||
44 | fmt = fmt_str; | ||
45 | while (*fmt) { | ||
46 | endp = strptime(date_str, fmt, ptm); | ||
47 | if (endp && *endp == '\0') { | ||
48 | #if ENABLE_FEATURE_TIMEZONE | ||
49 | if (strchr(fmt, 'z')) { | ||
50 | time_t t; | ||
51 | struct tm *utm; | ||
52 | |||
53 | /* we have timezone offset: obtain Unix time_t */ | ||
54 | ptm->tm_sec -= ptm->tm_gmtoff; | ||
55 | ptm->tm_isdst = 0; | ||
56 | t = timegm(ptm); | ||
57 | if (t == (time_t)-1) | ||
58 | break; | ||
59 | /* convert Unix time_t to struct tm in user's locale */ | ||
60 | utm = localtime(&t); | ||
61 | if (!utm) | ||
62 | break; | ||
63 | *ptm = *utm; | ||
64 | return 0; | ||
65 | } | ||
66 | #endif | ||
67 | return 1; | ||
68 | } | ||
69 | *ptm = save; | ||
70 | while (*++fmt) | ||
71 | continue; | ||
72 | ++fmt; | ||
73 | } | ||
74 | #else | ||
14 | const char *last_colon = strrchr(date_str, ':'); | 75 | const char *last_colon = strrchr(date_str, ':'); |
15 | 76 | ||
16 | if (last_colon != NULL) { | 77 | if (last_colon != NULL) { |
17 | /* Parse input and assign appropriately to ptm */ | 78 | /* Parse input and assign appropriately to ptm */ |
18 | #if ENABLE_DESKTOP | ||
19 | const char *endp; | ||
20 | #endif | ||
21 | 79 | ||
22 | /* HH:MM */ | 80 | /* HH:MM */ |
23 | if (sscanf(date_str, "%u:%u%c", | 81 | if (sscanf(date_str, "%u:%u%c", |
@@ -50,14 +108,6 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
50 | ptm->tm_year -= 1900; /* Adjust years */ | 108 | ptm->tm_year -= 1900; /* Adjust years */ |
51 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ | 109 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
52 | } else | 110 | } 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 | { | 111 | { |
62 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 112 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
63 | } | 113 | } |
@@ -89,6 +139,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
89 | ptm->tm_year -= 1900; /* Adjust years */ | 139 | ptm->tm_year -= 1900; /* Adjust years */ |
90 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ | 140 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
91 | } else | 141 | } else |
142 | #endif /* ENABLE_DESKTOP */ | ||
92 | if (date_str[0] == '@') { | 143 | if (date_str[0] == '@') { |
93 | time_t t; | 144 | time_t t; |
94 | if (sizeof(t) <= sizeof(long)) | 145 | if (sizeof(t) <= sizeof(long)) |
@@ -99,7 +150,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
99 | struct tm *lt = localtime(&t); | 150 | struct tm *lt = localtime(&t); |
100 | if (lt) { | 151 | if (lt) { |
101 | *ptm = *lt; | 152 | *ptm = *lt; |
102 | return; | 153 | return 0; |
103 | } | 154 | } |
104 | } | 155 | } |
105 | end = '1'; | 156 | end = '1'; |
@@ -216,6 +267,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
216 | if (end != '\0') { | 267 | if (end != '\0') { |
217 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 268 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
218 | } | 269 | } |
270 | return 1; | ||
219 | } | 271 | } |
220 | 272 | ||
221 | time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) | 273 | time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) |