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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 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, |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 26 | &end) >= 2 |
| 27 | ) { |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 28 | /* no adjustments needed */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 29 | } else |
| 30 | /* mm.dd-HH:MM */ |
| 31 | if (sscanf(date_str, "%u.%u-%u:%u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 32 | &ptm->tm_mon, &ptm->tm_mday, |
| 33 | &ptm->tm_hour, &ptm->tm_min, |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 34 | &end) >= 4 |
| 35 | ) { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 36 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 37 | ptm->tm_mon -= 1; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 38 | } else |
| 39 | /* yyyy.mm.dd-HH:MM */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 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, |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 43 | &end) >= 5 |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 44 | /* yyyy-mm-dd HH:MM */ |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 45 | || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year, |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 46 | &ptm->tm_mon, &ptm->tm_mday, |
| 47 | &ptm->tm_hour, &ptm->tm_min, |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 48 | &end) >= 5 |
| 49 | ) { |
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 |
Alexander Shishkin | 85dbf19 | 2010-03-22 18:22:33 +0100 | [diff] [blame] | 61 | { |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 62 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 63 | } |
| 64 | if (end == ':') { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 65 | /* xxx:SS */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 66 | if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1) |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 67 | end = '\0'; |
| 68 | /* else end != NUL and we error out */ |
| 69 | } |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 70 | } else |
| 71 | /* yyyy-mm-dd HH */ |
| 72 | if (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year, |
| 73 | &ptm->tm_mon, &ptm->tm_mday, |
| 74 | &ptm->tm_hour, |
| 75 | &end) >= 4 |
| 76 | /* yyyy-mm-dd */ |
| 77 | || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year, |
| 78 | &ptm->tm_mon, &ptm->tm_mday, |
| 79 | &end) >= 3 |
| 80 | ) { |
| 81 | ptm->tm_year -= 1900; /* Adjust years */ |
| 82 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
| 83 | } else |
| 84 | if (date_str[0] == '@') { |
Denys Vlasenko | 5f94346 | 2010-04-22 00:45:28 -0400 | [diff] [blame] | 85 | time_t t = bb_strtol(date_str + 1, NULL, 10); |
| 86 | if (!errno) { |
| 87 | struct tm *lt = localtime(&t); |
| 88 | if (lt) { |
| 89 | *ptm = *lt; |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | end = '1'; |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 94 | } else { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 95 | /* Googled the following on an old date manpage: |
| 96 | * |
| 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 |
Denys Vlasenko | 4b624d0 | 2009-09-02 11:49:25 +0200 | [diff] [blame] | 104 | * .SS Seconds, a number from 0 to 61 (with leap seconds) |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 105 | * Everything but the minutes is optional |
| 106 | * |
Denys Vlasenko | 10ee20b | 2011-01-17 14:23:42 +0100 | [diff] [blame] | 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). |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 114 | */ |
Denys Vlasenko | 92ffe05 | 2011-01-02 20:02:09 +0100 | [diff] [blame] | 115 | unsigned cur_year = ptm->tm_year; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 116 | int len = strchrnul(date_str, '.') - date_str; |
| 117 | |
| 118 | /* MM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 119 | 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] | 120 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 121 | &end) >= 1) { |
| 122 | } else |
| 123 | /* HHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 124 | 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] | 125 | &ptm->tm_hour, |
| 126 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 127 | &end) >= 2) { |
| 128 | } else |
| 129 | /* ddHHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 130 | 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] | 131 | &ptm->tm_mday, |
| 132 | &ptm->tm_hour, |
| 133 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 134 | &end) >= 3) { |
| 135 | } else |
| 136 | /* mmddHHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 137 | 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] | 138 | &ptm->tm_mon, |
| 139 | &ptm->tm_mday, |
| 140 | &ptm->tm_hour, |
| 141 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 142 | &end) >= 4) { |
| 143 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 144 | ptm->tm_mon -= 1; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 145 | } else |
| 146 | /* yymmddHHMM[.SS] */ |
| 147 | if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 148 | &ptm->tm_year, |
| 149 | &ptm->tm_mon, |
| 150 | &ptm->tm_mday, |
| 151 | &ptm->tm_hour, |
| 152 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 153 | &end) >= 5) { |
| 154 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 155 | ptm->tm_mon -= 1; |
Denys Vlasenko | 92ffe05 | 2011-01-02 20:02:09 +0100 | [diff] [blame] | 156 | if ((int)cur_year >= 50) { /* >= 1950 */ |
| 157 | /* Adjust year: */ |
| 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) |
| 162 | ptm->tm_year += 100; |
| 163 | /* 3. If too far in the future, -100 years */ |
| 164 | if (ptm->tm_year > cur_year + 50) |
| 165 | ptm->tm_year -= 100; |
| 166 | } |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 167 | } else |
Denys Vlasenko | 4b624d0 | 2009-09-02 11:49:25 +0200 | [diff] [blame] | 168 | /* ccyymmddHHMM[.SS] */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 169 | if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 170 | &ptm->tm_year, |
| 171 | &ptm->tm_mon, |
| 172 | &ptm->tm_mday, |
| 173 | &ptm->tm_hour, |
| 174 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 175 | &end) >= 5) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 176 | ptm->tm_year -= 1900; /* Adjust years */ |
| 177 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 178 | } else { |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 179 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 180 | } |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 181 | if (end == '.') { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 182 | /* xxx.SS */ |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 183 | if (sscanf(strchr(date_str, '.') + 1, "%u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 184 | &ptm->tm_sec, &end) == 1) |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 185 | end = '\0'; |
| 186 | /* else end != NUL and we error out */ |
| 187 | } |
| 188 | } |
| 189 | if (end != '\0') { |
| 190 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 191 | } |
| 192 | } |
| 193 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 194 | 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] | 195 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 196 | time_t t = mktime(ptm); |
Denys Vlasenko | 7aca89a | 2009-07-18 03:41:29 +0200 | [diff] [blame] | 197 | if (t == (time_t) -1L) { |
| 198 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 199 | } |
| 200 | return t; |
| 201 | } |
| 202 | |
Denys Vlasenko | 8f2cb7a | 2013-03-29 12:30:33 +0100 | [diff] [blame] | 203 | static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt) |
| 204 | { |
| 205 | time_t t; |
| 206 | if (!tp) { |
| 207 | tp = &t; |
| 208 | time(tp); |
| 209 | } |
| 210 | /* Returns pointer to NUL */ |
| 211 | return buf + strftime(buf, len, fmt, localtime(tp)); |
| 212 | } |
| 213 | |
| 214 | char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp) |
| 215 | { |
| 216 | return strftime_fmt(buf, len, tp, "%H:%M:%S"); |
| 217 | } |
| 218 | |
| 219 | char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) |
| 220 | { |
| 221 | return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S"); |
| 222 | } |
| 223 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 224 | #if ENABLE_MONOTONIC_SYSCALL |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 225 | |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 226 | #include <sys/syscall.h> |
Denis Vlasenko | 09c0a74 | 2008-06-07 23:43:43 +0000 | [diff] [blame] | 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 |
| 231 | #endif |
| 232 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 233 | /* libc has incredibly messy way of doing this, |
| 234 | * typically requiring -lrt. We just skip all this mess */ |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 235 | static void get_mono(struct timespec *ts) |
| 236 | { |
| 237 | if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts)) |
| 238 | bb_error_msg_and_die("clock_gettime(MONOTONIC) failed"); |
| 239 | } |
| 240 | unsigned long long FAST_FUNC monotonic_ns(void) |
| 241 | { |
| 242 | struct timespec ts; |
| 243 | get_mono(&ts); |
| 244 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 245 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 246 | unsigned long long FAST_FUNC monotonic_us(void) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 247 | { |
| 248 | struct timespec ts; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 249 | get_mono(&ts); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 250 | return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000; |
| 251 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 252 | unsigned long long FAST_FUNC monotonic_ms(void) |
| 253 | { |
| 254 | struct timespec ts; |
| 255 | get_mono(&ts); |
| 256 | return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000; |
| 257 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 258 | unsigned FAST_FUNC monotonic_sec(void) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 259 | { |
| 260 | struct timespec ts; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 261 | get_mono(&ts); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 262 | return ts.tv_sec; |
| 263 | } |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 265 | #else |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 266 | |
| 267 | unsigned long long FAST_FUNC monotonic_ns(void) |
| 268 | { |
| 269 | struct timeval tv; |
| 270 | gettimeofday(&tv, NULL); |
| 271 | return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; |
| 272 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 273 | unsigned long long FAST_FUNC monotonic_us(void) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 274 | { |
| 275 | struct timeval tv; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 276 | gettimeofday(&tv, NULL); |
Mike Frysinger | ebd27aa | 2007-06-18 07:12:31 +0000 | [diff] [blame] | 277 | return tv.tv_sec * 1000000ULL + tv.tv_usec; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 278 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 279 | unsigned long long FAST_FUNC monotonic_ms(void) |
| 280 | { |
| 281 | struct timeval tv; |
| 282 | gettimeofday(&tv, NULL); |
| 283 | return tv.tv_sec * 1000ULL + tv.tv_usec / 1000; |
| 284 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 285 | unsigned FAST_FUNC monotonic_sec(void) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 286 | { |
| 287 | return time(NULL); |
| 288 | } |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 289 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 290 | #endif |