aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2021-09-16 10:26:14 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2021-09-17 00:11:30 +0200
commit9fe1548bbfde548d54acaab113656a56ea0ccc72 (patch)
treebabe79b4f502417ca91a8fab3099f29ff718de19 /libbb
parent83e20cb81ca6d22a1ca268a0a64523b5af67325a (diff)
downloadbusybox-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>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Config.src11
-rw-r--r--libbb/time.c35
2 files changed, 42 insertions, 4 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
399config 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
11void 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. */
13int 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
246time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) 273time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)