Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Denys Vlasenko |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 6 | * |
| 7 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 8 | */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 11 | void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm) |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 12 | { |
| 13 | char end = '\0'; |
| 14 | const char *last_colon = strrchr(date_str, ':'); |
| 15 | |
| 16 | if (last_colon != NULL) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 17 | /* Parse input and assign appropriately to ptm */ |
Alexander Shishkin | 85dbf19 | 2010-03-22 18:22:33 +0100 | [diff] [blame] | 18 | #if ENABLE_DESKTOP |
| 19 | const char *endp; |
| 20 | #endif |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 21 | |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 22 | /* HH:MM */ |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 23 | if (sscanf(date_str, "%u:%u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 24 | &ptm->tm_hour, |
| 25 | &ptm->tm_min, |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 26 | &end) >= 2) { |
| 27 | /* no adjustments needed */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 28 | } else |
| 29 | /* mm.dd-HH:MM */ |
| 30 | if (sscanf(date_str, "%u.%u-%u:%u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 31 | &ptm->tm_mon, &ptm->tm_mday, |
| 32 | &ptm->tm_hour, &ptm->tm_min, |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 33 | &end) >= 4) { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 34 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 35 | ptm->tm_mon -= 1; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 36 | } else |
| 37 | /* yyyy.mm.dd-HH:MM */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 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, |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 41 | &end) >= 5) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 42 | ptm->tm_year -= 1900; /* Adjust years */ |
| 43 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 44 | } else |
| 45 | /* yyyy-mm-dd HH:MM */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 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, |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 49 | &end) >= 5) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 50 | ptm->tm_year -= 1900; /* Adjust years */ |
| 51 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
Alexander Shishkin | 85dbf19 | 2010-03-22 18:22:33 +0100 | [diff] [blame] | 52 | } 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 |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 61 | //TODO: coreutils 6.9 also accepts "yyyy-mm-dd HH" (no minutes) |
Alexander Shishkin | 85dbf19 | 2010-03-22 18:22:33 +0100 | [diff] [blame] | 62 | { |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 63 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 64 | } |
| 65 | if (end == ':') { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 66 | /* xxx:SS */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 67 | if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1) |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 68 | end = '\0'; |
| 69 | /* else end != NUL and we error out */ |
| 70 | } |
Denys Vlasenko | 5f94346 | 2010-04-22 00:45:28 -0400 | [diff] [blame] | 71 | } else if (date_str[0] == '@') { |
| 72 | time_t t = bb_strtol(date_str + 1, NULL, 10); |
| 73 | if (!errno) { |
| 74 | struct tm *lt = localtime(&t); |
| 75 | if (lt) { |
| 76 | *ptm = *lt; |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | end = '1'; |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 81 | } else { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 82 | /* Googled the following on an old date manpage: |
| 83 | * |
| 84 | * The canonical representation for setting the date/time is: |
| 85 | * cc Century (either 19 or 20) |
| 86 | * yy Year in abbreviated form (e.g. 89, 06) |
| 87 | * mm Numeric month, a number from 1 to 12 |
| 88 | * dd Day, a number from 1 to 31 |
| 89 | * HH Hour, a number from 0 to 23 |
| 90 | * MM Minutes, a number from 0 to 59 |
Denys Vlasenko | 4b624d0 | 2009-09-02 11:49:25 +0200 | [diff] [blame] | 91 | * .SS Seconds, a number from 0 to 61 (with leap seconds) |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 92 | * Everything but the minutes is optional |
| 93 | * |
| 94 | * This coincides with the format of "touch -t TIME" |
| 95 | */ |
| 96 | int len = strchrnul(date_str, '.') - date_str; |
| 97 | |
| 98 | /* MM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 99 | if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12, |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 100 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 101 | &end) >= 1) { |
| 102 | } else |
| 103 | /* HHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 104 | if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9, |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 105 | &ptm->tm_hour, |
| 106 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 107 | &end) >= 2) { |
| 108 | } else |
| 109 | /* ddHHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 110 | if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6, |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 111 | &ptm->tm_mday, |
| 112 | &ptm->tm_hour, |
| 113 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 114 | &end) >= 3) { |
| 115 | } else |
| 116 | /* mmddHHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 117 | if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3, |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 118 | &ptm->tm_mon, |
| 119 | &ptm->tm_mday, |
| 120 | &ptm->tm_hour, |
| 121 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 122 | &end) >= 4) { |
| 123 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 124 | ptm->tm_mon -= 1; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 125 | } else |
| 126 | /* yymmddHHMM[.SS] */ |
| 127 | if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 128 | &ptm->tm_year, |
| 129 | &ptm->tm_mon, |
| 130 | &ptm->tm_mday, |
| 131 | &ptm->tm_hour, |
| 132 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 133 | &end) >= 5) { |
| 134 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 135 | ptm->tm_mon -= 1; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 136 | } else |
Denys Vlasenko | 4b624d0 | 2009-09-02 11:49:25 +0200 | [diff] [blame] | 137 | /* ccyymmddHHMM[.SS] */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 138 | if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 139 | &ptm->tm_year, |
| 140 | &ptm->tm_mon, |
| 141 | &ptm->tm_mday, |
| 142 | &ptm->tm_hour, |
| 143 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 144 | &end) >= 5) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 145 | ptm->tm_year -= 1900; /* Adjust years */ |
| 146 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 147 | } else { |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 148 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 149 | } |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 150 | if (end == '.') { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 151 | /* xxx.SS */ |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 152 | if (sscanf(strchr(date_str, '.') + 1, "%u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 153 | &ptm->tm_sec, &end) == 1) |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 154 | end = '\0'; |
| 155 | /* else end != NUL and we error out */ |
| 156 | } |
| 157 | } |
| 158 | if (end != '\0') { |
| 159 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 160 | } |
| 161 | } |
| 162 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 163 | time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm) |
Denys Vlasenko | 7aca89a | 2009-07-18 03:41:29 +0200 | [diff] [blame] | 164 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 165 | time_t t = mktime(ptm); |
Denys Vlasenko | 7aca89a | 2009-07-18 03:41:29 +0200 | [diff] [blame] | 166 | if (t == (time_t) -1L) { |
| 167 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 168 | } |
| 169 | return t; |
| 170 | } |
| 171 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 172 | #if ENABLE_MONOTONIC_SYSCALL |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 174 | #include <sys/syscall.h> |
Denis Vlasenko | 09c0a74 | 2008-06-07 23:43:43 +0000 | [diff] [blame] | 175 | /* Old glibc (< 2.3.4) does not provide this constant. We use syscall |
| 176 | * directly so this definition is safe. */ |
| 177 | #ifndef CLOCK_MONOTONIC |
| 178 | #define CLOCK_MONOTONIC 1 |
| 179 | #endif |
| 180 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 181 | /* libc has incredibly messy way of doing this, |
| 182 | * typically requiring -lrt. We just skip all this mess */ |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 183 | static void get_mono(struct timespec *ts) |
| 184 | { |
| 185 | if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts)) |
| 186 | bb_error_msg_and_die("clock_gettime(MONOTONIC) failed"); |
| 187 | } |
| 188 | unsigned long long FAST_FUNC monotonic_ns(void) |
| 189 | { |
| 190 | struct timespec ts; |
| 191 | get_mono(&ts); |
| 192 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 193 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 194 | unsigned long long FAST_FUNC monotonic_us(void) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 195 | { |
| 196 | struct timespec ts; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 197 | get_mono(&ts); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 198 | return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000; |
| 199 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 200 | unsigned long long FAST_FUNC monotonic_ms(void) |
| 201 | { |
| 202 | struct timespec ts; |
| 203 | get_mono(&ts); |
| 204 | return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000; |
| 205 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 206 | unsigned FAST_FUNC monotonic_sec(void) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 207 | { |
| 208 | struct timespec ts; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 209 | get_mono(&ts); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 210 | return ts.tv_sec; |
| 211 | } |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 212 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 213 | #else |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 214 | |
| 215 | unsigned long long FAST_FUNC monotonic_ns(void) |
| 216 | { |
| 217 | struct timeval tv; |
| 218 | gettimeofday(&tv, NULL); |
| 219 | return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; |
| 220 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 221 | unsigned long long FAST_FUNC monotonic_us(void) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 222 | { |
| 223 | struct timeval tv; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 224 | gettimeofday(&tv, NULL); |
Mike Frysinger | ebd27aa | 2007-06-18 07:12:31 +0000 | [diff] [blame] | 225 | return tv.tv_sec * 1000000ULL + tv.tv_usec; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 226 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 227 | unsigned long long FAST_FUNC monotonic_ms(void) |
| 228 | { |
| 229 | struct timeval tv; |
| 230 | gettimeofday(&tv, NULL); |
| 231 | return tv.tv_sec * 1000ULL + tv.tv_usec / 1000; |
| 232 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 233 | unsigned FAST_FUNC monotonic_sec(void) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 234 | { |
| 235 | return time(NULL); |
| 236 | } |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 237 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 238 | #endif |