123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #include "libbb.h"
- void FAST_FUNC parse_datestr(const char *date_str, struct tm *ptm)
- {
- char end = '\0';
- const char *last_colon = strrchr(date_str, ':');
- if (last_colon != NULL) {
-
- #if ENABLE_DESKTOP
- const char *endp;
- #endif
-
- if (sscanf(date_str, "%u:%u%c",
- &ptm->tm_hour,
- &ptm->tm_min,
- &end) >= 2
- ) {
-
- } else
-
- if (sscanf(date_str, "%u.%u-%u:%u%c",
- &ptm->tm_mon, &ptm->tm_mday,
- &ptm->tm_hour, &ptm->tm_min,
- &end) >= 4
- ) {
-
- ptm->tm_mon -= 1;
- } else
-
- if (sscanf(date_str, "%u.%u.%u-%u:%u%c", &ptm->tm_year,
- &ptm->tm_mon, &ptm->tm_mday,
- &ptm->tm_hour, &ptm->tm_min,
- &end) >= 5
-
- || sscanf(date_str, "%u-%u-%u %u:%u%c", &ptm->tm_year,
- &ptm->tm_mon, &ptm->tm_mday,
- &ptm->tm_hour, &ptm->tm_min,
- &end) >= 5
- ) {
- ptm->tm_year -= 1900;
- ptm->tm_mon -= 1;
- } else
- #if ENABLE_DESKTOP
-
- if ((endp = strptime(date_str, "%b %d %T %Y", ptm)) != NULL
- && *endp == '\0'
- ) {
- return;
- } else
- #endif
- {
- bb_error_msg_and_die(bb_msg_invalid_date, date_str);
- }
- if (end == ':') {
-
- if (sscanf(last_colon + 1, "%u%c", &ptm->tm_sec, &end) == 1)
- end = '\0';
-
- }
- } else
- if (strchr(date_str, '-')
-
-
- && (sscanf(date_str, "%u-%u-%u %u%c", &ptm->tm_year,
- &ptm->tm_mon, &ptm->tm_mday,
- &ptm->tm_hour,
- &end) >= 4
-
- || sscanf(date_str, "%u-%u-%u%c", &ptm->tm_year,
- &ptm->tm_mon, &ptm->tm_mday,
- &end) >= 3
- )
- ) {
- ptm->tm_year -= 1900;
- ptm->tm_mon -= 1;
- } else
- if (date_str[0] == '@') {
- time_t t = bb_strtol(date_str + 1, NULL, 10);
- if (!errno) {
- struct tm *lt = localtime(&t);
- if (lt) {
- *ptm = *lt;
- return;
- }
- }
- end = '1';
- } else {
-
- unsigned cur_year = ptm->tm_year;
- int len = strchrnul(date_str, '.') - date_str;
-
- if (len == 2 && sscanf(date_str, "%2u%2u%2u%2u""%2u%c" + 12,
- &ptm->tm_min,
- &end) >= 1) {
- } else
-
- if (len == 4 && sscanf(date_str, "%2u%2u%2u""%2u%2u%c" + 9,
- &ptm->tm_hour,
- &ptm->tm_min,
- &end) >= 2) {
- } else
-
- if (len == 6 && sscanf(date_str, "%2u%2u""%2u%2u%2u%c" + 6,
- &ptm->tm_mday,
- &ptm->tm_hour,
- &ptm->tm_min,
- &end) >= 3) {
- } else
-
- if (len == 8 && sscanf(date_str, "%2u""%2u%2u%2u%2u%c" + 3,
- &ptm->tm_mon,
- &ptm->tm_mday,
- &ptm->tm_hour,
- &ptm->tm_min,
- &end) >= 4) {
-
- ptm->tm_mon -= 1;
- } else
-
- if (len == 10 && sscanf(date_str, "%2u%2u%2u%2u%2u%c",
- &ptm->tm_year,
- &ptm->tm_mon,
- &ptm->tm_mday,
- &ptm->tm_hour,
- &ptm->tm_min,
- &end) >= 5) {
-
- ptm->tm_mon -= 1;
- if ((int)cur_year >= 50) {
-
-
- ptm->tm_year += (cur_year / 100) * 100;
-
- if (ptm->tm_year < cur_year - 50)
- ptm->tm_year += 100;
-
- if (ptm->tm_year > cur_year + 50)
- ptm->tm_year -= 100;
- }
- } else
-
- if (len == 12 && sscanf(date_str, "%4u%2u%2u%2u%2u%c",
- &ptm->tm_year,
- &ptm->tm_mon,
- &ptm->tm_mday,
- &ptm->tm_hour,
- &ptm->tm_min,
- &end) >= 5) {
- ptm->tm_year -= 1900;
- ptm->tm_mon -= 1;
- } else {
- bb_error_msg_and_die(bb_msg_invalid_date, date_str);
- }
- ptm->tm_sec = 0;
- if (end == '.') {
-
- if (sscanf(strchr(date_str, '.') + 1, "%u%c",
- &ptm->tm_sec, &end) == 1)
- end = '\0';
-
- }
- }
- if (end != '\0') {
- bb_error_msg_and_die(bb_msg_invalid_date, date_str);
- }
- }
- time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
- {
- time_t t = mktime(ptm);
- if (t == (time_t) -1L) {
- bb_error_msg_and_die(bb_msg_invalid_date, date_str);
- }
- return t;
- }
- static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt)
- {
- time_t t;
- if (!tp) {
- tp = &t;
- time(tp);
- }
-
- return buf + strftime(buf, len, fmt, localtime(tp));
- }
- char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp)
- {
- return strftime_fmt(buf, len, tp, "%H:%M:%S");
- }
- char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
- {
- return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S");
- }
- #if ENABLE_MONOTONIC_SYSCALL
- #include <sys/syscall.h>
- #ifndef CLOCK_MONOTONIC
- #define CLOCK_MONOTONIC 1
- #endif
- static void get_mono(struct timespec *ts)
- {
- if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, ts))
- bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
- }
- unsigned long long FAST_FUNC monotonic_ns(void)
- {
- struct timespec ts;
- get_mono(&ts);
- return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
- }
- unsigned long long FAST_FUNC monotonic_us(void)
- {
- struct timespec ts;
- get_mono(&ts);
- return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
- }
- unsigned long long FAST_FUNC monotonic_ms(void)
- {
- struct timespec ts;
- get_mono(&ts);
- return ts.tv_sec * 1000ULL + ts.tv_nsec/1000000;
- }
- unsigned FAST_FUNC monotonic_sec(void)
- {
- struct timespec ts;
- get_mono(&ts);
- return ts.tv_sec;
- }
- #else
- unsigned long long FAST_FUNC monotonic_ns(void)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
- }
- unsigned long long FAST_FUNC monotonic_us(void)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_sec * 1000000ULL + tv.tv_usec;
- }
- unsigned long long FAST_FUNC monotonic_ms(void)
- {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
- }
- unsigned FAST_FUNC monotonic_sec(void)
- {
- return time(NULL);
- }
- #endif
|