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