* mu_date_parse_hdwmy => mu_str_date_parse_hdwmy; update unit tests + code

This commit is contained in:
Dirk-Jan C. Binnema 2010-11-30 22:33:15 +02:00
parent ba2918ca41
commit 3560eecf3a
4 changed files with 100 additions and 102 deletions

View File

@ -95,7 +95,7 @@ private:
date = datebuf;
} else {
time_t t;
t = mu_date_parse_hdwmy (date.c_str());
t = mu_str_date_parse_hdwmy (date.c_str());
if (t != (time_t)-1) {
strftime(datebuf, sizeof(datebuf), "%Y%m%d%H%M",
localtime(&t));

View File

@ -153,7 +153,7 @@ mu_str_summarize (const char* str, size_t max_lines)
return summary;
}
/* this is still somewhat simplistic... */
const char*
mu_str_display_contact_s (const char *str)
{
@ -165,14 +165,22 @@ mu_str_display_contact_s (const char *str)
g_strlcpy (contact, str, sizeof(contact));
/* strip the address, if any */
/* we check for '<', so we can strip out the address stuff in
* e.g. 'Hello World <hello@world.xx>, but only if there is
* something alphanumeric before the <
*/
c = g_strstr_len (contact, -1, "<");
if (c != NULL)
*c = '\0';
if (c != NULL) {
for (c2 = contact; c2 < c && !(isalnum(*c2)); ++c2);
if (c2 != c) /* apparently, there was something,
* so we can remove the <... part*/
*c = '\0';
}
/* replace " with space */
for (c2 = contact; *c2; ++c2)
if (*c2 == '"')
if (*c2 == '"' || *c2 == '<' || *c2 == '>')
*c2 = ' ';
g_strstrip (contact);
@ -189,44 +197,6 @@ mu_str_display_contact (const char *str)
}
time_t
mu_date_parse_hdwmy (const char* str)
{
long int num;
char *end;
time_t now, delta;
time_t never = (time_t)-1;
g_return_val_if_fail (str, never);
num = strtol (str, &end, 10);
if (num <= 0 || num > 9999)
return never;
if (!end || end[1] != '\0')
return never;
switch (end[0]) {
case 'h': /* hour */
delta = num * 24 * 60; break;
case 'd': /* day */
delta = num * 24 * 60 * 60; break;
case 'w': /* week */
delta = num * 7 * 24 * 60 * 60; break;
case 'm':
delta = num * 30 * 24 * 60 * 60; break;
case 'y':
delta = num * 365 * 24 * 60 * 60; break;
default:
return never;
}
now = time(NULL);
return delta <= now ? now - delta : never;
}
struct _CheckPrefix {
const char *pfx;
guint len;
@ -292,6 +262,43 @@ is_xapian_prefix (const char *q, const char *colon)
return FALSE;
}
time_t
mu_str_date_parse_hdwmy (const char* str)
{
long int num;
char *end;
time_t now, delta;
time_t never = (time_t)-1;
g_return_val_if_fail (str, never);
num = strtol (str, &end, 10);
if (num <= 0 || num > 9999)
return never;
if (!end || end[1] != '\0')
return never;
switch (end[0]) {
case 'h': /* hour */
delta = num * 60 * 60; break;
case 'd': /* day */
delta = num * 24 * 60 * 60; break;
case 'w': /* week */
delta = num * 7 * 24 * 60 * 60; break;
case 'm':
delta = num * 30 * 24 * 60 * 60; break;
case 'y':
delta = num * 365 * 24 * 60 * 60; break;
default:
return never;
}
now = time(NULL);
return delta <= now ? now - delta : never;
}
char*
mu_str_ascii_xapian_escape_in_place (char *query)
{

View File

@ -191,7 +191,7 @@ char* mu_str_ascii_xapian_escape_in_place (char *query);
* @return the time_t of the point in time indicated by 'now' minus
* the value, or (time_t)-1 otherwise
*/
time_t mu_date_parse_hdwmy (const char* str);
time_t mu_str_date_parse_hdwmy (const char* str);
G_END_DECLS

View File

@ -200,65 +200,55 @@ test_mu_str_ascii_xapian_escape (void)
}
#if 0
static void
test_mu_str_complete_iso_date_begin (void)
test_mu_str_display_contact (void)
{
int i;
struct {
const char* date1;
size_t len;
const char* date2;
} dates [] = {
{ "2010", 14, "20100101000000"},
{ "2009", 12, "200901010000" },
{ "19721214", 14, "19721214000000" },
{ "197212", 8, "19721201" },
};
for (i = 0; i != G_N_ELEMENTS(dates); ++i) {
gchar *str;
str = mu_str_complete_iso_date (dates[i].date1,
dates[i].len, TRUE);
g_assert_cmpstr (str, ==, dates[i].date2);
g_free (str);
}
int i;
struct {
const char* word;
const char* disp;
} words [] = {
{ "\"Foo Bar\" <aap@noot.mies>", "Foo Bar"},
{ "Foo Bar <aap@noot.mies>", "Foo Bar" },
{ "<aap@noot.mies>", "aap@noot.mies" },
{ "foo@bar.nl", "foo@bar.nl" }
};
for (i = 0; i != G_N_ELEMENTS(words); ++i)
g_assert_cmpstr (mu_str_display_contact_s (words[i].word), ==,
words[i].disp);
}
static void
test_mu_str_complete_iso_date_end (void)
test_mu_str_date_parse_hdwmy (void)
{
int i;
struct {
const char* date1;
size_t len;
const char* date2;
} dates [] = {
{ "2010", 14, "20101231235959"},
{ "2009", 12, "200912312359" },
{ "19721214", 14, "19721214235959" },
{ "197212", 8, "19721231" },
};
time_t diff;
diff = time(NULL) - mu_str_date_parse_hdwmy ("3h");
g_assert (diff > 0);
g_assert_cmpuint (3 * 60 * 60 - diff, <=, 1);
diff = time(NULL) - mu_str_date_parse_hdwmy ("5y");
g_assert (diff > 0);
g_assert_cmpuint (5 * 365 * 24 * 60 * 60 - diff, <=, 1);
for (i = 0; i != G_N_ELEMENTS(dates); ++i) {
gchar *str;
str = mu_str_complete_iso_date (dates[i].date1,
dates[i].len, FALSE);
g_assert_cmpstr (str, ==, dates[i].date2);
g_free (str);
}
}
diff = time(NULL) - mu_str_date_parse_hdwmy ("3m");
g_assert (diff > 0);
g_assert_cmpuint (3 * 30 * 24 * 60 * 60 - diff, <=, 1);
#endif
diff = time(NULL) - mu_str_date_parse_hdwmy ("21d");
g_assert (diff > 0);
g_assert_cmpuint (21 * 24 * 60 * 60 - diff, <=, 1);
diff = time(NULL) - mu_str_date_parse_hdwmy ("2w");
g_assert (diff > 0);
g_assert_cmpuint (2 * 7 * 24 * 60 * 60 - diff, <=, 1);
g_assert_cmpint (mu_str_date_parse_hdwmy("-1y"),==, (time_t)-1);
}
@ -287,16 +277,17 @@ main (int argc, char *argv[])
g_test_add_func ("/mu-str/mu-str-normalize-01",
test_mu_str_normalize_01);
g_test_add_func ("/mu-str/mu-str-normalize-02",
test_mu_str_normalize_02);
test_mu_str_normalize_02);
g_test_add_func ("/mu-str/mu-str-ascii-xapian-escape",
test_mu_str_ascii_xapian_escape);
test_mu_str_ascii_xapian_escape);
g_test_add_func ("/mu-str/mu-str-display_contact",
test_mu_str_display_contact);
/* mu_str_complete_iso_date_(begin|end) */
/* g_test_add_func ("/mu-str/mu-str-complete-iso-date-begin", */
/* test_mu_str_complete_iso_date_begin); */
/* g_test_add_func ("/mu-str/mu-str-complete-iso-date-begin", */
/* test_mu_str_complete_iso_date_end); */
g_test_add_func ("/mu-str/mu-str_date_parse_hdwmy",
test_mu_str_date_parse_hdwmy);
/* FIXME: add tests for mu_str_flags; but note the