From 120dd01f52ecb944fb0f317557a9b8bd020146dd Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Thu, 23 Apr 2015 19:19:10 +0300 Subject: [PATCH] lib-imap: Added imap_to_date() --- src/lib-imap/imap-date.c | 30 ++++++++++++++++++++++++------ src/lib-imap/imap-date.h | 5 +++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/lib-imap/imap-date.c b/src/lib-imap/imap-date.c index f3dcb676d2..5e3e92cd53 100644 --- a/src/lib-imap/imap-date.c +++ b/src/lib-imap/imap-date.c @@ -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(×tamp); + + 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; +} diff --git a/src/lib-imap/imap-date.h b/src/lib-imap/imap-date.h index d5055eb32f..b8ea9827c0 100644 --- a/src/lib-imap/imap-date.h +++ b/src/lib-imap/imap-date.h @@ -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 -- 2.47.3