Permalink
Browse files

add (untested) httpdate_parse() function

git-svn-id: http://svn.kfish.org/fastphoto/trunk@143 9c49b5d1-7df3-0310-bce2-b7278e68f44c
  • Loading branch information...
1 parent 6386249 commit a80a1cb505f7a77e2a426a42ba7ff50944de2ea5 conrad committed Feb 16, 2006
Showing with 41 additions and 5 deletions.
  1. +1 −1 src/header.c
  2. +38 −2 src/httpdate.c
  3. +2 −2 src/httpdate.h
View
@@ -13,7 +13,7 @@ header_last_modified (time_t mtime)
{
char buf[30];
- http_date_snprint (buf, 30, mtime);
+ httpdate_snprint (buf, 30, mtime);
return printf ("Last-Modified: %s\n", buf);
}
View
@@ -5,18 +5,54 @@
#include <string.h>
#include <time.h>
+#define HTTPDATE_FMT "%3s, %02d %s %4d %2d:%02d:%02d GMT"
+
static char * wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
static char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int
-http_date_snprint (char * buf, int n, time_t mtime)
+httpdate_snprint (char * buf, int n, time_t mtime)
{
struct tm * g;
g = gmtime (&mtime);
- return snprintf (buf, n, "%s, %02d %s %4d %2d:%02d:%02d GMT",
+ return snprintf (buf, n, HTTPDATE_FMT,
wdays[g->tm_wday], g->tm_mday, months[g->tm_mon],
g->tm_year + 1900, g->tm_hour, g->tm_min, g->tm_sec);
}
+
+time_t
+httpdate_parse (char * s, int n)
+{
+ struct tm d;
+ char wday[3], month[3];
+ int i;
+
+ if (n < 30) return (time_t)(-1);
+
+ memset (&d, 0, sizeof(struct tm));
+
+ sscanf (s, HTTPDATE_FMT,
+ wday, &d.tm_mday, month, &d.tm_year,
+ &d.tm_hour, &d.tm_min, &d.tm_sec);
+
+ for (i = 0; i < 7; i++) {
+ if (!strncmp (wday, wdays[i], 3)) {
+ d.tm_wday = i;
+ break;
+ }
+ }
+
+ for (i = 0; i < 12; i++) {
+ if (!strncmp (month, months[i], 3)) {
+ d.tm_mon = i;
+ break;
+ }
+ }
+
+ d.tm_year += 1900;
+
+ return mktime (&d);
+}
View
@@ -1,7 +1,7 @@
#ifndef __HTTPDATE_H__
#define __HTTPDATE_H__
-int http_date_snprint (char * buf, int n, time_t mtime);
-
+int httpdate_snprint (char * buf, int n, time_t mtime);
+time_t httpdate_parse (char * s, int n);
#endif /* __HTTPDATE_H__ */

0 comments on commit a80a1cb

Please sign in to comment.