]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: date string parsing also parses seconds fraction
authorYehuda Sadeh <yehuda@inktank.com>
Sat, 29 Jun 2013 23:13:15 +0000 (16:13 -0700)
committerYehuda Sadeh <yehuda@inktank.com>
Sun, 30 Jun 2013 03:38:52 +0000 (20:38 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
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 c163a69f4263ee129c0cc1c448848f423d1b9794..8d1536fcd66dde6baba9a0c0bfb5362b3dd2c052 100644 (file)
@@ -602,16 +602,17 @@ static int read_decode_json(const string& infile, T& t)
 static int parse_date_str(const string& date_str, utime_t& ut)
 {
   uint64_t epoch = 0;
+  uint64_t nsec = 0;
 
   if (!date_str.empty()) {
-    int ret = parse_date(date_str, &epoch);
+    int ret = parse_date(date_str, &epoch, &nsec);
     if (ret < 0) {
       cerr << "ERROR: failed to parse date: " << date_str << std::endl;
       return -EINVAL;
     }
   }
 
-  ut = utime_t(epoch, 0);
+  ut = utime_t(epoch, nsec);
 
   return 0;
 }
@@ -1374,7 +1375,7 @@ int main(int argc, char **argv)
       return usage();
     }
     string parsed_date, parsed_time;
-    int r = parse_date(date, NULL, &parsed_date, &parsed_time);
+    int r = parse_date(date, NULL, NULL, &parsed_date, &parsed_time);
     if (r < 0) {
       cerr << "failure parsing date: " << cpp_strerror(r) << std::endl;
       return 1;
@@ -1568,14 +1569,14 @@ next:
     int ret;
     
     if (!start_date.empty()) {
-      ret = parse_date(start_date, &start_epoch);
+      ret = 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);
+      ret = parse_date(end_date, &end_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse end date" << std::endl;
         return 1;
@@ -1604,7 +1605,7 @@ next:
 
 
     if (!start_date.empty()) {
-      ret = parse_date(start_date, &start_epoch);
+      ret = parse_date(start_date, &start_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse start date" << std::endl;
         return 1;
@@ -1612,7 +1613,7 @@ next:
     }
 
     if (!end_date.empty()) {
-      ret = parse_date(end_date, &end_epoch);
+      ret = parse_date(end_date, &end_epoch, NULL);
       if (ret < 0) {
         cerr << "ERROR: failed to parse end date" << std::endl;
         return 1;
index 1f31981a760cfee6697ea6400fb2b65938c1d77d..fbb918736c6ae3de74b1fe0f0b0f4455814703b9 100644 (file)
@@ -13,6 +13,7 @@
 #include "common/Clock.h"
 #include "common/Formatter.h"
 #include "common/perf_counters.h"
+#include "common/strtol.h"
 #include "include/str_list.h"
 #include "auth/Crypto.h"
 
@@ -386,18 +387,38 @@ int parse_time(const char *time_str, time_t *time)
   return 0;
 }
 
-int parse_date(const string& date, uint64_t *epoch, string *out_date, string *out_time)
+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++;
-      if (!strptime(p, " %H:%M:%S", &tm))
+      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;
index 7bcd06f6a91297946f24666d291168fa4b8eb637..0fe3af43a26dc40564d6140ff0af430e0899f1fc 100644 (file)
@@ -1200,7 +1200,7 @@ 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, string *out_date = NULL, string *out_time = NULL);
+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 b1578989d4b77e99dfe287611ba2d9ecdbedfe3e..944a2c917409d4681d8dbc545db315af45fa84cd 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;
+  uint64_t epoch, usec;
 
-  int r = parse_date(sval, &epoch);
+  int r = parse_date(sval, &epoch, &usec);
   if (r < 0)
     return r;
 
-  *val = utime_t(epoch, 0);
+  *val = utime_t(epoch, usec);
 
   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);
+  int r = parse_date(date, epoch, NULL);
   if (r < 0)
     return r;
 
index 09522bb034584be65f3e0042bb2cf2317486c9be..3e4f88739525dfd1cf909078cb57d8d99b801d9d 100644 (file)
 
 static int parse_date_str(string& in, utime_t& out) {
   uint64_t epoch = 0;
+  uint64_t nsec = 0;
 
   if (!in.empty()) {
-    if (parse_date(in, &epoch) < 0) {
+    if (parse_date(in, &epoch, &nsec) < 0) {
       dout(5) << "Error parsing date " << in << dendl;
       return -EINVAL;
     }
   }
-  out = utime_t(epoch, 0);
+  out = utime_t(epoch, nsec);
   return 0;
 }