Smalltalk中有没有检查两个时间跨度交集的功能?

mag*_*a93 5 timespan smalltalk

Smalltalk 中是否有一个内置函数可以将两个时间跨度作为输入,并检查它们是否相交?

例如:两个时间跨度 2018/01/01-2018/01/05 和 2018/01/03-2018/01/10 确实相交,并且此函数应输出 true。

更新:我实现了以下方法:

checkIntersection: aTimespan
"returns true if two timespans overlap and false if not"
| start1 end1 start2 end2 |
start1 := self start.
end1 := self end.
start2 := aTimespan start.
end2 := aTimespan end.
(start1 = start2)
    ifTrue: [ ^true ].
(start1 = end2)
    ifTrue: [ ^true ].
(end1 = start2)
    ifTrue: [ ^true ].
(end1 = end2)
    ifTrue: [ ^true ].
(start2 < start1 and: [ (start1 < end2) ])
    ifTrue: [ ^true ].
(start2 < end1 and: [ (end1 < end2) ])
    ifTrue: [ ^true ].  
^false
Run Code Online (Sandbox Code Playgroud)

它可以工作,但相当混乱,尤其是 if 和: 语句。

Lea*_*lia 5

在Squeak中有以下方法

Timespan >> #intersection: aTimespan

     "Return the Timespan both have in common, or nil"

     | aBegin anEnd |
     aBegin := self start max: aTimespan start.
     anEnd := self end min: aTimespan end.
     anEnd < aBegin ifTrue: [^nil].

     ^ self class starting: aBegin ending: anEnd
Run Code Online (Sandbox Code Playgroud)

因此,您可以计算交集,然后检查nil或调整此代码以获得您想要的结果。

作为旁注,我建议使用选择器#intersects:而不是#checkIntersection: