]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Added mail_parse_human_timestamp() to parse human-writable timestamps.
authorTimo Sirainen <tss@iki.fi>
Mon, 19 Jan 2015 21:40:27 +0000 (23:40 +0200)
committerTimo Sirainen <tss@iki.fi>
Mon, 19 Jan 2015 21:40:27 +0000 (23:40 +0200)
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..

src/lib-storage/mail-search-register-human.c
src/lib-storage/mail-storage.c
src/lib-storage/mail-storage.h

index c548acdcb66b16bef773616384cb633d7a33a70b..2e1fdf7b22e190d90ca46d39b161c48fd2a9f4c8 100644 (file)
@@ -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) {
index 6c640e0bd4fc75d3cbd03016bc3794a1464bff23..a6abf4e6bb07dfff11278eb37756d1cd6dc59164 100644 (file)
@@ -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;
+       }
+}
index 49d59760fd5a1ca1a9575bc51c69c947b2d91e8f..a7dbb9c55ab69ed9b67c7360746f8ce1f6d341cf 100644 (file)
@@ -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