blob: e66a9cba8438746722ff43e9eecdc9a721095205 [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
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010011void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020012{
13 char end = '\0';
14 const char *last_colon = strrchr(date_str, ':');
15
16 if (last_colon != NULL) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010017 /* Parse input and assign appropriately to ptm */
Alexander Shishkin85dbf192010-03-22 18:22:33 +010018#if ENABLE_DESKTOP
19 const char *endp;
20#endif
Denys Vlasenko73b71f32009-07-18 03:40:35 +020021
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020022 /* HH:MM */
Denys Vlasenko73b71f32009-07-18 03:40:35 +020023 if (sscanf(date_str, "%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010024 &ptm->tm_hour,
25 &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020026 &end) >= 2
27 ) {
Denys Vlasenko73b71f32009-07-18 03:40:35 +020028 /* no adjustments needed */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020029 } else
30 /* mm.dd-HH:MM */
31 if (sscanf(date_str, "%u.%u-%u:%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010032 &ptm->tm_mon, &ptm->tm_mday,
33 &ptm->tm_hour, &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020034 &end) >= 4
35 ) {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020036 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010037 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020038 } else
39 /* yyyy.mm.dd-HH:MM */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010040 if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
41 &ptm->tm_mon, &ptm->tm_mday,
42 &ptm->tm_hour, &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020043 &end) >= 5
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020044 /* yyyy-mm-dd HH:MM */
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020045 || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010046 &ptm->tm_mon, &ptm->tm_mday,
47 &ptm->tm_hour, &ptm->tm_min,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020048 &end) >= 5
49 ) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010050 ptm->tm_year -= 1900; /* Adjust years */
51 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Alexander Shishkin85dbf192010-03-22 18:22:33 +010052 } else
53#if ENABLE_DESKTOP /* strptime is BIG: ~1k in uclibc, ~10k in glibc */
54 /* month_name d HH:MM:SS YYYY. Supported by GNU date */
55 if ((endp = strptime(date_str, "%b %d %T %Y", ptm)) != NULL
56 && *endp == '\0'
57 ) {
58 return; /* don't fall through to end == ":" check */
59 } else
60#endif
Alexander Shishkin85dbf192010-03-22 18:22:33 +010061 {
Denys Vlasenko73b71f32009-07-18 03:40:35 +020062 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
63 }
64 if (end == ':') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +020065 /* xxx:SS */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +010066 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +020067 end = '\0';
68 /* else end != NUL and we error out */
69 }
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020070 } else
Denys Vlasenko3b394782014-01-21 07:58:18 +010071 if (strchr(date_str, '-')
72 /* Why strchr('-') check?
73 * sscanf below will trash ptm->tm_year, this breaks
74 * if parse_str is "10101010" (iow, "MMddhhmm" form)
75 * because we destroy year. Do these sscanf
76 * only if we saw a dash in parse_str.
77 */
78 /* yyyy-mm-dd HH */
79 && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020080 &ptm->tm_mon, &ptm->tm_mday,
81 &ptm->tm_hour,
82 &end) >= 4
Denys Vlasenko3b394782014-01-21 07:58:18 +010083 /* yyyy-mm-dd */
84 || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020085 &ptm->tm_mon, &ptm->tm_mday,
86 &end) >= 3
Denys Vlasenko3b394782014-01-21 07:58:18 +010087 )
Bartosz Golaszewski688a7e32013-07-25 04:59:46 +020088 ) {
89 ptm->tm_year -= 1900; /* Adjust years */
90 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
91 } else
92 if (date_str[0] == '@') {
Denys Vlasenko5f943462010-04-22 00:45:28 -040093 time_t t = bb_strtol(date_str + 1, NULL, 10);
94 if (!errno) {
95 struct tm *lt = localtime(&t);
96 if (lt) {
97 *ptm = *lt;
98 return;
99 }
100 }
101 end = '1';
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200102 } else {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200103 /* Googled the following on an old date manpage:
104 *
105 * The canonical representation for setting the date/time is:
106 * cc Century (either 19 or 20)
107 * yy Year in abbreviated form (e.g. 89, 06)
108 * mm Numeric month, a number from 1 to 12
109 * dd Day, a number from 1 to 31
110 * HH Hour, a number from 0 to 23
111 * MM Minutes, a number from 0 to 59
Denys Vlasenko4b624d02009-09-02 11:49:25 +0200112 * .SS Seconds, a number from 0 to 61 (with leap seconds)
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200113 * Everything but the minutes is optional
114 *
Denys Vlasenko10ee20b2011-01-17 14:23:42 +0100115 * "touch -t DATETIME" format: [[[[[YY]YY]MM]DD]hh]mm[.ss]
116 * Some, but not all, Unix "date DATETIME" commands
117 * move [[YY]YY] past minutes mm field (!).
118 * Coreutils date does it, and SUS mandates it.
119 * (date -s DATETIME does not support this format. lovely!)
120 * In bbox, this format is special-cased in date applet
121 * (IOW: this function assumes "touch -t" format).
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200122 */
Denys Vlasenko92ffe052011-01-02 20:02:09 +0100123 unsigned cur_year = ptm->tm_year;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200124 int len = strchrnul(date_str, '.') - date_str;
125
126 /* MM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100127 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100128 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200129 &end) >= 1) {
130 } else
131 /* HHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100132 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100133 &ptm->tm_hour,
134 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200135 &end) >= 2) {
136 } else
137 /* ddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100138 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100139 &ptm->tm_mday,
140 &ptm->tm_hour,
141 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200142 &end) >= 3) {
143 } else
144 /* mmddHHMM[.SS] */
Denys Vlasenkoff1822a2009-11-15 04:55:40 +0100145 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100146 &ptm->tm_mon,
147 &ptm->tm_mday,
148 &ptm->tm_hour,
149 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200150 &end) >= 4) {
151 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100152 ptm->tm_mon -= 1;
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200153 } else
154 /* yymmddHHMM[.SS] */
155 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100156 &ptm->tm_year,
157 &ptm->tm_mon,
158 &ptm->tm_mday,
159 &ptm->tm_hour,
160 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200161 &end) >= 5) {
162 /* Adjust month from 1-12 to 0-11 */
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100163 ptm->tm_mon -= 1;
Denys Vlasenko92ffe052011-01-02 20:02:09 +0100164 if ((int)cur_year >= 50) { /* >= 1950 */
165 /* Adjust year: */
166 /* 1. Put it in the current century */
167 ptm->tm_year += (cur_year / 100) * 100;
168 /* 2. If too far in the past, +100 years */
169 if (ptm->tm_year < cur_year - 50)
170 ptm->tm_year += 100;
171 /* 3. If too far in the future, -100 years */
172 if (ptm->tm_year > cur_year + 50)
173 ptm->tm_year -= 100;
174 }
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200175 } else
Denys Vlasenko4b624d02009-09-02 11:49:25 +0200176 /* ccyymmddHHMM[.SS] */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200177 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100178 &ptm->tm_year,
179 &ptm->tm_mon,
180 &ptm->tm_mday,
181 &ptm->tm_hour,
182 &ptm->tm_min,
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200183 &end) >= 5) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100184 ptm->tm_year -= 1900; /* Adjust years */
185 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200186 } else {
Denys Vlasenko76832ff2018-09-23 20:27:32 +0200187 err:
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200188 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200189 }
Natanael Copab684d1b2016-05-19 17:31:59 +0200190 ptm->tm_sec = 0; /* assume zero if [.SS] is not given */
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200191 if (end == '.') {
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200192 /* xxx.SS */
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200193 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100194 &ptm->tm_sec, &end) == 1)
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200195 end = '\0';
196 /* else end != NUL and we error out */
197 }
Denys Vlasenko76832ff2018-09-23 20:27:32 +0200198 /* Users were confused by "date -s 20180923"
199 * working (not in the way they were expecting).
200 * It was interpreted as MMDDhhmm, and not bothered by
201 * "month #20" in the least. Prevent such cases:
202 */
203 if (ptm->tm_sec > 60 /* allow "23:60" leap second */
204 || ptm->tm_min > 59
205 || ptm->tm_hour > 23
206 || ptm->tm_mday > 31
207 || ptm->tm_mon > 11 /* month# is 0..11, not 1..12 */
208 ) {
209 goto err;
210 }
Denys Vlasenko73b71f32009-07-18 03:40:35 +0200211 }
212 if (end != '\0') {
213 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
214 }
215}
216
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100217time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200218{
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100219 time_t t = mktime(ptm);
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200220 if (t == (time_t) -1L) {
221 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
222 }
223 return t;
224}
225
Denys Vlasenko8f2cb7a2013-03-29 12:30:33 +0100226static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt)
227{
228 time_t t;
229 if (!tp) {
230 tp = &t;
231 time(tp);
232 }
233 /* Returns pointer to NUL */
234 return buf + strftime(buf, len, fmt, localtime(tp));
235}
236
237char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp)
238{
239 return strftime_fmt(buf, len, tp, "%H:%M:%S");
240}
241
242char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
243{
244 return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S");
245}
246
Denis Vlasenko459be352007-06-17 19:09:05 +0000247#if ENABLE_MONOTONIC_SYSCALL
Denis Vlasenko459be352007-06-17 19:09:05 +0000248
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000249#include <sys/syscall.h>
Denis Vlasenko09c0a742008-06-07 23:43:43 +0000250/* Old glibc (< 2.3.4) does not provide this constant. We use syscall
251 * directly so this definition is safe. */
252#ifndef CLOCK_MONOTONIC
253#define CLOCK_MONOTONIC 1
254#endif
255
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000256static void get_mono(struct timespec *ts)
257{
Denys Vlasenkobe5a5052019-10-24 16:26:55 +0200258 if (clock_gettime(CLOCK_MONOTONIC, ts))
James Byrne69374872019-07-02 11:35:03 +0200259 bb_simple_error_msg_and_die("clock_gettime(MONOTONIC) failed");
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000260}
261unsigned long long FAST_FUNC monotonic_ns(void)
262{
263 struct timespec ts;
264 get_mono(&ts);
265 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
266}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000267unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000268{
269 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000270 get_mono(&ts);
Denis Vlasenko459be352007-06-17 19:09:05 +0000271 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
272}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100273unsigned long long FAST_FUNC monotonic_ms(void)
274{
275 struct timespec ts;
276 get_mono(&ts);
277 return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
278}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000279unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000280{
281 struct timespec ts;
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000282 get_mono(&ts);
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000283 return ts.tv_sec;
284}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000285
Denis Vlasenko459be352007-06-17 19:09:05 +0000286#else
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000287
288unsigned long long FAST_FUNC monotonic_ns(void)
289{
290 struct timeval tv;
291 gettimeofday(&tv, NULL);
292 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
293}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000294unsigned long long FAST_FUNC monotonic_us(void)
Denis Vlasenko459be352007-06-17 19:09:05 +0000295{
296 struct timeval tv;
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000297 gettimeofday(&tv, NULL);
Mike Frysingerebd27aa2007-06-18 07:12:31 +0000298 return tv.tv_sec * 1000000ULL + tv.tv_usec;
Denis Vlasenko459be352007-06-17 19:09:05 +0000299}
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100300unsigned long long FAST_FUNC monotonic_ms(void)
301{
302 struct timeval tv;
303 gettimeofday(&tv, NULL);
304 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
305}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000306unsigned FAST_FUNC monotonic_sec(void)
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000307{
308 return time(NULL);
309}
Denis Vlasenkoce13b762008-06-29 02:25:53 +0000310
Denis Vlasenko459be352007-06-17 19:09:05 +0000311#endif