Skip to content

Instantly share code, notes, and snippets.

@tyuki39
Created March 27, 2011 06:34
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 tyuki39/888987 to your computer and use it in GitHub Desktop.
Save tyuki39/888987 to your computer and use it in GitHub Desktop.
時間帯重複チェック(超ベタなコード版 & クラス使用版 & Range適用版)
// st = [時,分] が [0,0]~[24,0]の範囲内かどうかをチェック
def isValidTime = { st -> ((0..23).contains(st[0]) && (0..59).contains(st[1])) || (st[0] == 24 && st[1] == 0) }
// st1 = [時,分], st2 = [時,分] が st1 <= st2 を満たすかどうかをチェック
def isNotGreaterThan = { st1, st2 -> st1[0] < st2[0] ? true : (st1[0] == st2[0] ? st1[1] <= st2[1] : false) }
// pt = [開始時,開始分,終了時,終了分] が [開始時,開始分] <= [終了時,終了分] を満たすかどうかをチェック
def isValidPair = { pt -> isValidTime(pt[0,1]) && isValidTime(pt[2,3]) && isNotGreaterThan(pt[0,1],pt[2,3]) }
// pt1 = [開始時,開始分,終了時,終了分], pt2 = [開始時,開始分,終了時,終了分] がオーバーラップしているかどうかをチェック
// オーバーラップあり = true、なし = false
def timeDuplicationCheck = { pt1, pt2 ->
assert isValidPair(pt1) == true
assert isValidPair(pt2) == true
!(isNotGreaterThan(pt2[2,3],pt1[0,1]) || isNotGreaterThan(pt1[2,3],pt2[0,1]))
}
assert timeDuplicationCheck( [1, 0, 5, 30], [9, 0, 23, 0] ) == false // => 重複なし
assert timeDuplicationCheck( [1, 0, 2, 0], [2, 0, 3, 0] ) == false // => 重複なし
assert timeDuplicationCheck( [1, 0, 2, 1], [1, 59, 3, 0] ) == true // => 重複あり
class MyTime implements Comparable {
int hour
int minutes
MyTime(hour, minutes) {
this.hour = hour
this.minutes = minutes
assert isValid(), "The time ${this} is not valid."
}
int compareTo(that) {
(hour*60+minutes) <=> (that.hour*60+that.minutes)
}
boolean isValid() {
((0..23).contains(hour) && (0..59).contains(minutes)) || (hour == 24 && minutes == 0)
}
String toString() {
"${hour}:${minutes}"
}
}
@Newify([MyTime])
def makeDuration() { [
[
range1 : [ start: MyTime(1,0), end: MyTime(5,30) ],
range2 : [ start: MyTime(9,0), end: MyTime(23,0) ],
expected : false // => 重複なし
],
[
range1 : [ start: MyTime(1,0), end: MyTime(2,0) ],
range2 : [ start: MyTime(2,0), end: MyTime(3,0) ],
expected : false // => 重複なし
],
[
range1 : [ start: MyTime(1,0), end: MyTime(2,1) ],
range2 : [ start: MyTime(1,59), end: MyTime(3,0) ],
expected : true // => 重複あり
],
] }
def timeDuplicationCheck = { r1, r2 ->
assert r1.start <= r1.end, "The time ${r1.start} must be followed by the time ${r1.end}."
assert r2.start <= r2.end, "The time ${r2.start} must be followed by the time ${r2.end}."
!(r2.end <= r1.start || r1.end <= r2.start)
}
makeDuration().each {
assert timeDuplicationCheck(it.range1,it.range2) == it.expected
}
class MyTime implements Comparable {
int hour
int minutes
MyTime(hour, minutes) {
this.hour = hour
this.minutes = minutes
assert isValid(), "The time ${this} is not valid."
}
int compareTo(that) {
(hour*60+minutes) <=> (that.hour*60+that.minutes)
}
boolean isValid() {
((0..23).contains(hour) && (0..59).contains(minutes)) || (hour == 24 && minutes == 0)
}
String toString() {
"${hour}:${minutes}"
}
MyTime next() {
new MyTime(hour + (int)((minutes+1)/60), (minutes+1)%60)
}
MyTime previous() {
new MyTime(hour + (int)((minutes-61)/60), (minutes+59)%60)
}
}
@Newify([MyTime])
def makeDuration() { [
[
range1 : MyTime(0,0)..MyTime(5,30),
range2 : MyTime(9,0)..MyTime(23,0),
expected : false // => 重複なし
],
[
range1 : MyTime(1,0)..MyTime(2,0),
range2 : MyTime(2,0)..MyTime(3,0),
expected : false // => 重複なし
],
[
range1 : MyTime(1,0)..MyTime(2,1),
range2 : MyTime(1,59)..MyTime(3,0),
expected : true // => 重複あり
],
] }
def timeDuplicationCheck = { r1, r2 ->
assert r1.from <= r1.to, "The time ${r1.from} must be followed by the time ${r1.to}."
assert r2.from <= r2.to, "The time ${r2.from} must be followed by the time ${r2.to}."
r1.intersect(r2).size() > 1
}
makeDuration().each {
assert timeDuplicationCheck(it.range1,it.range2) == it.expected
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment