aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-time-utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'e-util/e-time-utils.c')
-rw-r--r--e-util/e-time-utils.c146
1 files changed, 93 insertions, 53 deletions
diff --git a/e-util/e-time-utils.c b/e-util/e-time-utils.c
index e466c680bc..716490abf2 100644
--- a/e-util/e-time-utils.c
+++ b/e-util/e-time-utils.c
@@ -25,7 +25,26 @@
#include <libgnome/gnome-i18n.h>
#include "e-time-utils.h"
-static gboolean string_is_empty (const char *value);
+
+/* Returns whether a string is NULL, empty, or full of whitespace */
+static gboolean
+string_is_empty (const char *value)
+{
+ const char *p;
+ gboolean empty = TRUE;
+
+ if (value) {
+ p = value;
+ while (*p) {
+ if (!isspace (*p)) {
+ empty = FALSE;
+ break;
+ }
+ p++;
+ }
+ }
+ return empty;
+}
/*
@@ -42,7 +61,7 @@ ETimeParseStatus
e_time_parse_date_and_time (const char *value,
struct tm *result)
{
- struct tm discard_tm, time_tm;
+ struct tm time_tm;
struct tm *today_tm;
time_t t;
const char *pos, *parse_end;
@@ -50,8 +69,11 @@ e_time_parse_date_and_time (const char *value,
gboolean parsed_date = FALSE, parsed_time = FALSE;
gint i;
- if (string_is_empty (value))
+ if (string_is_empty (value)) {
+ memset (result, 0, sizeof (*result));
+ result->tm_isdst = -1;
return E_TIME_PARSE_NONE;
+ }
pos = value;
@@ -151,24 +173,15 @@ e_time_parse_date_and_time (const char *value,
return E_TIME_PARSE_OK;
}
-
-/*
- * Parses a string containing a time. It is expected to be in a format
- * something like "14:20:00", though we use gettext to support the appropriate
- * local formats and we try to accept slightly different formats, e.g. we can
- * accept 12-hour formats with an am/pm string.
- *
- * Returns E_TIME_PARSE_OK if it could not be parsed, E_TIME_PARSE_NONE if it
- * was empty, or E_TIME_PARSE_INVALID if it couldn't be parsed.
+/* Takes a number of format strings for strptime() and attempts to parse a
+ * string with them.
*/
-ETimeParseStatus
-e_time_parse_time (const char *value,
- struct tm *result)
+static ETimeParseStatus
+parse_with_strptime (const char *value, struct tm *result, const char **formats, int n_formats)
{
const char *pos, *parse_end;
- char *format[4];
- gboolean parsed_time = FALSE;
- gint i;
+ gboolean parsed;
+ int i;
if (string_is_empty (value)) {
memset (result, 0, sizeof (*result));
@@ -178,62 +191,89 @@ e_time_parse_time (const char *value,
pos = value;
- /* Skip any whitespace. */
+ /* Skip whitespace */
while (isspace (*pos))
pos++;
- /* strptime format for a time of day, in 12-hour format.
- If it is not appropriate in the locale set to an empty string. */
- format[0] = _("%I:%M:%S %p");
-
- /* strptime format for a time of day, in 24-hour format. */
- format[1] = _("%H:%M:%S");
-
- /* strptime format for time of day, without seconds, 12-hour format.
- If it is is not appropriate in the locale set to an empty string. */
- format[2] = _("%I:%M %p");
-
- /* strptime format for time of day, without seconds 24-hour format. */
- format[3] = _("%H:%M");
+ /* Try each of the formats in turn */
- for (i = 0; i < sizeof (format) / sizeof (format[0]); i++) {
+ for (i = 0; i < n_formats; i++) {
memset (result, 0, sizeof (*result));
- parse_end = strptime (pos, format[i], result);
+ parse_end = strptime (pos, formats[i], result);
if (parse_end) {
pos = parse_end;
- parsed_time = TRUE;
+ parsed = TRUE;
break;
}
}
result->tm_isdst = -1;
- /* If we don't have a date or a time it must be invalid. */
- if (!parsed_time)
+ /* If we could not parse it it must be invalid. */
+ if (!parsed)
return E_TIME_PARSE_INVALID;
return E_TIME_PARSE_OK;
}
-/* Returns whether a string is NULL, empty, or full of whitespace */
-static gboolean
-string_is_empty (const char *value)
+/**
+ * e_time_parse_date:
+ * @value: A date string.
+ * @result: Return value for the parsed date.
+ *
+ * Takes in a date string entered by the user and tries to convert it to
+ * a struct tm.
+ *
+ * Return value: Result code indicating whether the @value was an empty
+ * string, a valid date, or an invalid date.
+ **/
+ETimeParseStatus
+e_time_parse_date (const char *value, struct tm *result)
{
- const char *p;
- gboolean empty = TRUE;
+ const char *format[4];
- if (value) {
- p = value;
- while (*p) {
- if (!isspace (*p)) {
- empty = FALSE;
- break;
- }
- p++;
- }
- }
- return empty;
+ g_return_val_if_fail (value != NULL, E_TIME_PARSE_INVALID);
+ g_return_val_if_fail (result != NULL, E_TIME_PARSE_INVALID);
+
+ format[0] = _("%m/%d/%Y");
+ format[1] = _("%d/%m/%Y");
+ format[2] = _("%Y/%m/%d");
+ format[3] = _("%x"); /* catch-all give-up strptime()-sucks format */
+
+ return parse_with_strptime (value, result, format, sizeof (format) / sizeof (format[0]));
+}
+
+
+/*
+ * Parses a string containing a time. It is expected to be in a format
+ * something like "14:20:00", though we use gettext to support the appropriate
+ * local formats and we try to accept slightly different formats, e.g. we can
+ * accept 12-hour formats with an am/pm string.
+ *
+ * Returns E_TIME_PARSE_OK if it could not be parsed, E_TIME_PARSE_NONE if it
+ * was empty, or E_TIME_PARSE_INVALID if it couldn't be parsed.
+ */
+ETimeParseStatus
+e_time_parse_time (const char *value, struct tm *result)
+{
+ const char *format[4];
+
+ /* strptime format for a time of day, in 12-hour format.
+ If it is not appropriate in the locale set to an empty string. */
+ format[0] = _("%I:%M:%S %p");
+
+ /* strptime format for a time of day, in 24-hour format. */
+ format[1] = _("%H:%M:%S");
+
+ /* strptime format for time of day, without seconds, 12-hour format.
+ If it is is not appropriate in the locale set to an empty string. */
+ format[2] = _("%I:%M %p");
+
+ /* strptime format for time of day, without seconds 24-hour format. */
+ format[3] = _("%H:%M");
+
+ return parse_with_strptime (value, result, format, sizeof (format) / sizeof (format[0]));
}