1
2
3
4
5
6
7
8
9#include "libbb.h"
10
11
12
13int FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
14{
15 char end = '\0';
16 time_t t;
17#if ENABLE_DESKTOP
18
19
20
21
22
23 static const char fmt_str[] ALIGN1 =
24 "%R" "\0"
25 "%T" "\0"
26 "%m.%d-%R" "\0"
27 "%m.%d-%T" "\0"
28 "%Y.%m.%d-%R" "\0"
29 "%Y.%m.%d-%T" "\0"
30 "%b %d %T %Y" "\0"
31 "%Y-%m-%d %R" "\0"
32 "%Y-%m-%d %T" "\0"
33# if ENABLE_FEATURE_TIMEZONE
34 "%Y-%m-%d %R %z" "\0"
35 "%Y-%m-%d %T %z" "\0"
36# endif
37 "%Y-%m-%d %H" "\0"
38 "%Y-%m-%d" "\0"
39 ;
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);
48 if (endp && *endp == '\0') {
49# if ENABLE_FEATURE_TIMEZONE
50 if (strchr(fmt, 'z')) {
51
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
58 goto localise;
59 }
60# endif
61 return 1;
62 }
63 *ptm = save;
64 while (*++fmt)
65 continue;
66 ++fmt;
67 }
68#else
69 const char *last_colon = strrchr(date_str, ':');
70
71 if (last_colon != NULL) {
72
73
74
75 if (sscanf(date_str, "%u:%u%c",
76 &ptm->tm_hour,
77 &ptm->tm_min,
78 &end) >= 2
79 ) {
80
81 } else
82
83 if (sscanf(date_str, "%u.%u-%u:%u%c",
84 &ptm->tm_mon, &ptm->tm_mday,
85 &ptm->tm_hour, &ptm->tm_min,
86 &end) >= 4
87 ) {
88
89 ptm->tm_mon -= 1;
90 } else
91
92 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,
95 &end) >= 5
96
97 || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
98 &ptm->tm_mon, &ptm->tm_mday,
99 &ptm->tm_hour, &ptm->tm_min,
100 &end) >= 5
101 ) {
102 ptm->tm_year -= 1900;
103 ptm->tm_mon -= 1;
104 } else
105 {
106 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
107 }
108 if (end == ':') {
109
110 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
111 end = '\0';
112
113 }
114 } else
115 if (strchr(date_str, '-')
116
117
118
119
120
121
122
123 && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
124 &ptm->tm_mon, &ptm->tm_mday,
125 &ptm->tm_hour,
126 &end) >= 4
127
128 || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
129 &ptm->tm_mon, &ptm->tm_mday,
130 &end) >= 3
131 )
132 ) {
133 ptm->tm_year -= 1900;
134 ptm->tm_mon -= 1;
135 } else
136#endif
137 if (date_str[0] == '@') {
138 if (sizeof(t) <= sizeof(long))
139 t = bb_strtol(date_str + 1, NULL, 10);
140 else
141 t = bb_strtoll(date_str + 1, NULL, 10);
142 if (!errno) {
143 struct tm *lt;
144 IF_FEATURE_TIMEZONE(localise:)
145 lt = localtime(&t);
146 if (lt) {
147 *ptm = *lt;
148 return 0;
149 }
150 }
151 end = '1';
152 } else {
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 unsigned cur_year = ptm->tm_year;
174 int len = strchrnul(date_str, '.') - date_str;
175
176
177 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
178 &ptm->tm_min,
179 &end) >= 1) {
180 } else
181
182 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
183 &ptm->tm_hour,
184 &ptm->tm_min,
185 &end) >= 2) {
186 } else
187
188 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
189 &ptm->tm_mday,
190 &ptm->tm_hour,
191 &ptm->tm_min,
192 &end) >= 3) {
193 } else
194
195 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
196 &ptm->tm_mon,
197 &ptm->tm_mday,
198 &ptm->tm_hour,
199 &ptm->tm_min,
200 &end) >= 4) {
201
202 ptm->tm_mon -= 1;
203 } else
204
205 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
206 &ptm->tm_year,
207 &ptm->tm_mon,
208 &ptm->tm_mday,
209 &ptm->tm_hour,
210 &ptm->tm_min,
211 &end) >= 5) {
212
213 ptm->tm_mon -= 1;
214 if ((int)cur_year >= 50) {
215
216
217 ptm->tm_year += (cur_year / 100) * 100;
218
219 if (ptm->tm_year < cur_year - 50)
220 ptm->tm_year += 100;
221
222 if (ptm->tm_year > cur_year + 50)
223 ptm->tm_year -= 100;
224 }
225 } else
226
227 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
228 &ptm->tm_year,
229 &ptm->tm_mon,
230 &ptm->tm_mday,
231 &ptm->tm_hour,
232 &ptm->tm_min,
233 &end) >= 5) {
234 ptm->tm_year -= 1900;
235 ptm->tm_mon -= 1;
236 } else {
237 err:
238 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
239 }
240 ptm->tm_sec = 0;
241 if (end == '.') {
242
243 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
244 &ptm->tm_sec, &end) == 1)
245 end = '\0';
246
247 }
248
249
250
251
252
253 if (ptm->tm_sec > 60
254 || ptm->tm_min > 59
255 || ptm->tm_hour > 23
256 || ptm->tm_mday > 31
257 || ptm->tm_mon > 11
258 ) {
259 goto err;
260 }
261 }
262 if (end != '\0') {
263 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
264 }
265 return 1;
266}
267
268time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
269{
270 time_t t = mktime(ptm);
271 if (t == (time_t) -1L) {
272 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
273 }
274 return t;
275}
276
277static 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
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
298#if ENABLE_MONOTONIC_SYSCALL
299
300
301
302#ifndef CLOCK_MONOTONIC
303#define CLOCK_MONOTONIC 1
304#endif
305
306static void get_mono(struct timespec *ts)
307{
308 if (clock_gettime(CLOCK_MONOTONIC, ts))
309 bb_simple_error_msg_and_die("clock_gettime(MONOTONIC) failed");
310}
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}
317unsigned long long FAST_FUNC monotonic_us(void)
318{
319 struct timespec ts;
320 get_mono(&ts);
321 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
322}
323unsigned 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}
329unsigned FAST_FUNC monotonic_sec(void)
330{
331 struct timespec ts;
332 get_mono(&ts);
333 return ts.tv_sec;
334}
335
336#else
337
338unsigned long long FAST_FUNC monotonic_ns(void)
339{
340 struct timeval tv;
341 xgettimeofday(&tv);
342 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
343}
344unsigned long long FAST_FUNC monotonic_us(void)
345{
346 struct timeval tv;
347 xgettimeofday(&tv);
348 return tv.tv_sec * 1000000ULL + tv.tv_usec;
349}
350unsigned long long FAST_FUNC monotonic_ms(void)
351{
352 struct timeval tv;
353 xgettimeofday(&tv);
354 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
355}
356unsigned FAST_FUNC monotonic_sec(void)
357{
358 return time(NULL);
359}
360
361#endif
362