From: Timo Sirainen Date: Mon, 19 Jan 2015 21:40:27 +0000 (+0200) Subject: lib-storage: Added mail_parse_human_timestamp() to parse human-writable timestamps. X-Git-Tag: 2.2.16.rc1~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d81bbb6948b4d758e87206acb8da4cc4366fd478;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Added mail_parse_human_timestamp() to parse human-writable timestamps. This isn't really the ideal location for the function, but since it uses both lib-settings and lib-imap, there's not any good location for it that I can think of.. --- diff --git a/src/lib-storage/mail-search-register-human.c b/src/lib-storage/mail-search-register-human.c index c548acdcb6..2e1fdf7b22 100644 --- a/src/lib-storage/mail-search-register-human.c +++ b/src/lib-storage/mail-search-register-human.c @@ -6,7 +6,7 @@ #include "str.h" #include "unichar.h" #include "settings-parser.h" -#include "imap-date.h" +#include "mail-storage.h" #include "mail-search-register.h" #include "mail-search-parser.h" #include "mail-search-build.h" @@ -34,38 +34,15 @@ arg_new_human_date(struct mail_search_build_context *ctx, enum mail_search_date_type date_type) { struct mail_search_arg *sarg; - const char *value, *error; - struct tm tm; - unsigned int secs; - unsigned long unixtime; + const char *value; sarg = mail_search_build_new(ctx, type); if (mail_search_parse_string(ctx->parser, &value) < 0) return NULL; - /* a) yyyy-mm-dd - b) imap date - c) unix timestamp - d) interval (e.g. n days) */ - if (i_isdigit(value[0]) && i_isdigit(value[1]) && - i_isdigit(value[2]) && i_isdigit(value[3]) && value[4] == '-' && - i_isdigit(value[5]) && i_isdigit(value[6]) && value[7] == '-' && - i_isdigit(value[8]) && i_isdigit(value[9]) && value[10] == '\0') { - memset(&tm, 0, sizeof(tm)); - tm.tm_year = (value[0]-'0') * 1000 + (value[1]-'0') * 100 + - (value[2]-'0') * 10 + (value[3]-'0') - 1900; - tm.tm_mon = (value[5]-'0') * 10 + (value[6]-'0') - 1; - tm.tm_mday = (value[8]-'0') * 10 + (value[9]-'0'); - sarg->value.time = mktime(&tm); - } else if (imap_parse_date(value, &sarg->value.time)) { - /* imap date */ - } else if (str_to_ulong(value, &unixtime) == 0) { - sarg->value.time = unixtime; - } else if (settings_get_time(value, &secs, &error) == 0) { - sarg->value.time = ioloop_time - secs; - } else { + if (mail_parse_human_timestamp(value, &sarg->value.time) < 0) sarg->value.time = (time_t)-1; - } + sarg->value.search_flags = MAIL_SEARCH_ARG_FLAG_USE_TZ; if (sarg->value.time == (time_t)-1) { diff --git a/src/lib-storage/mail-storage.c b/src/lib-storage/mail-storage.c index 6c640e0bd4..a6abf4e6bb 100644 --- a/src/lib-storage/mail-storage.c +++ b/src/lib-storage/mail-storage.c @@ -12,6 +12,8 @@ #include "time-util.h" #include "var-expand.h" #include "dsasl-client.h" +#include "imap-date.h" +#include "settings-parser.h" #include "mail-index-private.h" #include "mail-index-alloc-cache.h" #include "mailbox-tree.h" @@ -2480,3 +2482,35 @@ mail_storage_settings_to_index_flags(const struct mail_storage_settings *set) index_flags |= MAIL_INDEX_OPEN_FLAG_NFS_FLUSH; return index_flags; } + +int mail_parse_human_timestamp(const char *str, time_t *timestamp_r) +{ + struct tm tm; + unsigned int secs; + const char *error; + + if (i_isdigit(str[0]) && i_isdigit(str[1]) && + i_isdigit(str[2]) && i_isdigit(str[3]) && str[4] == '-' && + i_isdigit(str[5]) && i_isdigit(str[6]) && str[7] == '-' && + i_isdigit(str[8]) && i_isdigit(str[9]) && str[10] == '\0') { + /* yyyy-mm-dd */ + memset(&tm, 0, sizeof(tm)); + tm.tm_year = (str[0]-'0') * 1000 + (str[1]-'0') * 100 + + (str[2]-'0') * 10 + (str[3]-'0') - 1900; + tm.tm_mon = (str[5]-'0') * 10 + (str[6]-'0') - 1; + tm.tm_mday = (str[8]-'0') * 10 + (str[9]-'0'); + *timestamp_r = mktime(&tm); + return 0; + } else if (imap_parse_date(str, timestamp_r)) { + /* imap date */ + return 0; + } else if (str_to_time(str, timestamp_r) == 0) { + /* unix timestamp */ + return 0; + } else if (settings_get_time(str, &secs, &error) == 0) { + *timestamp_r = ioloop_time - secs; + return 0; + } else { + return -1; + } +} diff --git a/src/lib-storage/mail-storage.h b/src/lib-storage/mail-storage.h index 49d59760fd..a7dbb9c55a 100644 --- a/src/lib-storage/mail-storage.h +++ b/src/lib-storage/mail-storage.h @@ -948,4 +948,9 @@ void mail_set_cache_corrupted(struct mail *mail, enum mail_fetch_field field); 128 bits are returned. */ void mail_generate_guid_128_hash(const char *guid, guid_128_t guid_128_r); +/* Parse a human-writable string into a timestamp. Returns 0 and timestamp on + success, -1 if the string couldn't be parsed. Currently supported string + formats: yyyy-mm-dd, imap date, unix timestamp, interval (e.g. n days). */ +int mail_parse_human_timestamp(const char *str, time_t *timestamp_r); + #endif