如何快速检查时间重叠?

时间:2018-12-10 05:32:30

标签: ios swift time datepicker

我的视图中有4个startTime datePicker和4个endTime datePicker。 我想检查时间是否重叠。

Ex. slot 1 = 09:00 - 12:00
slot 2 = 08:00 - 11:00
slot 3 = 05:00 - 10:30
slot 4 = 11:00 - 16:00

我使用以下公式将此时间转换为十进制:

(分钟+(小时* 6))/ 10

现在我有十进制时间,所以我尝试了以下代码:

for item in validationArray! {
            let startIndexvalue:Int = item["startTime"]!
            let stopIndexvalue:Int = item["stopTime"]!

            print(startIndexvalue)
            print(stopIndexvalue)
            print(fastLinkStartTime)

            if stopIndexvalue > startIndexvalue {
                if startIndexvalue ... stopIndexvalue ~= fastLinkStartTime {

                    if startIndexvalue == 0 {
                        validationTimeDic["startTime"] = fastLinkStartTime
                        timeDic["startTime"] = hours + "0"
                    }

                    else {
                        showValidationAlert(title: NSLocalizedString("You can not select time in between existing time slot.", comment: ""), message: "")
                        DispatchQueue.main.async {
                            self.StartTime.text = "--:--"
                        }

                        validationTimeDic["startTime"] = 0
                        timeDic["startTime"] = "--:--"
                    }

                    validationArray?[currentRow] = validationTimeDic
                    self.currentObject!.validationData[currentSection.description] = validationArray

                    array?[currentRow] = timeDic
                    self.currentObject!.mondayToSundayData[currentSection.description] = array

                    print(self.currentObject!.validationData)

                }
            }

请给我任何想法吗?

3 个答案:

答案 0 :(得分:1)

您需要检查两个Date范围是否发生拍动。您可以通过检查一个ClosedRange<Date>是否与另一个

重叠来完成此操作
let isOverlapping = (startDate1...endDate1).overlaps(startDate2...endDate2)

但是对于您的情况,如果该时间(对于您的情况Int索引)与数组中的任何其他时间重叠,则不希望将此时间附加到数组中,因此在添加此新时间之前,请检查是否重叠还有一些时间。如果没有添加,则添加任何内容。

let array: [[String: Int]] = ...
let newStartIndex: Int = ...
let newEndIndex: Int = ...
let isOverlapping = array.allSatisfy { $0["startTime"] <= newEndIndex && newStartIndex <= $0["stopTime"]}

答案 1 :(得分:0)

您可以为此扩展Range类,也可以在CGRect类上使用交集。

// CGRect用法

var r1 = CGRect(x: 4.8, y: 0, width: 1.8, height: 1)
var r2 = CGRect(x: 5.8, y: 0, width: 2, height: 1)
var r3 = CGRect(x: 6, y: 0, width: 0.8, height: 1)

r1.intersection(r2).intersection(r3)

// ClosedRange用法

extension CountableClosedRange where Bound == Int {
        func intersection(_ range: CountableClosedRange<Int>) -> CountableClosedRange<Int>? {
            if(self.lowerBound >= range.upperBound) {
                return nil;
            }
            if(self.upperBound <= range.lowerBound) {
                return nil;
            }
            return CountableClosedRange(uncheckedBounds:
                (lower: self.lowerBound > range.lowerBound ?
                    self.lowerBound
                    :range.lowerBound,
                 upper: self.upperBound < range.upperBound ?
                    self.upperBound
                    :range.upperBound))
        }
    }

    func convertTime(h:Int, m: Int = 0) -> Int {
        return m+h*60;
    }
    var range1 = CountableClosedRange(uncheckedBounds: (lower: 48 , upper: 56))
    var range2 = CountableClosedRange(uncheckedBounds: (lower: 53 , upper: 55))
    var range3 = CountableClosedRange(uncheckedBounds: (lower: 45 , upper: 54))
    var range4 = CountableClosedRange(uncheckedBounds: (lower: 47 , upper: 64))
    let arr = [range2,range3,range4]
    var resultRange:CountableClosedRange<Int>? = range1
    for item in arr {
        resultRange = resultRange?.intersection(item)
    }

答案 2 :(得分:0)

  for i in 0..<validationArray!.count {
                var currentItem = validationArray![i]
                let currentStart:Int = currentItem["startTime"]!
                let currentEnd:Int = currentItem["stopTime"]!
                if currentStart != 0 && currentEnd != 0 {
                    if i < (validationArray?.count)! - 1  {
                        let nextItem = validationArray![i+1]
                        let nextStart:Int = nextItem["startTime"]!
                        let nextEnd:Int = nextItem["stopTime"]!
                        if currentEnd > currentStart {
                            if nextEnd  <  currentStart {
                                currentItem["stopTime"] = fastLinkStopTime
                                timeDic["stopTime"] = hours + "0"
                            }
                            else if nextStart >  currentEnd {
                                currentItem["stopTime"] = fastLinkStopTime
                                timeDic["stopTime"] = hours + "0"
                            }
                            else {
                                showValidationAlert(title: NSLocalizedString("Your heating time is in an already set heating interval.", comment: ""), message: "")
                                DispatchQueue.main.async {
                                    self.StopTime.text = "--:--"
                                }
                                fastLinkStopTime = 255
                                timeDic["stopTime"] = "--:--"
                                dataArray?[currentRow] = timeDic
                                self.currentObject!.mondayToSundayData[currentSection.description] = dataArray
                            }
                        }
                        else {
                            showValidationAlert(title: NSLocalizedString("Please check your entry. The start time must not be before the end time.", comment: ""), message: "")
                            DispatchQueue.main.async {
                                self.StopTime.text = "--:--"
                            }
                        }
                    }
                    else
                    {
                        break
                    }
                }
            }