diff options
| author | Ron Yorston <rmy@pobox.com> | 2021-09-16 10:26:14 +0100 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-09-17 00:11:30 +0200 |
| commit | 9fe1548bbfde548d54acaab113656a56ea0ccc72 (patch) | |
| tree | babe79b4f502417ca91a8fab3099f29ff718de19 | |
| parent | 83e20cb81ca6d22a1ca268a0a64523b5af67325a (diff) | |
| download | busybox-w32-9fe1548bbfde548d54acaab113656a56ea0ccc72.tar.gz busybox-w32-9fe1548bbfde548d54acaab113656a56ea0ccc72.tar.bz2 busybox-w32-9fe1548bbfde548d54acaab113656a56ea0ccc72.zip | |
date,touch: allow timezone offsets in dates
Allow ISO 8601 style dates to include a timezone offset. Like
the '@' format these dates aren't relative to the user's current
timezone and shouldn't be subject to DST adjustment.
- The implementation uses the strptime() '%z' format specifier.
This an extension which may not be available so the use of
timezones is a configuration option.
- The 'touch' applet has been updated to respect whether DST
adjustment is required, matching 'date'.
function old new delta
parse_datestr 624 730 +106
static.fmt_str 106 136 +30
touch_main 388 392 +4
date_main 818 819 +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 141/0) Total: 141 bytes
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
| -rw-r--r-- | coreutils/date.c | 7 | ||||
| -rw-r--r-- | coreutils/touch.c | 6 | ||||
| -rw-r--r-- | include/libbb.h | 2 | ||||
| -rw-r--r-- | libbb/Config.src | 11 | ||||
| -rw-r--r-- | libbb/time.c | 35 | ||||
| -rw-r--r-- | testsuite/date/date-timezone | 32 |
6 files changed, 83 insertions, 10 deletions
diff --git a/coreutils/date.c b/coreutils/date.c index 7061f1719..abcc37c33 100644 --- a/coreutils/date.c +++ b/coreutils/date.c | |||
| @@ -266,6 +266,7 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
| 266 | 266 | ||
| 267 | /* If date string is given, update tm_time, and maybe set date */ | 267 | /* If date string is given, update tm_time, and maybe set date */ |
| 268 | if (date_str != NULL) { | 268 | if (date_str != NULL) { |
| 269 | int check_dst = 1; | ||
| 269 | /* Zero out fields - take her back to midnight! */ | 270 | /* Zero out fields - take her back to midnight! */ |
| 270 | tm_time.tm_sec = 0; | 271 | tm_time.tm_sec = 0; |
| 271 | tm_time.tm_min = 0; | 272 | tm_time.tm_min = 0; |
| @@ -276,12 +277,12 @@ int date_main(int argc UNUSED_PARAM, char **argv) | |||
| 276 | if (strptime(date_str, fmt_str2dt, &tm_time) == NULL) | 277 | if (strptime(date_str, fmt_str2dt, &tm_time) == NULL) |
| 277 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); | 278 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 278 | } else { | 279 | } else { |
| 279 | parse_datestr(date_str, &tm_time); | 280 | check_dst = parse_datestr(date_str, &tm_time); |
| 280 | } | 281 | } |
| 281 | 282 | ||
| 282 | /* Correct any day of week and day of year etc. fields */ | 283 | /* Correct any day of week and day of year etc. fields */ |
| 283 | /* Be sure to recheck dst (but not if date is time_t format) */ | 284 | /* Be sure to recheck dst (but not if date is UTC) */ |
| 284 | if (date_str[0] != '@') | 285 | if (check_dst) |
| 285 | tm_time.tm_isdst = -1; | 286 | tm_time.tm_isdst = -1; |
| 286 | ts.tv_sec = validate_tm_time(date_str, &tm_time); | 287 | ts.tv_sec = validate_tm_time(date_str, &tm_time); |
| 287 | ts.tv_nsec = 0; | 288 | ts.tv_nsec = 0; |
diff --git a/coreutils/touch.c b/coreutils/touch.c index 78100ba1d..7e13a27be 100644 --- a/coreutils/touch.c +++ b/coreutils/touch.c | |||
| @@ -140,15 +140,17 @@ int touch_main(int argc UNUSED_PARAM, char **argv) | |||
| 140 | if (opts & (OPT_d|OPT_t)) { | 140 | if (opts & (OPT_d|OPT_t)) { |
| 141 | struct tm tm_time; | 141 | struct tm tm_time; |
| 142 | time_t t; | 142 | time_t t; |
| 143 | int check_dst; | ||
| 143 | 144 | ||
| 144 | //memset(&tm_time, 0, sizeof(tm_time)); | 145 | //memset(&tm_time, 0, sizeof(tm_time)); |
| 145 | /* Better than memset: makes "HH:MM" dates meaningful */ | 146 | /* Better than memset: makes "HH:MM" dates meaningful */ |
| 146 | time(&t); | 147 | time(&t); |
| 147 | localtime_r(&t, &tm_time); | 148 | localtime_r(&t, &tm_time); |
| 148 | parse_datestr(date_str, &tm_time); | 149 | check_dst = parse_datestr(date_str, &tm_time); |
| 149 | 150 | ||
| 150 | /* Correct any day of week and day of year etc. fields */ | 151 | /* Correct any day of week and day of year etc. fields */ |
| 151 | tm_time.tm_isdst = -1; /* Be sure to recheck dst */ | 152 | if (check_dst) |
| 153 | tm_time.tm_isdst = -1; /* recheck dst unless date is UTC */ | ||
| 152 | t = validate_tm_time(date_str, &tm_time); | 154 | t = validate_tm_time(date_str, &tm_time); |
| 153 | 155 | ||
| 154 | timebuf[1].tv_sec = timebuf[0].tv_sec = t; | 156 | timebuf[1].tv_sec = timebuf[0].tv_sec = t; |
diff --git a/include/libbb.h b/include/libbb.h index 7d6ab4a93..1ec8d2d3b 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
| @@ -690,7 +690,7 @@ struct BUG_too_small { | |||
| 690 | }; | 690 | }; |
| 691 | 691 | ||
| 692 | 692 | ||
| 693 | void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; | 693 | int parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC; |
| 694 | time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; | 694 | time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; |
| 695 | char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; | 695 | char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; |
| 696 | char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; | 696 | char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; |
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 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) |
diff --git a/testsuite/date/date-timezone b/testsuite/date/date-timezone new file mode 100644 index 000000000..8628aa1d7 --- /dev/null +++ b/testsuite/date/date-timezone | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | # FEATURE: CONFIG_FEATURE_TIMEZONE | ||
| 2 | |||
| 3 | # 'Z' is UTC | ||
| 4 | dt=$(TZ=UTC0 busybox date -d '1999-1-2 3:4:5Z') | ||
| 5 | dt=$(echo "$dt" | cut -b1-19) | ||
| 6 | test x"$dt" = x"Sat Jan 2 03:04:05" | ||
| 7 | |||
| 8 | # '+0600' is six hours ahead of UTC | ||
| 9 | dt=$(TZ=UTC0 busybox date -d '1999-1-2 3:4:5 +0600') | ||
| 10 | dt=$(echo "$dt" | cut -b1-19) | ||
| 11 | test x"$dt" = x"Fri Jan 1 21:04:05" | ||
| 12 | |||
| 13 | # '-0600' is six hours behind UTC | ||
| 14 | dt=$(TZ=UTC0 busybox date -d '1999-1-2 3:4:5 -0600') | ||
| 15 | dt=$(echo "$dt" | cut -b1-19) | ||
| 16 | test x"$dt" = x"Sat Jan 2 09:04:05" | ||
| 17 | |||
| 18 | # before dst is switched on | ||
| 19 | dt=$(TZ=GMT0BST,M3.5.0/1,M10.5.0/2 busybox date -d '2021-03-28 00:59:59 +0000') | ||
| 20 | test x"$dt" = x"Sun Mar 28 00:59:59 GMT 2021" | ||
| 21 | |||
| 22 | # after dst is switched on | ||
| 23 | dt=$(TZ=GMT0BST,M3.5.0/1,M10.5.0/2 busybox date -d '2021-03-28 01:00:01 +0000') | ||
| 24 | test x"$dt" = x"Sun Mar 28 02:00:01 BST 2021" | ||
| 25 | |||
| 26 | # before dst is switched off | ||
| 27 | dt=$(TZ=GMT0BST,M3.5.0/1,M10.5.0/2 busybox date -d '2021-10-31 00:00:01 +0000') | ||
| 28 | test x"$dt" = x"Sun Oct 31 01:00:01 BST 2021" | ||
| 29 | |||
| 30 | # after dst is switched off: back to 01:00:01 but with different TZ | ||
| 31 | dt=$(TZ=GMT0BST,M3.5.0/1,M10.5.0/2 busybox date -d '2021-10-31 01:00:01 +0000') | ||
| 32 | test x"$dt" = x"Sun Oct 31 01:00:01 GMT 2021" | ||
