]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: move parse_time into utime_t header
authorGreg Farnum <greg@inktank.com>
Mon, 1 Jul 2013 21:57:56 +0000 (14:57 -0700)
committerYehuda Sadeh <yehuda@inktank.com>
Tue, 2 Jul 2013 20:06:06 +0000 (13:06 -0700)
Signed-off-by: Greg Farnum <greg@inktank.com>
src/include/utime.h
src/rgw/rgw_admin.cc
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_rest.cc
src/rgw/rgw_rest_log.cc

index bfbc512263af238cb6bcd93c3ceae30542d34cba..fab542736836d7fb14842e4c3ab94aaad43cb2ef 100644 (file)
 #include <math.h>
 #include <sys/time.h>
 #include <time.h>
+#include <errno.h>
 
 #include "include/types.h"
+#include "common/strtol.h"
 
 
 // --------
@@ -234,6 +236,57 @@ public:
                    bdt.tm_year + 1900, bdt.tm_mon + 1, bdt.tm_mday,
                    bdt.tm_hour, bdt.tm_min, bdt.tm_sec, usec());
   }
+
+  static int parse_date(const string& date, uint64_t *epoch, uint64_t *nsec,
+                        string *out_date=NULL, string *out_time=NULL) {
+    struct tm tm;
+    memset(&tm, 0, sizeof(tm));
+
+    const char *p = strptime(date.c_str(), "%Y-%m-%d", &tm);
+    if (p) {
+      if (*p == ' ') {
+       p++;
+       p = strptime(p, " %H:%M:%S", &tm);
+       if (!p)
+         return -EINVAL;
+        if (nsec && *p == '.') {
+          ++p;
+          unsigned i;
+          char buf[10]; /* 9 digit + null termination */
+          for (i = 0; (i < sizeof(buf) - 1) && isdigit(*p); ++i, ++p) {
+            buf[i] = *p;
+          }
+          for (; i < sizeof(buf) - 1; ++i) {
+            buf[i] = '0';
+          }
+          buf[i] = '\0';
+          string err;
+          *nsec = (uint64_t)strict_strtol(buf, 10, &err);
+          if (!err.empty()) {
+            return -EINVAL;
+          }
+        }
+      }
+    } else {
+      return -EINVAL;
+    }
+    time_t t = timegm(&tm);
+    if (epoch)
+      *epoch = (uint64_t)t;
+
+    if (out_date) {
+      char buf[32];
+      strftime(buf, sizeof(buf), "%F", &tm);
+      *out_date = buf;
+    }
+    if (out_time) {
+      char buf[32];
+      strftime(buf, sizeof(buf), "%T", &tm);
+      *out_time = buf;
+    }
+
+    return 0;
+  }
 };
 WRITE_CLASS_ENCODER(utime_t)
 
index 810b18c6984e34047dd5af07331dbe5492e42493..23d6e72cfb5566b0bc529dce67fd5534a9ea7e06 100644 (file)
@@ -605,7 +605,7 @@ static int parse_date_str(const string& date_str, utime_t& ut)
   uint64_t nsec = 0;
 
   if (!date_str.empty()) {
-    int ret = parse_date(date_str, &epoch, &nsec);
+    int ret = utime_t::parse_date(date_str, &epoch, &nsec);
     if (ret < 0) {
       cerr << "ERROR: failed to parse date: " << date_str << std::endl;
       return -EINVAL;
@@ -1375,7 +1375,7 @@ int main(int argc, char **argv)
       return usage();
     }
     string parsed_date, parsed_time;
-    int r = parse_date(date, NULL, NULL, &parsed_date, &parsed_time);
+    int r = utime_t::parse_date(date, NULL, NULL, &parsed_date, &parsed_time);
     if (r < 0) {
       cerr << "failure parsing date: " << cpp_strerror(r) << std::endl;
       return 1;
@@ -1569,14 +1569,14 @@ next:
     int ret;
     
     if (!start_date.empty()) {
-      ret = parse_date(start_date, &start_epoch, NULL);
+      ret = utime_t::parse_date(start_date, &start_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse start date" << std::endl;
         return 1;
       }
     }
     if (!end_date.empty()) {
-      ret = parse_date(end_date, &end_epoch, NULL);
+      ret = utime_t::parse_date(end_date, &end_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse end date" << std::endl;
         return 1;
@@ -1605,7 +1605,7 @@ next:
 
 
     if (!start_date.empty()) {
-      ret = parse_date(start_date, &start_epoch, NULL);
+      ret = utime_t::parse_date(start_date, &start_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse start date" << std::endl;
         return 1;
@@ -1613,7 +1613,7 @@ next:
     }
 
     if (!end_date.empty()) {
-      ret = parse_date(end_date, &end_epoch, NULL);
+      ret = utime_t::parse_date(end_date, &end_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse end date" << std::endl;
         return 1;
index fbb918736c6ae3de74b1fe0f0b0f4455814703b9..a17f1c2017e5bef2943ed131b1198d7ebde8d4e7 100644 (file)
@@ -387,60 +387,6 @@ int parse_time(const char *time_str, time_t *time)
   return 0;
 }
 
-int parse_date(const string& date, uint64_t *epoch, uint64_t *nsec, string *out_date, string *out_time)
-{
-  struct tm tm;
-
-  memset(&tm, 0, sizeof(tm));
-  if (nsec)
-    *nsec = 0;
-
-  const char *p = strptime(date.c_str(), "%Y-%m-%d", &tm);
-  if (p) {
-    if (*p == ' ') {
-      p++;
-      p = strptime(p, " %H:%M:%S", &tm);
-      if (!p)
-       return -EINVAL;
-      if (nsec && *p == '.') {
-        ++p;
-        unsigned i;
-        char buf[10]; /* 9 digit + null termination */
-        for (i = 0; (i < sizeof(buf) - 1) && isdigit(*p); ++i, ++p) {
-          buf[i] = *p;
-        }
-        for (; i < sizeof(buf) - 1; ++i) {
-          buf[i] = '0';
-        }
-        buf[i] = '\0';
-        string err;
-        *nsec = (uint64_t)strict_strtol(buf, 10, &err);
-        if (!err.empty()) {
-          return -EINVAL;
-        }
-      }
-    }
-  } else {
-    return -EINVAL;
-  }
-  time_t t = timegm(&tm);
-  if (epoch)
-    *epoch = (uint64_t)t;
-
-  if (out_date) {
-    char buf[32];
-    strftime(buf, sizeof(buf), "%F", &tm);
-    *out_date = buf;
-  }
-  if (out_time) {
-    char buf[32];
-    strftime(buf, sizeof(buf), "%T", &tm);
-    *out_time = buf;
-  }
-
-  return 0;
-}
-
 /*
  * calculate the sha1 value of a given msg and key
  */
index 84d1075b77767c39bced0bb059cf8ad62612bb14..c03ee74367ff8db2bdff07e2948b9b8dd7332e65 100644 (file)
@@ -1204,7 +1204,6 @@ extern int parse_key_value(string& in_str, const char *delim, string& key, strin
 extern int parse_time(const char *time_str, time_t *time);
 extern bool parse_rfc2616(const char *s, struct tm *t);
 extern bool parse_iso8601(const char *s, struct tm *t);
-extern int parse_date(const string& date, uint64_t *epoch, uint64_t *nsec, string *out_date = NULL, string *out_time = NULL);
 extern string rgw_trim_whitespace(const string& src);
 extern string rgw_trim_quotes(const string& val);
 
index 944a2c917409d4681d8dbc545db315af45fa84cd..04223dd00ce407203a9ac8cade57fe45eacdacae 100644 (file)
@@ -575,13 +575,13 @@ int RESTArgs::get_time(struct req_state *s, const string& name, const utime_t& d
     return 0;
   }
 
-  uint64_t epoch, usec;
+  uint64_t epoch, nsec;
 
-  int r = parse_date(sval, &epoch, &usec);
+  int r = utime_t::parse_date(sval, &epoch, &nsec);
   if (r < 0)
     return r;
 
-  *val = utime_t(epoch, usec);
+  *val = utime_t(epoch, nsec);
 
   return 0;
 }
@@ -599,7 +599,7 @@ int RESTArgs::get_epoch(struct req_state *s, const string& name, uint64_t def_va
     return 0;
   }
 
-  int r = parse_date(date, epoch, NULL);
+  int r = utime_t::parse_date(date, epoch, NULL);
   if (r < 0)
     return r;
 
index cb24251ac24107513bb5c2f30c894bb193990f7f..2dc6071321bd317f3aec41f48ee10ee78224c310 100644 (file)
@@ -28,7 +28,7 @@ static int parse_date_str(string& in, utime_t& out) {
   uint64_t nsec = 0;
 
   if (!in.empty()) {
-    if (parse_date(in, &epoch, &nsec) < 0) {
+    if (utime_t::parse_date(in, &epoch, &nsec) < 0) {
       dout(5) << "Error parsing date " << in << dendl;
       return -EINVAL;
     }