]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/util.c
DB changes for Retention Periods
[bacula/bacula] / bacula / src / lib / util.c
index aaa7d72679565902756808e9beba2bf05ffe85ab..1235e7ffb1cd203777f72d5aeafb60b51b4a3947 100644 (file)
  *
  */
 
+/*
+ * Convert a string to btime_t (64 bit seconds)
+ * Returns 0: if error
+          1: if OK, and value stored in value
+ */
+int string_to_btime(char *str, btime_t *value)
+{
+   int i, ch, len;
+   btime_t val;
+   static int  mod[] = {'*', 's', 'm', 'h', 'd', 'w', 'o', 'q', 'y', 0};
+   static int mult[] = {1,    1,  60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
+                 60*60*24*91, 60*60*24*365};
+
+   /* Look for modifier */
+   len = strlen(str);
+   ch = str[len - 1];
+   i = 0;
+   if (ISALPHA(ch)) {
+      if (ISUPPER(ch)) {
+        ch = tolower(ch);
+      }
+      while (mod[++i] != 0) {
+        if (ch == mod[i]) {
+           len--;
+           str[len] = 0; /* strip modifier */
+           break;
+        }
+      }
+   }
+   if (mod[i] == 0 || !is_a_number(str)) {
+      return 0;
+   }
+   val = (btime_t)strtod(str, NULL);
+   if (errno != 0 || val < 0) {
+      return 0;
+   }
+   *value = val * mult[i];
+   return 1;
+
+}
+
+char *edit_btime(btime_t val, char *buf)
+{
+   char mybuf[30];
+   static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
+   static char *mod[]  = {"year",  "month",  "day", "hour", "min"};
+   int i;
+   uint32_t times;
+
+   *buf = 0;
+   for (i=0; i<5; i++) {
+      times = val / mult[i];
+      if (times > 0) {
+        val = val - (btime_t)times * mult[i];
+         sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
+        strcat(buf, mybuf);
+      }
+   }
+   if (val == 0 && strlen(buf) == 0) {    
+      strcat(buf, "0 secs");
+   } else if (val != 0) {
+      sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
+      strcat(buf, mybuf);
+   }
+   return buf;
+}
+
 /*
  * Check if specified string is a number or not.
  *  Taken from SQLite, cool, thanks.