Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created June 21, 2020 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppazos/0d90d52f139b8c12da35b064a50b98c2 to your computer and use it in GitHub Desktop.
Save ppazos/0d90d52f139b8c12da35b064a50b98c2 to your computer and use it in GitHub Desktop.
Generates a duration expression from an ISO 8601 duration pattern
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