diff options
Diffstat (limited to 'libbb/time.c')
-rw-r--r-- | libbb/time.c | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/libbb/time.c b/libbb/time.c index 365b1df02..41a69c754 100644 --- a/libbb/time.c +++ b/libbb/time.c | |||
@@ -8,7 +8,9 @@ | |||
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'; |
14 | #if ENABLE_DESKTOP | 16 | #if ENABLE_DESKTOP |
@@ -27,6 +29,10 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
27 | "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */ | 29 | "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */ |
28 | "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */ | 30 | "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */ |
29 | "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */ | 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 | ||
30 | "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */ | 36 | "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */ |
31 | "%Y-%m-%d" "\0" /* yyyy-mm-dd */ | 37 | "%Y-%m-%d" "\0" /* yyyy-mm-dd */ |
32 | /* extra NUL */; | 38 | /* extra NUL */; |
@@ -38,8 +44,28 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
38 | fmt = fmt_str; | 44 | fmt = fmt_str; |
39 | while (*fmt) { | 45 | while (*fmt) { |
40 | endp = strptime(date_str, fmt, ptm); | 46 | endp = strptime(date_str, fmt, ptm); |
41 | if (endp && *endp == '\0') | 47 | if (endp && *endp == '\0') { |
42 | return; | 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 | } | ||
43 | *ptm = save; | 69 | *ptm = save; |
44 | while (*++fmt) | 70 | while (*++fmt) |
45 | continue; | 71 | continue; |
@@ -124,7 +150,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
124 | struct tm *lt = localtime(&t); | 150 | struct tm *lt = localtime(&t); |
125 | if (lt) { | 151 | if (lt) { |
126 | *ptm = *lt; | 152 | *ptm = *lt; |
127 | return; | 153 | return 0; |
128 | } | 154 | } |
129 | } | 155 | } |
130 | end = '1'; | 156 | end = '1'; |
@@ -241,6 +267,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) | |||
241 | if (end != '\0') { | 267 | if (end != '\0') { |
242 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 268 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
243 | } | 269 | } |
270 | return 1; | ||
244 | } | 271 | } |
245 | 272 | ||
246 | 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) |