[PATCH] date_parse_iso
Paul Bagyenda <[email protected]>
| Newsgroups | gmane.comp.mobile.kannel.devel |
|---|---|
| Message-ID | <[email protected]> |
date_parse_iso in gwlib/date.c is a little broken. It will not correctly parse ISO dates of the form YYYMMDDTHHMMSS (e.g. 20090121T150501). It works just fine on YYYY-MM-DD... The attached patch remedies this without loss of functionality P.
date.c.diff
(application/octet-stream, 2.5 KB)
Index: gwlib/date.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/date.c,v
retrieving revision 1.21
diff -u -r1.21 date.c
--- gwlib/date.c 12 Jan 2009 16:46:54 -0000 1.21
+++ gwlib/date.c 21 Jan 2009 14:33:11 -0000
@@ -227,7 +227,8 @@
int date_parse_iso (struct universaltime *ut, Octstr *os)
{
long pos = 0;
- int c;
+ int c, n = 0;
+ char *p, *q;
/* assign defaults */
ut->month = 0;
@@ -236,41 +237,55 @@
ut->minute = 0;
ut->second = 0;
- if ((pos = octstr_parse_long(&(ut->year), os, pos, 10)) < 0)
+ p = octstr_get_cstr(os);
+ q = p + ((n = octstr_search_char(os, 'T', 0)) >= 0 ? n : octstr_len(os)); /* stop at the end of string or at the time separator */
+ if (sscanf(p, "%4ld%n", &ut->year, &n) < 1)
return -1;
+ p += n;
+
if (ut->year < 70)
ut->year += 2000;
else if (ut->year < 100)
ut->year += 1900;
-
- while ((c = octstr_get_char(os, pos)) != -1 && !gw_isdigit(c))
- pos++;
- if ((pos = octstr_parse_long(&(ut->month), os, pos, 10)) < 0)
+
+ while (p < q && !gw_isdigit(*p))
+ p++;
+ if (sscanf(p, "%2ld%n", &ut->month, &n) < 1)
return 0;
-
- /* 0-based months */
+ p += n;
+
+ /* 0-based months */
if (ut->month > 0)
- ut->month--;
-
- while ((c = octstr_get_char(os, pos)) != -1 && !gw_isdigit(c))
- pos++;
- if ((pos = octstr_parse_long(&(ut->day), os, pos, 10)) < 0)
+ ut->month--;
+
+ while (p < q && !gw_isdigit(*p))
+ p++;
+ if (sscanf(p, "%2ld%n", &ut->day, &n) < 1)
return 0;
+ p += n;
- while ((c = octstr_get_char(os, pos)) != -1 && !gw_isdigit(c))
- pos++;
- if ((pos = octstr_parse_long(&(ut->hour), os, pos, 10)) < 0)
+ if (*q == 'T')
+ p = q+1;
+ else
+ return 0;
+
+ while (*p && !gw_isdigit(*p))
+ p++;
+ if (sscanf(p, "%2ld%n", &ut->hour, &n) < 1)
return 0;
+ p += n;
- while ((c = octstr_get_char(os, pos)) != -1 && !gw_isdigit(c))
- pos++;
- if ((pos = octstr_parse_long(&(ut->minute), os, pos, 10)) < 0)
+ while (*p && !gw_isdigit(*p))
+ p++;
+ if (sscanf(p, "%2ld%n", &ut->minute, &n) < 1)
return 0;
-
- while ((c = octstr_get_char(os, pos)) != -1 && !gw_isdigit(c))
- pos++;
- if ((pos = octstr_parse_long(&(ut->second), os, pos, 10)) < 0)
+ p += n;
+
+ while (*p && !gw_isdigit(*p))
+ p++;
+ if (sscanf(p, "%2ld%n", &ut->second, &n) < 1)
return 0;
+ p += n;
return 0;
}