0

I am going to provide a circulation bus notification system within the university.

I want to extract the time that is closest to the current time in the time array(gateArray) and let me know how many minutes I have left to arrive.

For example, if the array gateArray = ["07:40", "08:00", "08:16", "08:32", "08:48", "09:04", "09:20"], If the current time is 08:03, the output time from the array is 08:16 instead of 08:00, and the difference between departing time and current time is 08:16 (departing time) - 08:03 (Current time) to calculate the time interval and display UITextView ->> "departing after 13 minutes".

Current time 09:00 am >> GateArray to "09:04" Extraction >> Departure after 4 minutes "

Current time 09:05 am >> GateArray to "09:20" Extraction >> "Departure after 15 minutes"

user12345625
  • 398
  • 6
  • 17

1 Answers1

0

Swift 3 version with added handling of next day case:

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 version which was the original answer.

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. Represent the arrival times as Date
  2. Get times from string
  3. Get difference from current time and arrival dates
  4. Select the minimum difference and print departure interval string

Still need to figure out if the first on the next day is the closest one. It might be the one with the most negative minute difference.

Community
  • 1
  • 1
user12345625
  • 398
  • 6
  • 17