dovecot-2.2: lib-storage: Added mail_parse_human_timestamp() to ...

dovecot at dovecot.org dovecot at dovecot.org
Mon Jan 19 21:59:53 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/b0fb4113fbbe
changeset: 18176:b0fb4113fbbe
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Jan 19 23:40:27 2015 +0200
description:
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..

diffstat:

 src/lib-storage/mail-search-register-human.c |  31 +++----------------------
 src/lib-storage/mail-storage.c               |  34 ++++++++++++++++++++++++++++
 src/lib-storage/mail-storage.h               |   5 ++++
 3 files changed, 43 insertions(+), 27 deletions(-)

diffs (115 lines):

diff -r f2a0dd55ad37 -r b0fb4113fbbe src/lib-storage/mail-search-register-human.c
--- a/src/lib-storage/mail-search-register-human.c	Mon Jan 19 22:24:42 2015 +0200
+++ b/src/lib-storage/mail-search-register-human.c	Mon Jan 19 23:40:27 2015 +0200
@@ -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 @@
 		   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 -r f2a0dd55ad37 -r b0fb4113fbbe src/lib-storage/mail-storage.c
--- a/src/lib-storage/mail-storage.c	Mon Jan 19 22:24:42 2015 +0200
+++ b/src/lib-storage/mail-storage.c	Mon Jan 19 23:40:27 2015 +0200
@@ -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 @@
 		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 -r f2a0dd55ad37 -r b0fb4113fbbe src/lib-storage/mail-storage.h
--- a/src/lib-storage/mail-storage.h	Mon Jan 19 22:24:42 2015 +0200
+++ b/src/lib-storage/mail-storage.h	Mon Jan 19 23:40:27 2015 +0200
@@ -948,4 +948,9 @@
    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


More information about the dovecot-cvs mailing list