[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Refactor ISO-style (yyyy-mm-dd hh:mm:ss) time parsing into ...



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv18598/src/common

Modified Files:
	util.c util.h 
Log Message:
Refactor ISO-style (yyyy-mm-dd hh:mm:ss) time parsing into util.c; rename format/parse_rfc1123_time; make rephist remember used bandwidth; published used bandwidth in descriptors in 15-minute chunks.  Breaks unittests.

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.121
retrieving revision 1.122
diff -u -d -r1.121 -r1.122
--- util.c	7 Aug 2004 01:18:02 -0000	1.121
+++ util.c	7 Aug 2004 02:46:15 -0000	1.122
@@ -9,6 +9,10 @@
  * process control, and cross-platform portability.
  **/
 
+/* This is required on rh7 to make strptime not complain.
+ */
+#define _GNU_SOURCE
+
 #include "orconfig.h"
 
 #ifdef MS_WINDOWS
@@ -905,7 +909,7 @@
   { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
 
-void tor_format_rfc1123_time(char *buf, time_t t) {
+void format_rfc1123_time(char *buf, time_t t) {
   struct tm *tm = gmtime(&t);
 
   strftime(buf, RFC1123_TIME_LEN+1, "XXX, %d XXX %Y %H:%M:%S GMT", tm);
@@ -915,7 +919,7 @@
   memcpy(buf+8, MONTH_NAMES[tm->tm_mon], 3);
 }
 
-int tor_parse_rfc1123_time(const char *buf, time_t *t) {
+int parse_rfc1123_time(const char *buf, time_t *t) {
   struct tm tm;
   char month[4];
   char weekday[4];
@@ -949,6 +953,38 @@
   return 0;
 }
 
+void format_iso_time(char *buf, time_t t) {
+  strftime(buf, ISO_TIME_LEN+1, "%Y-%m-%d %H:%M:%S", gmtime(&t));
+}
+
+int parse_iso_time(const char *cp, time_t *t) {
+  struct tm st_tm;
+#ifdef HAVE_STRPTIME
+  if (!strptime(cp, "%Y-%m-%d %H:%M:%S", &st_tm)) {
+    log_fn(LOG_WARN, "Published time was unparseable"); return -1;
+  }
+#else
+  unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
+  if (sscanf(cp, "%u-%u-%u %u:%u:%u", &year, &month,
+                &day, &hour, &minute, &second) < 6) {
+        log_fn(LOG_WARN, "Published time was unparseable"); return -1;
+  }
+  if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
+          hour > 23 || minute > 59 || second > 61) {
+        log_fn(LOG_WARN, "Published time was nonsensical"); return -1;
+  }
+  st_tm.tm_year = year;
+  st_tm.tm_mon = month-1;
+  st_tm.tm_mday = day;
+  st_tm.tm_hour = hour;
+  st_tm.tm_min = minute;
+  st_tm.tm_sec = second;
+#endif
+  *t = tor_timegm(&st_tm);
+  return 0;
+}
+
+
 /*
  *   Low-level I/O.
  */

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.78
retrieving revision 1.79
diff -u -d -r1.78 -r1.79
--- util.h	7 Aug 2004 01:03:32 -0000	1.78
+++ util.h	7 Aug 2004 02:46:15 -0000	1.79
@@ -201,8 +201,11 @@
 int tv_cmp(struct timeval *a, struct timeval *b);
 time_t tor_timegm(struct tm *tm);
 #define RFC1123_TIME_LEN 29
-void tor_format_rfc1123_time(char *buf, time_t t);
-int tor_parse_rfc1123_time(const char *buf, time_t *t);
+void format_rfc1123_time(char *buf, time_t t);
+int parse_rfc1123_time(const char *buf, time_t *t);
+#define ISO_TIME_LEN 19
+void format_iso_time(char *buf, time_t t);
+int parse_iso_time(const char *buf, time_t *t);
 
 int write_all(int fd, const char *buf, size_t count, int isSocket);
 int read_all(int fd, char *buf, size_t count, int isSocket);