[xiph-commits] r3502 - liboggz/trunk/src/tools/oggz-chop
conrad at svn.annodex.net
conrad at svn.annodex.net
Thu Feb 28 22:50:27 PST 2008
Author: conrad
Date: 2008-02-28 22:50:26 -0800 (Thu, 28 Feb 2008)
New Revision: 3502
Added:
liboggz/trunk/src/tools/oggz-chop/httpdate.c
liboggz/trunk/src/tools/oggz-chop/httpdate_test.c
Log:
commit missing httpdate_test.c, httpdate.c
Added: liboggz/trunk/src/tools/oggz-chop/httpdate.c
===================================================================
--- liboggz/trunk/src/tools/oggz-chop/httpdate.c (rev 0)
+++ liboggz/trunk/src/tools/oggz-chop/httpdate.c 2008-02-29 06:50:26 UTC (rev 3502)
@@ -0,0 +1,66 @@
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#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"};
+
+void
+httpdate_init (void)
+{
+ tzset();
+}
+
+int
+httpdate_snprint (char * buf, int n, time_t mtime)
+{
+ struct tm * g;
+
+ g = gmtime (&mtime);
+
+ 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;
+
+ d.tm_sec -= timezone;
+
+ return mktime (&d);
+}
Added: liboggz/trunk/src/tools/oggz-chop/httpdate_test.c
===================================================================
--- liboggz/trunk/src/tools/oggz-chop/httpdate_test.c (rev 0)
+++ liboggz/trunk/src/tools/oggz-chop/httpdate_test.c 2008-02-29 06:50:26 UTC (rev 3502)
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+#include "tests.h"
+
+#include "httpdate.h"
+
+int
+main (int argc, char * argv[])
+{
+ char * d_in = "Mon, 06 Feb 2006 11:20:01 GMT";
+ char d_out[30];
+ time_t t;
+
+ INFO ("Parsing date:");
+ INFO (d_in);
+ t = httpdate_parse (d_in, 30);
+
+ if (t == (time_t)-1) {
+ FAIL ("Parse error");
+ } else {
+ t -= timezone;
+ httpdate_snprint (d_out, 30, t);
+
+ INFO ("Output date:");
+ INFO (d_out);
+
+ if (strcmp (d_in, d_out)) {
+ FAIL ("Mismatched dates");
+ }
+ }
+
+ return 0;
+}
More information about the commits
mailing list