Add getDuration() function
Related-To: PRODX-10059
Change-Id: Ic5ac382e11e22a7ecb72192f8f040ab7c0fdca49
diff --git a/src/com/mirantis/mk/Common.groovy b/src/com/mirantis/mk/Common.groovy
index 5027041..448935f 100644
--- a/src/com/mirantis/mk/Common.groovy
+++ b/src/com/mirantis/mk/Common.groovy
@@ -3,6 +3,8 @@
import static groovy.json.JsonOutput.prettyPrint
import static groovy.json.JsonOutput.toJson
+import groovy.time.TimeCategory
+
import com.cloudbees.groovy.cps.NonCPS
import groovy.json.JsonSlurperClassic
@@ -25,6 +27,31 @@
}
/**
+ * Return Duration for given datetime period with suffix
+ *
+ * @param input String in format '\d+[smhd]', to convert given number into seconds, minutes, hours
+ * and days duration respectively. For example: '7d' is for 7 days, '10m' - 10 minutes
+ * and so on. Return null if input in incorrect format
+ */
+def getDuration(String input) {
+ // Verify input format
+ if (!input.matches('[0-9]+[smhd]')) {
+ errorMsg("Incorrect input data for getDuration(): ${input}")
+ return
+ }
+ switch (input[-1]) {
+ case 's':
+ return TimeCategory.getSeconds(input[0..-2].toInteger())
+ case 'm':
+ return TimeCategory.getMinutes(input[0..-2].toInteger())
+ case 'h':
+ return TimeCategory.getHours(input[0..-2].toInteger())
+ case 'd':
+ return TimeCategory.getDays(input[0..-2].toInteger())
+ }
+}
+
+/**
* Return workspace.
* Currently implemented by calling pwd so it won't return relevant result in
* dir context