1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2007 Denys Vlasenko
7 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
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",
27 /* no adjustments needed */
30 if (sscanf(date_str, "%u.%u-%u:%u%c",
31 &ptm->tm_mon, &ptm->tm_mday,
32 &ptm->tm_hour, &ptm->tm_min,
34 /* Adjust month from 1-12 to 0-11 */
37 /* yyyy.mm.dd-HH:MM */
38 if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
39 &ptm->tm_mon, &ptm->tm_mday,
40 &ptm->tm_hour, &ptm->tm_min,
42 ptm->tm_year -= 1900; /* Adjust years */
43 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
45 /* yyyy-mm-dd HH:MM */
46 if (sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
47 &ptm->tm_mon, &ptm->tm_mday,
48 &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 */
61 //TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes)
63 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
67 if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
69 /* else end != NUL and we error out */
72 /* Googled the following on an old date manpage:
74 * The canonical representation for setting the date/time is:
75 * cc Century (either 19 or 20)
76 * yy Year in abbreviated form (e.g. 89, 06)
77 * mm Numeric month, a number from 1 to 12
78 * dd Day, a number from 1 to 31
79 * HH Hour, a number from 0 to 23
80 * MM Minutes, a number from 0 to 59
81 * .SS Seconds, a number from 0 to 61 (with leap seconds)
82 * Everything but the minutes is optional
84 * This coincides with the format of "touch -t TIME"
86 int len = strchrnul(date_str, '.') - date_str;
89 if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
94 if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
100 if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
107 if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
113 /* Adjust month from 1-12 to 0-11 */
116 /* yymmddHHMM[.SS] */
117 if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
124 /* Adjust month from 1-12 to 0-11 */
127 /* ccyymmddHHMM[.SS] */
128 if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
135 ptm->tm_year -= 1900; /* Adjust years */
136 ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */
138 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
142 if (sscanf(strchr(date_str, '.') + 1, "%u%c",
143 &ptm->tm_sec, &end) == 1)
145 /* else end != NUL and we error out */
149 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
153 time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
155 time_t t = mktime(ptm);
156 if (t == (time_t) -1L) {
157 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
162 #if ENABLE_MONOTONIC_SYSCALL
164 #include <sys/syscall.h>
165 /* Old glibc (< 2.3.4) does not provide this constant. We use syscall
166 * directly so this definition is safe. */
167 #ifndef CLOCK_MONOTONIC
168 #define CLOCK_MONOTONIC 1
171 /* libc has incredibly messy way of doing this,
172 * typically requiring -lrt. We just skip all this mess */
173 static void get_mono(struct timespec *ts)
175 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
176 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
178 unsigned long long FAST_FUNC monotonic_ns(void)
182 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
184 unsigned long long FAST_FUNC monotonic_us(void)
188 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
190 unsigned long long FAST_FUNC monotonic_ms(void)
194 return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
196 unsigned FAST_FUNC monotonic_sec(void)
205 unsigned long long FAST_FUNC monotonic_ns(void)
208 gettimeofday(&tv, NULL);
209 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
211 unsigned long long FAST_FUNC monotonic_us(void)
214 gettimeofday(&tv, NULL);
215 return tv.tv_sec * 1000000ULL + tv.tv_usec;
217 unsigned long long FAST_FUNC monotonic_ms(void)
220 gettimeofday(&tv, NULL);
221 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
223 unsigned FAST_FUNC monotonic_sec(void)