diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Config.src | 11 | ||||
-rw-r--r-- | libbb/time.c | 35 |
2 files changed, 42 insertions, 4 deletions
diff --git a/libbb/Config.src b/libbb/Config.src index f97de8e..58c5fad 100644 --- a/libbb/Config.src +++ b/libbb/Config.src @@ -395,3 +395,14 @@ config FEATURE_HWIB default y help Support for printing infiniband addresses in network applets. + +config FEATURE_TIMEZONE + bool "Allow timezone in dates" + default y + depends on DESKTOP + help + Permit the use of timezones when parsing user-provided data + strings, e.g. '1996-04-09 12:45:00 -0500'. + + This requires support for the '%z' extension to strptime() which + may not be available in all implementations. diff --git a/libbb/time.c b/libbb/time.c index 365b1df..41a69c7 100644 --- a/libbb/time.c +++ b/libbb/time.c @@ -8,7 +8,9 @@ */ #include "libbb.h" -void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) +/* Returns 0 if the time structure contains an absolute UTC time which + * should not be subject to DST adjustment by the caller. */ +int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) { char end = '\0'; #if ENABLE_DESKTOP @@ -27,6 +29,10 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */ "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */ "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */ +#if ENABLE_FEATURE_TIMEZONE + "%Y-%m-%d %R %z" "\0" /* yyyy-mm-dd HH:MM TZ */ + "%Y-%m-%d %T %z" "\0" /* yyyy-mm-dd HH:MM:SS TZ */ +#endif "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */ "%Y-%m-%d" "\0" /* yyyy-mm-dd */ /* extra NUL */; @@ -38,8 +44,28 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) fmt = fmt_str; while (*fmt) { endp = strptime(date_str, fmt, ptm); - if (endp && *endp == '\0') - return; + if (endp && *endp == '\0') { +#if ENABLE_FEATURE_TIMEZONE + if (strchr(fmt, 'z')) { + time_t t; + struct tm *utm; + + /* we have timezone offset: obtain Unix time_t */ + ptm->tm_sec -= ptm->tm_gmtoff; + ptm->tm_isdst = 0; + t = timegm(ptm); + if (t == (time_t)-1) + break; + /* convert Unix time_t to struct tm in user's locale */ + utm = localtime(&t); + if (!utm) + break; + *ptm = *utm; + return 0; + } +#endif + return 1; + } *ptm = save; while (*++fmt) continue; @@ -124,7 +150,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) struct tm *lt = localtime(&t); if (lt) { *ptm = *lt; - return; + return 0; } } end = '1'; @@ -241,6 +267,7 @@ void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) if (end != '\0') { bb_error_msg_and_die(bb_msg_invalid_date, date_str); } + return 1; } time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) |