提取时间数组中最接近当前时间的时间。大学环形巴士到达通知系统

时间:2017-01-04 12:36:27

标签: ios iphone arrays time swift3

我打算在大学内提供循环巴士通知系统。

我想在时间数组(gateArray)中提取最接近当前时间的时间,并告诉我剩下多少分钟到达。

例如,如果数组gateArray = [“07:40”,“08:00”,“08:16”,“08:32”,“08:48”,“09:04”,“09 :20" ], 如果当前时间是08:03,则阵列的输出时间是08:16而不是08:00,离开时间和当前时间之差是08:16(离开时间) - 08:03(当前时间)计算时间间隔并显示UITextView - >> “13分钟后离开”。

当前时间上午9:00>> GateArray到“09:04”提取>> 4分钟后离开“

当前时间09:05 am>> GateArray到“09:20”提取>> “15分钟后离开”

1 个答案:

答案 0 :(得分:0)

Swift 3版,增加了对第二天案件的处理:

import UIKit

struct ArrivalDataContainer {
    var  date = Date()
    var timeString = "00:00"
    var currentMinuteDifference = 0
}

let currentDate = Date()

let unitFlags: Set = Set<Calendar.Component>([.day, .month, .year])
var components = NSCalendar.current.dateComponents(unitFlags, from: currentDate)

let gateArray = ["07:40", "08:00", "08:16", "08:32", "08:48", "09:04", "09:20", "23:00"]

var arrivalArray = [ArrivalDataContainer]()

for timeString in gateArray {
    let timeAndMinutes = timeString.components(separatedBy: ":")
    components.setValue(Int(timeAndMinutes[0])!, for: .hour)
    components.setValue(Int(timeAndMinutes[1])!, for: .minute)
    let newDate = NSCalendar.current.date(from: components)
    let difference = NSCalendar.current.dateComponents(Set<Calendar.Component>([.minute]), from: currentDate, to: newDate!).minute
    let dataContainer = ArrivalDataContainer(date: newDate!, timeString: timeString, currentMinuteDifference: difference!)
    arrivalArray.append(dataContainer)
}

let minimumPositiveDifference = arrivalArray.filter{ $0.currentMinuteDifference > 0 }.sorted(by: { $0.currentMinuteDifference < $1.currentMinuteDifference }).first

if let firstWithinSameDay = minimumPositiveDifference {
    print("Departure in \(firstWithinSameDay.currentMinuteDifference) minute(s)")
}
else {
    let largestNegativeDifference = arrivalArray.sorted(by: { $0.currentMinuteDifference < $1.currentMinuteDifference }).first
    print("Next departure \(largestNegativeDifference?.timeString)")
}

Swift 2版这是原始答案。

import UIKit

struct ArrivalDataContainer {
    var  date = NSDate()
    var timeString = "00:00"
    var currentMinuteDifference = 0
}

let currentDate = NSDate()

let unitFlags: NSCalendarUnit = [.Day, .Month, .Year]
let components = NSCalendar.currentCalendar().components(unitFlags, fromDate: currentDate)

let gateArray = ["07:40", "08:00", "08:16", "08:32", "08:48", "09:04", "09:20", "23:00"]

var arrivalArray = [ArrivalDataContainer]()

for timeString in gateArray {
    let timeAndMinutes = timeString.componentsSeparatedByString(":")
    components.setValue(Int(timeAndMinutes[0])!, forComponent: .Hour)
    components.setValue(Int(timeAndMinutes[1])!, forComponent: .Minute)
    let newDate = NSCalendar.currentCalendar().dateFromComponents(components)
    let difference = NSCalendar.currentCalendar().components([.Minute], fromDate: currentDate, toDate: newDate!, options: []).minute
    let dataContainer = ArrivalDataContainer(date: newDate!, timeString: timeString, currentMinuteDifference: difference)
    arrivalArray.append(dataContainer)
}

let minimumPositiveDifference = arrivalArray.filter{ $0.currentMinuteDifference > 0 }.sort({ $0.currentMinuteDifference < $1.currentMinuteDifference })[0].currentMinuteDifference

print("Departure in \(minimumPositiveDifference) minute(s)")
  1. 将到达时间表示为日期
  2. Get times from string
  3. Get difference from current time and arrival dates
  4. 选择最小差异和打印出发间隔字符串
  5. 还需要弄清楚第二天的第一个是否是最接近的一个。它可能是微分差最大的那个。