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 |
Denys Vlasenko | 3b39478 | 2014-01-21 07:58:18 +0100 | [diff] [blame] | 71 | if (strchr(date_str, '-') |
| 72 | /* Why strchr('-') check? |
| 73 | * sscanf below will trash ptm->tm_year, this breaks |
| 74 | * if parse_str is "10101010" (iow, "MMddhhmm" form) |
| 75 | * because we destroy year. Do these sscanf |
| 76 | * only if we saw a dash in parse_str. |
| 77 | */ |
| 78 | /* yyyy-mm-dd HH */ |
| 79 | && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year, |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 80 | &ptm->tm_mon, &ptm->tm_mday, |
| 81 | &ptm->tm_hour, |
| 82 | &end) >= 4 |
Denys Vlasenko | 3b39478 | 2014-01-21 07:58:18 +0100 | [diff] [blame] | 83 | /* yyyy-mm-dd */ |
| 84 | || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year, |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 85 | &ptm->tm_mon, &ptm->tm_mday, |
| 86 | &end) >= 3 |
Denys Vlasenko | 3b39478 | 2014-01-21 07:58:18 +0100 | [diff] [blame] | 87 | ) |
Bartosz Golaszewski | 688a7e3 | 2013-07-25 04:59:46 +0200 | [diff] [blame] | 88 | ) { |
| 89 | ptm->tm_year -= 1900; /* Adjust years */ |
| 90 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
| 91 | } else |
| 92 | if (date_str[0] == '@') { |
Denys Vlasenko | 32a8f70 | 2020-11-28 23:21:13 +0100 | [diff] [blame] | 93 | time_t t; |
| 94 | if (sizeof(t) <= sizeof(long)) |
| 95 | t = bb_strtol(date_str + 1, NULL, 10); |
| 96 | else /* time_t is 64 bits but longs are smaller */ |
| 97 | t = bb_strtoll(date_str + 1, NULL, 10); |
Denys Vlasenko | 5f94346 | 2010-04-22 00:45:28 -0400 | [diff] [blame] | 98 | if (!errno) { |
| 99 | struct tm *lt = localtime(&t); |
| 100 | if (lt) { |
| 101 | *ptm = *lt; |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | end = '1'; |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 106 | } else { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 107 | /* Googled the following on an old date manpage: |
| 108 | * |
| 109 | * The canonical representation for setting the date/time is: |
| 110 | * cc Century (either 19 or 20) |
| 111 | * yy Year in abbreviated form (e.g. 89, 06) |
| 112 | * mm Numeric month, a number from 1 to 12 |
| 113 | * dd Day, a number from 1 to 31 |
| 114 | * HH Hour, a number from 0 to 23 |
| 115 | * MM Minutes, a number from 0 to 59 |
Denys Vlasenko | 4b624d0 | 2009-09-02 11:49:25 +0200 | [diff] [blame] | 116 | * .SS Seconds, a number from 0 to 61 (with leap seconds) |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 117 | * Everything but the minutes is optional |
| 118 | * |
Denys Vlasenko | 10ee20b | 2011-01-17 14:23:42 +0100 | [diff] [blame] | 119 | * "touch -t DATETIME" format: [[[[[YY]YY]MM]DD]hh]mm[.ss] |
| 120 | * Some, but not all, Unix "date DATETIME" commands |
| 121 | * move [[YY]YY] past minutes mm field (!). |
| 122 | * Coreutils date does it, and SUS mandates it. |
| 123 | * (date -s DATETIME does not support this format. lovely!) |
| 124 | * In bbox, this format is special-cased in date applet |
| 125 | * (IOW: this function assumes "touch -t" format). |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 126 | */ |
Denys Vlasenko | 92ffe05 | 2011-01-02 20:02:09 +0100 | [diff] [blame] | 127 | unsigned cur_year = ptm->tm_year; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 128 | int len = strchrnul(date_str, '.') - date_str; |
| 129 | |
| 130 | /* MM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 131 | 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] | 132 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 133 | &end) >= 1) { |
| 134 | } else |
| 135 | /* HHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 136 | 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] | 137 | &ptm->tm_hour, |
| 138 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 139 | &end) >= 2) { |
| 140 | } else |
| 141 | /* ddHHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 142 | 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] | 143 | &ptm->tm_mday, |
| 144 | &ptm->tm_hour, |
| 145 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 146 | &end) >= 3) { |
| 147 | } else |
| 148 | /* mmddHHMM[.SS] */ |
Denys Vlasenko | ff1822a | 2009-11-15 04:55:40 +0100 | [diff] [blame] | 149 | 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] | 150 | &ptm->tm_mon, |
| 151 | &ptm->tm_mday, |
| 152 | &ptm->tm_hour, |
| 153 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 154 | &end) >= 4) { |
| 155 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 156 | ptm->tm_mon -= 1; |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 157 | } else |
| 158 | /* yymmddHHMM[.SS] */ |
| 159 | if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 160 | &ptm->tm_year, |
| 161 | &ptm->tm_mon, |
| 162 | &ptm->tm_mday, |
| 163 | &ptm->tm_hour, |
| 164 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 165 | &end) >= 5) { |
| 166 | /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 167 | ptm->tm_mon -= 1; |
Denys Vlasenko | 92ffe05 | 2011-01-02 20:02:09 +0100 | [diff] [blame] | 168 | if ((int)cur_year >= 50) { /* >= 1950 */ |
| 169 | /* Adjust year: */ |
| 170 | /* 1. Put it in the current century */ |
| 171 | ptm->tm_year += (cur_year / 100) * 100; |
| 172 | /* 2. If too far in the past, +100 years */ |
| 173 | if (ptm->tm_year < cur_year - 50) |
| 174 | ptm->tm_year += 100; |
| 175 | /* 3. If too far in the future, -100 years */ |
| 176 | if (ptm->tm_year > cur_year + 50) |
| 177 | ptm->tm_year -= 100; |
| 178 | } |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 179 | } else |
Denys Vlasenko | 4b624d0 | 2009-09-02 11:49:25 +0200 | [diff] [blame] | 180 | /* ccyymmddHHMM[.SS] */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 181 | if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 182 | &ptm->tm_year, |
| 183 | &ptm->tm_mon, |
| 184 | &ptm->tm_mday, |
| 185 | &ptm->tm_hour, |
| 186 | &ptm->tm_min, |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 187 | &end) >= 5) { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 188 | ptm->tm_year -= 1900; /* Adjust years */ |
| 189 | ptm->tm_mon -= 1; /* Adjust month from 1-12 to 0-11 */ |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 190 | } else { |
Denys Vlasenko | 76832ff | 2018-09-23 20:27:32 +0200 | [diff] [blame] | 191 | err: |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 192 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 193 | } |
Natanael Copa | b684d1b | 2016-05-19 17:31:59 +0200 | [diff] [blame] | 194 | ptm->tm_sec = 0; /* assume zero if [.SS] is not given */ |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 195 | if (end == '.') { |
Denys Vlasenko | 38dd8aa | 2009-07-18 04:49:20 +0200 | [diff] [blame] | 196 | /* xxx.SS */ |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 197 | if (sscanf(strchr(date_str, '.') + 1, "%u%c", |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 198 | &ptm->tm_sec, &end) == 1) |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 199 | end = '\0'; |
| 200 | /* else end != NUL and we error out */ |
| 201 | } |
Denys Vlasenko | 76832ff | 2018-09-23 20:27:32 +0200 | [diff] [blame] | 202 | /* Users were confused by "date -s 20180923" |
| 203 | * working (not in the way they were expecting). |
| 204 | * It was interpreted as MMDDhhmm, and not bothered by |
| 205 | * "month #20" in the least. Prevent such cases: |
| 206 | */ |
| 207 | if (ptm->tm_sec > 60 /* allow "23:60" leap second */ |
| 208 | || ptm->tm_min > 59 |
| 209 | || ptm->tm_hour > 23 |
| 210 | || ptm->tm_mday > 31 |
| 211 | || ptm->tm_mon > 11 /* month# is 0..11, not 1..12 */ |
| 212 | ) { |
| 213 | goto err; |
| 214 | } |
Denys Vlasenko | 73b71f3 | 2009-07-18 03:40:35 +0200 | [diff] [blame] | 215 | } |
| 216 | if (end != '\0') { |
| 217 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 218 | } |
| 219 | } |
| 220 | |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 221 | 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] | 222 | { |
Denys Vlasenko | dc698bb | 2010-01-09 19:10:49 +0100 | [diff] [blame] | 223 | time_t t = mktime(ptm); |
Denys Vlasenko | 7aca89a | 2009-07-18 03:41:29 +0200 | [diff] [blame] | 224 | if (t == (time_t) -1L) { |
| 225 | bb_error_msg_and_die(bb_msg_invalid_date, date_str); |
| 226 | } |
| 227 | return t; |
| 228 | } |
| 229 | |
Denys Vlasenko | 8f2cb7a | 2013-03-29 12:30:33 +0100 | [diff] [blame] | 230 | static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt) |
| 231 | { |
| 232 | time_t t; |
| 233 | if (!tp) { |
| 234 | tp = &t; |
| 235 | time(tp); |
| 236 | } |
| 237 | /* Returns pointer to NUL */ |
| 238 | return buf + strftime(buf, len, fmt, localtime(tp)); |
| 239 | } |
| 240 | |
| 241 | char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp) |
| 242 | { |
| 243 | return strftime_fmt(buf, len, tp, "%H:%M:%S"); |
| 244 | } |
| 245 | |
| 246 | char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) |
| 247 | { |
| 248 | return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S"); |
| 249 | } |
| 250 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 251 | #if ENABLE_MONOTONIC_SYSCALL |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 252 | |
Denis Vlasenko | 09c0a74 | 2008-06-07 23:43:43 +0000 | [diff] [blame] | 253 | /* Old glibc (< 2.3.4) does not provide this constant. We use syscall |
| 254 | * directly so this definition is safe. */ |
| 255 | #ifndef CLOCK_MONOTONIC |
| 256 | #define CLOCK_MONOTONIC 1 |
| 257 | #endif |
| 258 | |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 259 | static void get_mono(struct timespec *ts) |
| 260 | { |
Denys Vlasenko | be5a505 | 2019-10-24 16:26:55 +0200 | [diff] [blame] | 261 | if (clock_gettime(CLOCK_MONOTONIC, ts)) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 262 | bb_simple_error_msg_and_die("clock_gettime(MONOTONIC) failed"); |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 263 | } |
| 264 | unsigned long long FAST_FUNC monotonic_ns(void) |
| 265 | { |
| 266 | struct timespec ts; |
| 267 | get_mono(&ts); |
| 268 | return ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 269 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 270 | unsigned long long FAST_FUNC monotonic_us(void) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 271 | { |
| 272 | struct timespec ts; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 273 | get_mono(&ts); |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 274 | return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000; |
| 275 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 276 | unsigned long long FAST_FUNC monotonic_ms(void) |
| 277 | { |
| 278 | struct timespec ts; |
| 279 | get_mono(&ts); |
| 280 | return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000; |
| 281 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 282 | unsigned FAST_FUNC monotonic_sec(void) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 283 | { |
| 284 | struct timespec ts; |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 285 | get_mono(&ts); |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 286 | return ts.tv_sec; |
| 287 | } |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 289 | #else |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 290 | |
| 291 | unsigned long long FAST_FUNC monotonic_ns(void) |
| 292 | { |
| 293 | struct timeval tv; |
| 294 | gettimeofday(&tv, NULL); |
| 295 | return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; |
| 296 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 297 | unsigned long long FAST_FUNC monotonic_us(void) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 298 | { |
| 299 | struct timeval tv; |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 300 | gettimeofday(&tv, NULL); |
Mike Frysinger | ebd27aa | 2007-06-18 07:12:31 +0000 | [diff] [blame] | 301 | return tv.tv_sec * 1000000ULL + tv.tv_usec; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 302 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 303 | unsigned long long FAST_FUNC monotonic_ms(void) |
| 304 | { |
| 305 | struct timeval tv; |
| 306 | gettimeofday(&tv, NULL); |
| 307 | return tv.tv_sec * 1000ULL + tv.tv_usec / 1000; |
| 308 | } |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 309 | unsigned FAST_FUNC monotonic_sec(void) |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 310 | { |
| 311 | return time(NULL); |
| 312 | } |
Denis Vlasenko | ce13b76 | 2008-06-29 02:25:53 +0000 | [diff] [blame] | 313 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 314 | #endif |