[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, 3.4 KB)
Index: gw/smsc/smsc_smpp.c
===================================================================
RCS file: /home/cvs/gateway/gw/smsc/smsc_smpp.c,v
retrieving revision 1.110
diff -u -r1.110 smsc_smpp.c
--- gw/smsc/smsc_smpp.c	14 Jan 2009 11:11:46 -0000	1.110
+++ gw/smsc/smsc_smpp.c	21 Jan 2009 14:33:11 -0000
@@ -927,13 +927,15 @@
         pdu->u.submit_sm.registered_delivery = 1;
     else if (DLR_IS_FAIL(msg->sms.dlr_mask) && !DLR_IS_SUCCESS(msg->sms.dlr_mask))
         pdu->u.submit_sm.registered_delivery = 2;
-
+#if 0
     /* set priority */
     if (msg->sms.priority >= 0 && msg->sms.priority <= 3)
         pdu->u.submit_sm.priority_flag = msg->sms.priority;
     else
         pdu->u.submit_sm.priority_flag = smpp->priority;
-
+#else
+    pdu->u.submit_sm.priority_flag  = 3;
+#endif
     /* set more messages to send */
     if (smpp->version > 0x33 && msg->sms.msg_left > 0)
         pdu->u.submit_sm.more_messages_to_send = 1;
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;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.