Created
June 21, 2020 02:22
-
-
Save ppazos/0d90d52f139b8c12da35b064a50b98c2 to your computer and use it in GitHub Desktop.
Generates a duration expression from an ISO 8601 duration pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def p = "PYMDTHMS" | |
Random random = new Random() | |
def gen = "" | |
def is_time = false | |
def util_date = new Date() | |
p.each { c -> | |
switch (c) | |
{ | |
case 'P': | |
gen += c | |
break | |
case 'T': | |
gen += c | |
is_time = true | |
break | |
case 'Y': | |
gen += (1900 + util_date.getYear()) + 'Y' | |
break | |
case 'M': | |
if (is_time) | |
gen += random.nextInt(60) + 'M' | |
else | |
gen += (util_date.getMonth()+1) + 'M' | |
break | |
case 'D': | |
gen += util_date.getDate() + 'D' | |
break | |
case ['H', 'S']: | |
gen += random.nextInt(60) + c | |
break | |
} | |
} | |
println gen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment