Skip to content

Instantly share code, notes, and snippets.

@soniakeys
Created June 19, 2015 12:59
Show Gist options
  • Save soniakeys/ea722017e19b29402216 to your computer and use it in GitHub Desktop.
Save soniakeys/ea722017e19b29402216 to your computer and use it in GitHub Desktop.
ISO 8601 Durations
const (
decPart = `\d*[.,]\d*`
intPart = `\d+`
decS = `(?:(?P<decS>` + decPart + `)S)`
intS = `(?:(?P<intS>` + intPart + `)S)?`
sPat = `(?:` + decS + `|` + intS + `)`
decMin = `(?:(?P<decMin>` + decPart + `)M)`
intMin = `(?:(?P<intMin>` + intPart + `)M)?`
minPat = `(?:` + decMin + `|(?:` + intMin + sPat + `))`
decH = `(?:(?P<decH>` + decPart + `)H)`
intH = `(?:(?P<intH>` + intPart + `)H)?`
hPat = `(?:T(?:` + decH + `|(?:` + intH + minPat + `)))?`
decD = `(?:(?P<decD>` + decPart + `)D)`
intD = `(?:(?P<intD>` + intPart + `)D)?`
dPat = `(?:` + decD + `|(?:` + intD + hPat + `))`
decW = `(?:(?P<decW>` + decPart + `)W)`
intW = `(?:(?P<intW>` + intPart + `)W)?`
wPat = `(?:` + decW + `|(?:` + intW + dPat + `))`
decMon = `(?:(?P<decMon>` + decPart + `)M)`
intMon = `(?:(?P<intMon>` + intPart + `)M)?`
monPat = `(?:` + decMon + `|(?:` + intMon + wPat + `))`
decY = `(?:(?P<decY>` + decPart + `)Y)`
intY = `(?:(?P<intY>` + intPart + `)Y)?`
yPat = `(?:` + decY + `|(?:` + intY + monPat + `))`
durPat = `^P` + yPat + `$`
)
@soniakeys
Copy link
Author

some experimentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment