]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-imap: Added imap_to_date()
authorTimo Sirainen <tss@iki.fi>
Thu, 23 Apr 2015 16:19:10 +0000 (19:19 +0300)
committerTimo Sirainen <tss@iki.fi>
Thu, 23 Apr 2015 16:19:10 +0000 (19:19 +0300)
src/lib-imap/imap-date.c
src/lib-imap/imap-date.h

index f3dcb676d2a7fef5ff8da593c18c5144cd81fb13..5e3e92cd537252ff7e7110e2733eb631f048b18d 100644 (file)
@@ -158,15 +158,10 @@ bool imap_parse_datetime(const char *str, time_t *timestamp_r,
        return TRUE;
 }
 
-static const char *
-imap_to_datetime_tm(const struct tm *tm, int timezone_offset)
+static void imap_to_date_tm(char buf[11], const struct tm *tm)
 {
-       char *buf;
        int year;
 
-       /* @UNSAFE: but faster than t_strdup_printf() call.. */
-       buf = t_malloc(27);
-
        /* dd-mon- */
        buf[0] = (tm->tm_mday / 10) + '0';
        buf[1] = (tm->tm_mday % 10) + '0';
@@ -180,6 +175,16 @@ imap_to_datetime_tm(const struct tm *tm, int timezone_offset)
        buf[8] = ((year / 100) % 10) + '0';
        buf[9] = ((year / 10) % 10) + '0';
        buf[10] = (year % 10) + '0';
+}
+
+static const char *
+imap_to_datetime_tm(const struct tm *tm, int timezone_offset)
+{
+       char *buf;
+
+       /* @UNSAFE: but faster than t_strdup_printf() call.. */
+       buf = t_malloc(27);
+       imap_to_date_tm(buf, tm);
        buf[11] = ' ';
 
        /* hh:mi:ss */
@@ -227,3 +232,16 @@ const char *imap_to_datetime_tz(time_t timestamp, int timezone_offset)
        tm = gmtime(&adjusted);
        return imap_to_datetime_tm(tm, timezone_offset);
 }
+
+bool imap_to_date(time_t timestamp, const char **str_r)
+{
+       const struct tm *tm;
+       char *buf;
+
+       tm = localtime(&timestamp);
+
+       buf = t_malloc0(12);
+       imap_to_date_tm(buf, tm);
+       *str_r = buf;
+       return tm->tm_hour == 0 && tm->tm_min == 0 && tm->tm_sec == 0;
+}
index d5055eb32fca452b9bb7365b445c61fee8af6694..b8ea9827c0a096a6394dc8389296e7d2e7a18219 100644 (file)
@@ -17,4 +17,9 @@ const char *imap_to_datetime(time_t timestamp);
 /* Returns given UTC timestamp as IMAP date-time string in given timezone. */
 const char *imap_to_datetime_tz(time_t timestamp, int timezone_offset);
 
+/* Returns TRUE if timestamp was successfully converted to a date,
+   FALSE if time would have been required as well (but the string is still
+   returned). */
+bool imap_to_date(time_t timestamp, const char **str_r);
+
 #endif