* 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; date = datebuf;
} else { } else {
time_t t; 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) { if (t != (time_t)-1) {
strftime(datebuf, sizeof(datebuf), "%Y%m%d%H%M", strftime(datebuf, sizeof(datebuf), "%Y%m%d%H%M",
localtime(&t)); localtime(&t));

View File

@ -153,7 +153,7 @@ mu_str_summarize (const char* str, size_t max_lines)
return summary; return summary;
} }
/* this is still somewhat simplistic... */
const char* const char*
mu_str_display_contact_s (const char *str) 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)); 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, "<"); c = g_strstr_len (contact, -1, "<");
if (c != NULL) if (c != NULL) {
*c = '\0';
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 */ /* replace " with space */
for (c2 = contact; *c2; ++c2) for (c2 = contact; *c2; ++c2)
if (*c2 == '"') if (*c2 == '"' || *c2 == '<' || *c2 == '>')
*c2 = ' '; *c2 = ' ';
g_strstrip (contact); 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 { struct _CheckPrefix {
const char *pfx; const char *pfx;
guint len; guint len;
@ -292,6 +262,43 @@ is_xapian_prefix (const char *q, const char *colon)
return FALSE; 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* char*
mu_str_ascii_xapian_escape_in_place (char *query) 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 * @return the time_t of the point in time indicated by 'now' minus
* the value, or (time_t)-1 otherwise * 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 G_END_DECLS

View File

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