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