* clear non-numerics from dates, so '2012-06-12' is parsed correctly

This commit is contained in:
djcb 2012-06-29 10:11:26 +03:00
parent 38d92b98c2
commit 0b70a457de
2 changed files with 39 additions and 3 deletions

View File

@ -19,11 +19,11 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#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;
}

View File

@ -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");
}