From 0b70a457de6e7faa91e57e7e9df0dd9ac7674150 Mon Sep 17 00:00:00 2001 From: djcb Date: Fri, 29 Jun 2012 10:11:26 +0300 Subject: [PATCH] * clear non-numerics from dates, so '2012-06-12' is parsed correctly --- lib/mu-date.c | 27 ++++++++++++++++++++++++--- lib/tests/test-mu-date.c | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/mu-date.c b/lib/mu-date.c index 2e969a3d..10addca1 100644 --- a/lib/mu-date.c +++ b/lib/mu-date.c @@ -19,11 +19,11 @@ #include #include +#include #include "mu-util.h" #include "mu-date.h" - const char* mu_date_str_s (const char* frm, time_t t) { @@ -119,19 +119,40 @@ mu_date_parse_hdwmy (const char *nptr) } +/* clear a date of anything non-numberic; static string, non-reentrant */ +static char* +clear_date_s (const char *date) +{ + static char cleandate [14 + 1]; + unsigned u1, u2; + + for (u1 = u2 = 0; date[u1] != '\0'; ++u1) + if (isdigit(date[u1])) + cleandate[u2++] = date[u1]; + + cleandate[u2] = '\0'; + + return cleandate; +} + + + const char* mu_date_complete_s (const char *date, gboolean is_begin) { static char fulldate[14 + 1]; - static const char* full_begin = "00000101000000"; static const char* full_end = "99991231235959"; + char *cleardate; + g_return_val_if_fail (date, NULL); + cleardate = clear_date_s (date); + strncpy (fulldate, is_begin ? full_begin : full_end, sizeof(fulldate)); - memcpy (fulldate, date, strlen(date)); + memcpy (fulldate, cleardate, strlen(cleardate)); return fulldate; } diff --git a/lib/tests/test-mu-date.c b/lib/tests/test-mu-date.c index a47ffa90..dfac6089 100644 --- a/lib/tests/test-mu-date.c +++ b/lib/tests/test-mu-date.c @@ -106,6 +106,14 @@ test_mu_date_complete_begin (void) "19721214000000"); g_assert_cmpstr (mu_date_complete_s ("19721214234512", TRUE), ==, "19721214234512"); + + g_assert_cmpstr (mu_date_complete_s ("2010-07", TRUE), ==, + "20100701000000"); + g_assert_cmpstr (mu_date_complete_s ("1972/12/14", TRUE), ==, + "19721214000000"); + g_assert_cmpstr (mu_date_complete_s ("1972-12-14 23:45:12", TRUE), ==, + "19721214234512"); + } @@ -124,6 +132,13 @@ test_mu_date_complete_end (void) "19721214235959"); g_assert_cmpstr (mu_date_complete_s ("19721214234512", FALSE), ==, "19721214234512"); + + g_assert_cmpstr (mu_date_complete_s ("2010-07", FALSE), ==, + "20100731235959"); + g_assert_cmpstr (mu_date_complete_s ("1972.12.14", FALSE), ==, + "19721214235959"); + g_assert_cmpstr (mu_date_complete_s ("1972.12.14 23:45/12", FALSE), ==, + "19721214234512"); }