* clear non-numerics from dates, so '2012-06-12' is parsed correctly
This commit is contained in:
@ -19,11 +19,11 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "mu-util.h"
|
#include "mu-util.h"
|
||||||
#include "mu-date.h"
|
#include "mu-date.h"
|
||||||
|
|
||||||
|
|
||||||
const char*
|
const char*
|
||||||
mu_date_str_s (const char* frm, time_t t)
|
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*
|
const char*
|
||||||
mu_date_complete_s (const char *date, gboolean is_begin)
|
mu_date_complete_s (const char *date, gboolean is_begin)
|
||||||
{
|
{
|
||||||
static char fulldate[14 + 1];
|
static char fulldate[14 + 1];
|
||||||
|
|
||||||
static const char* full_begin = "00000101000000";
|
static const char* full_begin = "00000101000000";
|
||||||
static const char* full_end = "99991231235959";
|
static const char* full_end = "99991231235959";
|
||||||
|
|
||||||
|
char *cleardate;
|
||||||
|
|
||||||
g_return_val_if_fail (date, NULL);
|
g_return_val_if_fail (date, NULL);
|
||||||
|
|
||||||
|
cleardate = clear_date_s (date);
|
||||||
|
|
||||||
strncpy (fulldate, is_begin ? full_begin : full_end,
|
strncpy (fulldate, is_begin ? full_begin : full_end,
|
||||||
sizeof(fulldate));
|
sizeof(fulldate));
|
||||||
memcpy (fulldate, date, strlen(date));
|
memcpy (fulldate, cleardate, strlen(cleardate));
|
||||||
|
|
||||||
return fulldate;
|
return fulldate;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -106,6 +106,14 @@ test_mu_date_complete_begin (void)
|
|||||||
"19721214000000");
|
"19721214000000");
|
||||||
g_assert_cmpstr (mu_date_complete_s ("19721214234512", TRUE), ==,
|
g_assert_cmpstr (mu_date_complete_s ("19721214234512", TRUE), ==,
|
||||||
"19721214234512");
|
"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");
|
"19721214235959");
|
||||||
g_assert_cmpstr (mu_date_complete_s ("19721214234512", FALSE), ==,
|
g_assert_cmpstr (mu_date_complete_s ("19721214234512", FALSE), ==,
|
||||||
"19721214234512");
|
"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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user