summaryrefslogtreecommitdiff
path: root/libbb/time.c
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/time.c
parent83e20cb81ca6d22a1ca268a0a64523b5af67325a (diff)
downloadbusybox-9fe1548bbfde548d54acaab113656a56ea0ccc72.tar.gz
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/time.c')
-rw-r--r--libbb/time.c35
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 @@
*/
#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)