blob: f09ef5d52782fa3b5d58f9f0e9921edfc6a545e1 [file] [log] [blame]
Denis Vlasenko459be352007-06-17 19:09:05 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko
Denis Vlasenko459be352007-06-17 19:09:05 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko459be352007-06-17 19:09:05 +00008 */
Denis Vlasenko459be352007-06-17 19:09:05 +00009#include "libbb.h"
10
Ron Yorston9fe15482021-09-16 10:26:14 +010011/* 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)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020014{
15 char end = '\0';
Ron Yorstonf27a6a92021-09-18 08:40:44 +010016 time_t t;
Ron Yorston3512ef82021-09-14 08:52:49 +010017#if ENABLE_DESKTOP
18/*
19 * strptime is BIG: ~1k in uclibc, ~10k in glibc
20 * We need it for 'month_name d HH:MM:SS YYYY', supported by GNU date,
21 * but if we've linked it we might as well use it for everything.
22 */
23 static const char fmt_str[] ALIGN1 =
24 "%R" "\0" /* HH:MM */
25 "%T" "\0" /* HH:MM:SS */
26 "%m.%d-%R" "\0" /* mm.dd-HH:MM */
27 "%m.%d-%T" "\0" /* mm.dd-HH:MM:SS */
28 "%Y.%m.%d-%R" "\0" /* yyyy.mm.dd-HH:MM */
29 "%Y.%m.%d-%T" "\0" /* yyyy.mm.dd-HH:MM:SS */
30 "%b %d %T %Y" "\0" /* month_name d HH:MM:SS YYYY */
31 "%Y-%m-%d %R" "\0" /* yyyy-mm-dd HH:MM */
32 "%Y-%m-%d %T" "\0" /* yyyy-mm-dd HH:MM:SS */
Ron Yorstonf27a6a92021-09-18 08:40:44 +010033# if ENABLE_FEATURE_TIMEZONE
Ron Yorston9fe15482021-09-16 10:26:14 +010034 "%Y-%m-%d %R %z" "\0" /* yyyy-mm-dd HH:MM TZ */
35 "%Y-%m-%d %T %z" "\0" /* yyyy-mm-dd HH:MM:SS TZ */
Ron Yorstonf27a6a92021-09-18 08:40:44 +010036# endif
Ron Yorston3512ef82021-09-14 08:52:49 +010037 "%Y-%m-%d %H" "\0" /* yyyy-mm-dd HH */
38 "%Y-%m-%d" "\0" /* yyyy-mm-dd */
39 /* extra NUL */;
40 struct tm save;
41 const char *fmt;
42 char *endp;
43
44 save = *ptm;
45 fmt = fmt_str;
46 while (*fmt) {
47 endp = strptime(date_str, fmt, ptm);
Ron Yorston9fe15482021-09-16 10:26:14 +010048 if (endp && *endp == '\0') {
Ron Yorstonf27a6a92021-09-18 08:40:44 +010049# if ENABLE_FEATURE_TIMEZONE
Ron Yorston9fe15482021-09-16 10:26:14 +010050 if (strchr(fmt, 'z')) {
Ron Yorston9fe15482021-09-16 10:26:14 +010051 /* we have timezone offset: obtain Unix time_t */
52 ptm->tm_sec -= ptm->tm_gmtoff;
53 ptm->tm_isdst = 0;
54 t = timegm(ptm);
55 if (t == (time_t)-1)
56 break;
57 /* convert Unix time_t to struct tm in user's locale */
Ron Yorstonf27a6a92021-09-18 08:40:44 +010058 goto localise;
Ron Yorston9fe15482021-09-16 10:26:14 +010059 }
Ron Yorstonf27a6a92021-09-18 08:40:44 +010060# endif
Ron Yorston9fe15482021-09-16 10:26:14 +010061 return 1;
62 }
Ron Yorston3512ef82021-09-14 08:52:49 +010063 *ptm = save;
64 while (*++fmt)
65 continue;
66 ++fmt;
67 }
68#else
Denys Vlasenko73b71f32009-07-18 03:40:35 +020069 const char *last_colon = strrchr(date_str, ':');
70
71 if (last_colon != NULL) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010072 /* Parse input and assign appropriately to ptm */
Denys Vlasenko73b71f32009-07-18 03:40:35 +020073
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020074 /* HH:MM */
Denys Vlasenko73b71f32009-07-18 03:40:35 +020075 if (sscanf(date_str, "%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010076 &ptm->tm_hour,
77 &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020078 &end) >= 2
79 ) {
Denys Vlasenko73b71f32009-07-18 03:40:35 +020080 /* no adjustments needed */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020081 } else
82 /* mm.dd-HH:MM */
83 if (sscanf(date_str, "%u.%u-%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010084 &ptm->tm_mon, &ptm->tm_mday,
85 &ptm->tm_hour, &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020086 &end) >= 4
87 ) {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020088 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010089 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020090 } else
91 /* yyyy.mm.dd-HH:MM */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010092 if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
93 &ptm->tm_mon, &ptm->tm_mday,
94 &ptm->tm_hour, &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020095 &end) >= 5
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020096 /* yyyy-mm-dd HH:MM */
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020097 || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010098 &ptm->tm_mon, &ptm->tm_mday,
99 &ptm->tm_hour, &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +0200100 &end) >= 5
101 ) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100102 ptm->tm_year -= 1900; /* Adjust years */
103 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Alexander Shishkin85dbf192010-03-22 18:22:33 +0100104 } else
Alexander Shishkin85dbf192010-03-22 18:22:33 +0100105 {
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200106 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
107 }
108 if (end == ':') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200109 /* xxx:SS */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100110 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200111 end = '\0';
112 /* else end != NUL and we error out */
113 }
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +0200114 } else
Denys Vlasenko3b394782014-01-21 07:58:18 +0100115 if (strchr(date_str, '-')
116 /* Why strchr('-') check?
117 * sscanf below will trash ptm->tm_year, this breaks
118 * if parse_str is "10101010" (iow, "MMddhhmm" form)
119 * because we destroy year. Do these sscanf
120 * only if we saw a dash in parse_str.
121 */
122 /* yyyy-mm-dd HH */
123 && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +0200124 &ptm->tm_mon, &ptm->tm_mday,
125 &ptm->tm_hour,
126 &end) >= 4
Denys Vlasenko3b394782014-01-21 07:58:18 +0100127 /* yyyy-mm-dd */
128 || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +0200129 &ptm->tm_mon, &ptm->tm_mday,
130 &end) >= 3
Denys Vlasenko3b394782014-01-21 07:58:18 +0100131 )
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +0200132 ) {
133 ptm->tm_year -= 1900; /* Adjust years */
134 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
135 } else
Ron Yorston3512ef82021-09-14 08:52:49 +0100136#endif /* ENABLE_DESKTOP */
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +0200137 if (date_str[0] == '@') {
Denys Vlasenko32a8f702020-11-28 23:21:13 +0100138 if (sizeof(t) <= sizeof(long))
139 t = bb_strtol(date_str + 1, NULL, 10);
140 else /* time_t is 64 bits but longs are smaller */
141 t = bb_strtoll(date_str + 1, NULL, 10);
Denys Vlasenko5f943462010-04-22 00:45:28 -0400142 if (!errno) {
Ron Yorstonf27a6a92021-09-18 08:40:44 +0100143 struct tm *lt;
144 IF_FEATURE_TIMEZONE(localise:)
145 lt = localtime(&t);
Denys Vlasenko5f943462010-04-22 00:45:28 -0400146 if (lt) {
147 *ptm = *lt;
Ron Yorston9fe15482021-09-16 10:26:14 +0100148 return 0;
Denys Vlasenko5f943462010-04-22 00:45:28 -0400149 }
150 }
151 end = '1';
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200152 } else {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200153 /* Googled the following on an old date manpage:
154 *
155 * The canonical representation for setting the date/time is:
156 * cc Century (either 19 or 20)
157 * yy Year in abbreviated form (e.g. 89, 06)
158 * mm Numeric month, a number from 1 to 12
159 * dd Day, a number from 1 to 31
160 * HH Hour, a number from 0 to 23
161 * MM Minutes, a number from 0 to 59
Denys Vlasenko4b624d02009-09-02 11:49:25 +0200162 * .SS Seconds, a number from 0 to 61 (with leap seconds)
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200163 * Everything but the minutes is optional
164 *
Denys Vlasenko10ee20b2011-01-17 14:23:42 +0100165 * "touch -t DATETIME" format: [[[[[YY]YY]MM]DD]hh]mm[.ss]
166 * Some, but not all, Unix "date DATETIME" commands
167 * move [[YY]YY] past minutes mm field (!).
168 * Coreutils date does it, and SUS mandates it.
169 * (date -s DATETIME does not support this format. lovely!)
170 * In bbox, this format is special-cased in date applet
171 * (IOW: this function assumes "touch -t" format).
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200172 */
Denys Vlasenko92ffe052011-01-02 20:02:09 +0100173 unsigned cur_year = ptm->tm_year;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200174 int len = strchrnul(date_str, '.') - date_str;
175
176 /* MM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100177 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100178 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200179 &end) >= 1) {
180 } else
181 /* HHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100182 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100183 &ptm->tm_hour,
184 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200185 &end) >= 2) {
186 } else
187 /* ddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100188 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100189 &ptm->tm_mday,
190 &ptm->tm_hour,
191 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200192 &end) >= 3) {
193 } else
194 /* mmddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100195 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100196 &ptm->tm_mon,
197 &ptm->tm_mday,
198 &ptm->tm_hour,
199 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200200 &end) >= 4) {
201 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100202 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200203 } else
204 /* yymmddHHMM[.SS] */
205 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100206 &ptm->tm_year,
207 &ptm->tm_mon,
208 &ptm->tm_mday,
209 &ptm->tm_hour,
210 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200211 &end) >= 5) {
212 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100213 ptm->tm_mon -= 1;
Denys Vlasenko92ffe052011-01-02 20:02:09 +0100214 if ((int)cur_year >= 50) { /* >= 1950 */
215 /* Adjust year: */
216 /* 1. Put it in the current century */
217 ptm->tm_year += (cur_year / 100) * 100;
218 /* 2. If too far in the past, +100 years */
219 if (ptm->tm_year < cur_year - 50)
220 ptm->tm_year += 100;
221 /* 3. If too far in the future, -100 years */
222 if (ptm->tm_year > cur_year + 50)
223 ptm->tm_year -= 100;
224 }
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200225 } else
Denys Vlasenko4b624d02009-09-02 11:49:25 +0200226 /* ccyymmddHHMM[.SS] */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200227 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100228 &ptm->tm_year,
229 &ptm->tm_mon,
230 &ptm->tm_mday,
231 &ptm->tm_hour,
232 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200233 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100234 ptm->tm_year -= 1900; /* Adjust years */
235 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200236 } else {
Denys Vlasenko76832ff2018-09-23 20:27:32 +0200237 err:
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200238 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200239 }
Natanael Copab684d1b2016-05-19 17:31:59 +0200240 ptm->tm_sec = 0; /* assume zero if [.SS] is not given */
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200241 if (end == '.') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200242 /* xxx.SS */
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200243 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100244 &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200245 end = '\0';
246 /* else end != NUL and we error out */
247 }
Denys Vlasenko76832ff2018-09-23 20:27:32 +0200248 /* Users were confused by "date -s 20180923"
249 * working (not in the way they were expecting).
250 * It was interpreted as MMDDhhmm, and not bothered by
251 * "month #20" in the least. Prevent such cases:
252 */
253 if (ptm->tm_sec > 60 /* allow "23:60" leap second */
254 || ptm->tm_min > 59
255 || ptm->tm_hour > 23
256 || ptm->tm_mday > 31
257 || ptm->tm_mon > 11 /* month# is 0..11, not 1..12 */
258 ) {
259 goto err;
260 }
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200261 }
262 if (end != '\0') {
263 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
264 }
Ron Yorston9fe15482021-09-16 10:26:14 +0100265 return 1;
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200266}
267
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100268time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200269{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100270 time_t t = mktime(ptm);
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200271 if (t == (time_t) -1L) {
272 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
273 }
274 return t;
275}
276
Denys Vlasenko8f2cb7a2013-03-29 12:30:33 +0100277static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt)
278{
279 time_t t;
280 if (!tp) {
281 tp = &t;
282 time(tp);
283 }
284 /* Returns pointer to NUL */
285 return buf + strftime(buf, len, fmt, localtime(tp));
286}
287
288char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp)
289{
290 return strftime_fmt(buf, len, tp, "%H:%M:%S");
291}
292
293char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
294{
295 return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S");
296}
297
Denis Vlasenko459be352007-06-17 19:09:05 +0000298#if ENABLE_MONOTONIC_SYSCALL
Denis Vlasenko459be352007-06-17 19:09:05 +0000299
Denis Vlasenko09c0a742008-06-07 23:43:43 +0000300/* Old glibc (< 2.3.4) does not provide this constant. We use syscall
301 * directly so this definition is safe. */
302#ifndef CLOCK_MONOTONIC
303#define CLOCK_MONOTONIC 1
304#endif
305
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000306static void get_mono(struct timespec *ts)
307{
Denys Vlasenkobe5a5052019-10-24 16:26:55 +0200308 if (clock_gettime(CLOCK_MONOTONIC, ts))
James Byrne69374872019-07-02 11:35:03 +0200309 bb_simple_error_msg_and_die("clock_gettime(MONOTONIC) failed");
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000310}
311unsigned long long FAST_FUNC monotonic_ns(void)
312{
313 struct timespec ts;
314 get_mono(&ts);
315 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
316}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000317unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000318{
319 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000320 get_mono(&ts);
Denis Vlasenko459be352007-06-17 19:09:05 +0000321 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
322}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100323unsigned long long FAST_FUNC monotonic_ms(void)
324{
325 struct timespec ts;
326 get_mono(&ts);
327 return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
328}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000329unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000330{
331 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000332 get_mono(&ts);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000333 return ts.tv_sec;
334}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000335
Denis Vlasenko459be352007-06-17 19:09:05 +0000336#else
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000337
338unsigned long long FAST_FUNC monotonic_ns(void)
339{
340 struct timeval tv;
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100341 xgettimeofday(&tv);
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000342 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
343}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000344unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000345{
346 struct timeval tv;
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100347 xgettimeofday(&tv);
Mike Frysingerebd27aa2007-06-18 07:12:31 +0000348 return tv.tv_sec * 1000000ULL + tv.tv_usec;
Denis Vlasenko459be352007-06-17 19:09:05 +0000349}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100350unsigned long long FAST_FUNC monotonic_ms(void)
351{
352 struct timeval tv;
Denys Vlasenko3c13da32020-12-30 23:48:01 +0100353 xgettimeofday(&tv);
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100354 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
355}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000356unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000357{
358 return time(NULL);
359}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000360
Denis Vlasenko459be352007-06-17 19:09:05 +0000361#endif