Skip to content

Instantly share code, notes, and snippets.

@nmische
Created February 13, 2014 02:47
Show Gist options
  • Save nmische/8968852 to your computer and use it in GitHub Desktop.
Save nmische/8968852 to your computer and use it in GitHub Desktop.
Example of how to find time conflicts.
@Grab(group='joda-time', module='joda-time', version='2.3')
import org.joda.time.DateTime
def eventList = [
[ name: "Event One", startDate: new DateTime(2014,2,12,11,0,0), endDate: new DateTime(2014,2,12,12,0,0) ],
[ name: "Event Two", startDate: new DateTime(2014,2,12,13,0,0), endDate: new DateTime(2014,2,12,14,0,0) ],
[ name: "Event Three", startDate: new DateTime(2014,2,12,11,30,0), endDate: new DateTime(2014,2,12,12,30,0) ],
[ name: "Event Four", startDate: new DateTime(2014,2,12,9,0,0), endDate: new DateTime(2014,2,12,10,0,0) ],
[ name: "Event Five", startDate: new DateTime(2014,2,12,9,0,0), endDate: new DateTime(2014,2,12,10,0,0) ],
[ name: "Event Six", startDate: new DateTime(2014,2,12,10,0,0), endDate: new DateTime(2014,2,12,11,0,0) ],
[ name: "Event Seven", startDate: new DateTime(2014,2,12,9,0,0), endDate: new DateTime(2014,2,12,10,0,0) ]
]
def conflicts = []
while (eventList.size() > 0) {
def e1 = eventList.pop()
for (def e2 : eventList) {
if (isConflict(e1,e2)) {
if (!conflicts.contains(e1)) conflicts.push(e1)
if (!conflicts.contains(e2)) conflicts.push(e2)
}
}
}
def isConflict(a,b) {
if (a.startDate >= b.endDate) return false
if (a.endDate <= b.startDate) return false
return true
}
conflicts.each {
println it
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment