]> granicus.if.org Git - curl/commitdiff
provide and export Curl_parsedate() as a library-wide internal function
authorDaniel Stenberg <daniel@haxx.se>
Thu, 3 Sep 2009 08:13:32 +0000 (08:13 +0000)
committerDaniel Stenberg <daniel@haxx.se>
Thu, 3 Sep 2009 08:13:32 +0000 (08:13 +0000)
for a better API to date parsing than the external API is

lib/parsedate.c
lib/parsedate.h

index 843368cf74ac59980407a6bc7e13b337122564fb..badf71114d9fc17b02dbb901ded8012d3201737c 100644 (file)
@@ -270,7 +270,18 @@ static time_t my_timegm(struct my_tm *tm)
            + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec;
 }
 
-static time_t parsedate(const char *date)
+/*
+ * Curl_parsedate()
+ *
+ * Returns:
+ *
+ * PARSEDATE_OK     - a fine conversion
+ * PARSEDATE_FAIL   - failed to convert
+ * PARSEDATE_LATER  - time overflow at the far end of time_t
+ * PARSEDATE_SOONER - time underflow at the low end of time_t
+ */
+
+int Curl_parsedate(const char *date, time_t *output)
 {
   time_t t = 0;
   int wdaynum=-1;  /* day of the week number, 0-6 (mon-sun) */
@@ -318,7 +329,7 @@ static time_t parsedate(const char *date)
       }
 
       if(!found)
-        return -1; /* bad string */
+        return PARSEDATE_FAIL; /* bad string */
 
       date += len;
     }
@@ -389,7 +400,7 @@ static time_t parsedate(const char *date)
         }
 
         if(!found)
-          return -1;
+          return PARSEDATE_FAIL;
 
         date = end;
       }
@@ -405,14 +416,21 @@ static time_t parsedate(const char *date)
      (-1 == monnum) ||
      (-1 == yearnum))
     /* lacks vital info, fail */
-    return -1;
+    return PARSEDATE_FAIL;
 
 #if SIZEOF_TIME_T < 5
   /* 32 bit time_t can only hold dates to the beginning of 2038 */
-  if(yearnum > 2037)
-    return 0x7fffffff;
+  if(yearnum > 2037) {
+    *output = 0x7fffffff;
+    return PARSEDATE_LATER;
+  }
 #endif
 
+  if(yearnum < 1970) {
+    *output = 0;
+    return PARSEDATE_SOONER;
+  }
+
   tm.tm_sec = secnum;
   tm.tm_min = minnum;
   tm.tm_hour = hournum;
@@ -441,11 +459,23 @@ static time_t parsedate(const char *date)
     t += delta;
   }
 
-  return t;
+  *output = t;
+
+  return PARSEDATE_OK;
 }
 
 time_t curl_getdate(const char *p, const time_t *now)
 {
-  (void)now;
-  return parsedate(p);
+  time_t parsed;
+  int rc = Curl_parsedate(p, &parsed);
+  (void)now; /* legacy argument from the past that we ignore */
+
+  switch(rc) {
+  case PARSEDATE_OK:
+  case PARSEDATE_LATER:
+  case PARSEDATE_SOONER:
+    return parsed;
+  }
+  /* everything else is fail */
+  return -1;
 }
index 3e502b9bcc2caeab33b11cb8c1f3438c76a80f66..af59bae7ffb96b33cbe4e6cfa8ce4070fffdf9e1 100644 (file)
 extern const char * const Curl_wkday[7];
 extern const char * const Curl_month[12];
 
+/*
+ * Curl_parsedate()
+ *
+ * Returns:
+ *
+ * PARSEDATE_OK     - a fine conversion
+ * PARSEDATE_FAIL   - failed to convert
+ * PARSEDATE_LATER  - time overflow at the far end of time_t
+ * PARSEDATE_SOONER - time underflow at the low end of time_t
+ */
+
+int Curl_parsedate(const char *date, time_t *output);
+
+#define PARSEDATE_OK     0
+#define PARSEDATE_FAIL   -1
+#define PARSEDATE_LATER  1
+#define PARSEDATE_SOONER 2
+
 #endif