1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2007 Denys Vlasenko
7 * Licensed under GPLv2, see file LICENSE in this source tree.
11 void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
14 const char *last_colon = strrchr(date_str, ':');
16 if (last_colon != NULL) {
17 /* Parse input and assign appropriately to ptm */
23 if (sscanf(date_str, "%u:%u%c",
28 /* no adjustments needed */
31 if (sscanf(date_str, "%u.%u-%u:%u%c",
32 &ptm->tm_mon, &ptm->tm_mday,
33 &ptm->tm_hour, &ptm->tm_min,
36 /* Adjust month from 1-12 to 0-11 */
39 /* yyyy.mm.dd-HH:MM */
40 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,
44 /* yyyy-mm-dd HH:MM */
45 || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
46 &ptm->tm_mon, &ptm->tm_mday,
47 &ptm->tm_hour, &ptm->tm_min,
50 ptm->tm_year -= 1900; /* Adjust years */
51 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
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
58 return; /* don't fall through to end == ":" check */
62 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
66 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
68 /* else end != NUL and we error out */
72 if (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
73 &ptm->tm_mon, &ptm->tm_mday,
77 || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
78 &ptm->tm_mon, &ptm->tm_mday,
81 ptm->tm_year -= 1900; /* Adjust years */
82 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
84 if (date_str[0] == '@') {
85 time_t t = bb_strtol(date_str + 1, NULL, 10);
87 struct tm *lt = localtime(&t);
95 /* Googled the following on an old date manpage:
97 * The canonical representation for setting the date/time is:
98 * cc Century (either 19 or 20)
99 * yy Year in abbreviated form (e.g. 89, 06)
100 * mm Numeric month, a number from 1 to 12
101 * dd Day, a number from 1 to 31
102 * HH Hour, a number from 0 to 23
103 * MM Minutes, a number from 0 to 59
104 * .SS Seconds, a number from 0 to 61 (with leap seconds)
105 * Everything but the minutes is optional
107 * "touch -t DATETIME" format: [[[[[YY]YY]MM]DD]hh]mm[.ss]
108 * Some, but not all, Unix "date DATETIME" commands
109 * move [[YY]YY] past minutes mm field (!).
110 * Coreutils date does it, and SUS mandates it.
111 * (date -s DATETIME does not support this format. lovely!)
112 * In bbox, this format is special-cased in date applet
113 * (IOW: this function assumes "touch -t" format).
115 unsigned cur_year = ptm->tm_year;
116 int len = strchrnul(date_str, '.') - date_str;
119 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
124 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
130 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
137 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
143 /* Adjust month from 1-12 to 0-11 */
146 /* yymmddHHMM[.SS] */
147 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
154 /* Adjust month from 1-12 to 0-11 */
156 if ((int)cur_year >= 50) { /* >= 1950 */
158 /* 1. Put it in the current century */
159 ptm->tm_year += (cur_year / 100) * 100;
160 /* 2. If too far in the past, +100 years */
161 if (ptm->tm_year < cur_year - 50)
163 /* 3. If too far in the future, -100 years */
164 if (ptm->tm_year > cur_year + 50)
168 /* ccyymmddHHMM[.SS] */
169 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
176 ptm->tm_year -= 1900; /* Adjust years */
177 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
179 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
183 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
184 &ptm->tm_sec, &end) == 1)
186 /* else end != NUL and we error out */
190 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
194 time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
196 time_t t = mktime(ptm);
197 if (t == (time_t) -1L) {
198 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
203 static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt)
210 /* Returns pointer to NUL */
211 return buf + strftime(buf, len, fmt, localtime(tp));
214 char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp)
216 return strftime_fmt(buf, len, tp, "%H:%M:%S");
219 char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
221 return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S");
224 #if ENABLE_MONOTONIC_SYSCALL
226 #include <sys/syscall.h>
227 /* Old glibc (< 2.3.4) does not provide this constant. We use syscall
228 * directly so this definition is safe. */
229 #ifndef CLOCK_MONOTONIC
230 #define CLOCK_MONOTONIC 1
233 /* libc has incredibly messy way of doing this,
234 * typically requiring -lrt. We just skip all this mess */
235 static void get_mono(struct timespec *ts)
237 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
238 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
240 unsigned long long FAST_FUNC monotonic_ns(void)
244 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
246 unsigned long long FAST_FUNC monotonic_us(void)
250 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
252 unsigned long long FAST_FUNC monotonic_ms(void)
256 return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
258 unsigned FAST_FUNC monotonic_sec(void)
267 unsigned long long FAST_FUNC monotonic_ns(void)
270 gettimeofday(&tv, NULL);
271 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
273 unsigned long long FAST_FUNC monotonic_us(void)
276 gettimeofday(&tv, NULL);
277 return tv.tv_sec * 1000000ULL + tv.tv_usec;
279 unsigned long long FAST_FUNC monotonic_ms(void)
282 gettimeofday(&tv, NULL);
283 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
285 unsigned FAST_FUNC monotonic_sec(void)