How can I compare times in an array? - arrays

I am building a bus arrival time application. It needs a function to which two arguments are passed: a current stop which the user is at, and a destination stop which the user wants to go to. The arrival times are hard-coded, and there are no "live arrival" times of any sort.
The problem I am having is trying to compare the times and work out when the next bus is arriving. The times are stored in an array and cannot be changed.
For example, if the array is as follows: ["08:00", "23:00", "01:00", "04:00"]
and also, let us say the current time is "16:00", the time returned by the function is "23:00". Simple, right? I have coded this bit already with an extension class which can be found in my Pastebin.
However, the problem arises when the time passes into "the next day", so if the time is "00:00", I don't know how to return "01:00", since my function will only return the first time in the array ("08:00") since "00:00" is lower than "08:00" .
import UIKit
// Replace the variable currentTime with a value of "00:00" and see how my function returns "08:17" which is wrong. I want the function to return "00:03" since it is the next time in the array. Or if the current time is "01:00" it should return "01:03". BUT, if the current time is "01:04" or greater, it should return the first time in the array "08:17"
// Hard coded bus arrival times
let array: [String] = ["08:17", "08:37", "08:57", "09:21", "09:51", "10:21", "10:51", "11:21", "11:51", "12:21", "12:51", "13:21", "13:51", "14:21", "14:51", "15:21", "15:51", "16:21", "16:51", "17:21", "17:51", "18:21", "18:51", "19:21", "19:51", "21:03", "22:03", "23:03", "00:03", "01:03"]
// Date object stuff
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let date = Date()
let calender = Calendar.current
let components = calender.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
let year = components.year
let month = components.month
let day = components.day
let hour = components.hour
let minute = components.minute
let second = components.second
// Returns current time in a "HH:mm:ss" format
func getTimeString() -> String {
var minuteAsString = String(minute!)
if minute! < 10 {
minuteAsString = "0\(String(minute!))"
}
let timeString = String(hour!) + ":" + minuteAsString
return timeString
}
func getNextBus(_ currentStop: String,_ destinationStop: String) -> String {
var listToUse: [String] = []
let currentTime = getTimeString()
print(currentTime)
switch (currentStop, destinationStop) {
case ("stop1", "stop2"):
listToUse = array
default: ()
}
print(listToUse)
for busTime in listToUse {
if currentTime < busTime {
return busTime
}
}
return "Error! No time found."
}
print(getNextBus("stop1", "stop2"))
// Time class which allows times to be compared and equated
class Time: Comparable, Equatable {
init(_ date: Date) {
//get the current calender
let calendar = Calendar.current
//get just the minute and the hour of the day passed to it
let dateComponents = calendar.dateComponents([.hour, .minute], from: date)
//calculate the seconds since the beggining of the day for comparisions
let dateSeconds = dateComponents.hour! * 3600 + dateComponents.minute! * 60
//set the varibles
secondsSinceBeginningOfDay = dateSeconds
hour = dateComponents.hour!
minute = dateComponents.minute!
}
init(_ hour: Int, _ minute: Int) {
//calculate the seconds since the beggining of the day for comparisions
let dateSeconds = (hour * 3600 + minute * 60)
//set the variables
secondsSinceBeginningOfDay = dateSeconds
self.hour = hour
self.minute = minute
}
var hour : Int
var minute: Int
var date: Date {
//get the current calender
let calendar = Calendar.current
//create a new date components.
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
return calendar.date(byAdding: dateComponents, to: Date())!
}
/// the number or seconds since the beginning of the day, this is used for comparisions
public let secondsSinceBeginningOfDay: Int
static func < (lhs: Time, rhs: Time) -> Bool {
return lhs.secondsSinceBeginningOfDay < rhs.secondsSinceBeginningOfDay
}
}```

Suppose we have this array:
let array = ["08:00", "23:00", "01:00", "04:00"]
A more convenient way of dealing with "bus times" would be to define a struct like so:
struct BusTime: Comparable, CustomStringConvertible {
let hour : Int
let minute : Int
static func < (lhs: BusTime, rhs: BusTime) -> Bool {
return (lhs.hour, lhs.minute) < (rhs.hour, rhs.minute)
}
var description: String {
get {
let formatter = NumberFormatter()
formatter.minimumIntegerDigits = 2
return formatter.string(for: hour)! + ":" + formatter.string(for: minute)!
}
}
}
N.B: In the rest of the answer I'll be force-unwrapping for brevity)
Let's create a sorted array of BusTimes:
let busTimes: [BusTime] = array.map { str in
return BusTime(hour: Int(str.prefix(2))!, minute: Int(str.suffix(2))!)
}
var sortedBusTimes = busTimes.sorted()
Let's also define a variable nextBus which represents the next bus time:
var nextBus: BusTime = sortedBusTimes[0]
Now, let's create a time that corresponds to say the current date:
let nowComps = Calendar.current.dateComponents([.hour, .minute], from: Date())
let now = BusTime(hour: nowComps.hour!, minute: nowComps.minute!)
With binary search, we'll be able to find the next bus time in O(log(n)):
var low = sortedBusTimes.startIndex
var high = sortedBusTimes.endIndex
while low < high {
let middle = low + (high - low)/2
let middleTime = sortedBusTimes[middle]
if middleTime == now {
low = middle
break
} else if middleTime < now {
low = middle + 1
} else if now < middleTime {
high = middle
}
}
if low != sortedBusTimes.endIndex, high != 0 {
nextBus = sortedBusTimes[low]
}
The definition middle could be simpler this way:
let middle = low + (high - low)/2
But take this article into consideration.
Finally, let's check:
print(nextBus)
At the time of writing this answer, it is 17:52. So the result printed in the console is:
23:00

Related

swift check date is today or tomorrow

I have an integer array which is a collection of today's and tomorrow's dates, I want to separate the integer array based on the type of day
let dateCollection = [
1633722900,
1633730500,
1633754910,
1633758913,
1633820400,
1633824000,
1633827600,
1633831200,
1633834800,
1633838400,
1633842000
]
expected result
let today: [Int] = [
1633722900,
1633730500,
1633754910,
1633758913
]
let tomorrow: [Int] = [
1633820400,
1633824000,
1633827600,
1633831200,
1633834800,
1633838400,
1633842000
]
what should i do to separate them, i have made an extension to convert the integer to date or vice versa, and display it as a time, i already create the date to time extension too
func getTimetringFromINT() -> String {
let date = Date(timeIntervalSince1970: TimeInterval(self))
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "id")
dateFormatter.dateFormat = "HH:mm"
return dateFormatter.string(from: date)
}
selector i made, after convert the date to Time string
You can use Calendar.current.ordinality to compare the day of the year for the various dates. Here's an example that generates dates from today and tomorrow and then filters them back into separate arrays:
let today = Date()
let todayInts = Array(0..<10).map { today.timeIntervalSince1970 + TimeInterval($0) }
print("Today:", todayInts,"\n")
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: today)!
let tomorrowInts = Array(0..<10).map { tomorrow.timeIntervalSince1970 + TimeInterval($0) }
print("Tomorrow:", tomorrowInts,"\n")
let allInts = todayInts + tomorrowInts
let todayDayOfYear = Calendar.current.ordinality(of: .day, in: .year, for: today)!
let filteredTodayInts = allInts.filter { Calendar.current.ordinality(of: .day, in: .year, for: Date(timeIntervalSince1970: $0)) == todayDayOfYear }
print("Filtered today:", filteredTodayInts,"\n")
let tomorrowDayOfYear = todayDayOfYear + 1
let filteredTomorrowInts = allInts.filter { Calendar.current.ordinality(of: .day, in: .year, for: Date(timeIntervalSince1970: $0)) == tomorrowDayOfYear }
print("Filtered tomorrow:", filteredTomorrowInts,"\n")
An efficient way to do this would be to calculate the int value of midnight between today and tomorrow and then split the array based on that value
let calendar = Calendar.current
var today = [Int]()
var tomorrow = [Int]()
if let midnight = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: .now))?.timeIntervalSince1970 {
let limit = Int(midnight)
dateCollection.forEach { $0 < limit ? today.append($0) : tomorrow.append($0) }
}
Another approach is to get the start date of the next day (regardless of daylight saving time changes) with the Calendar API and then partition the array
let startOfTomorrow = Calendar.current.nextDate(after: Date(),
matching: DateComponents(hour: 0),
matchingPolicy: .nextTime)!
.timeIntervalSince1970
let splitIndex = dateCollection.partition{ $0 < Int(startOfTomorrow) }
let tomorrowDates = dateCollection[..<splitIndex]
let todayDates = dateCollection[splitIndex...]
To be able to run this declare dateCollection as var
By Getting StartOfDay of any Time, we can classify all Date that shares the same StartOfDay.
extension Date {
var morning: Date { Calendar.current.startOfDay(for: self) }
var nextMorning: Date { Calendar.current.date(byAdding: .day, value: 1, to: self)!.morning }
}
extension Int {
var since1970: Date { Date(timeIntervalSince1970: TimeInterval(self)) }
}
In this case:
let todayMorning = Date().morning // Date().advanced(by: 3600*24 * -19).morning
let tomorrowMorning = Date().nextMorning // Date().advanced(by: 3600*24 * -19).nextMorning
let dateCollection = [ ... ]
let today = dateCollection.filter{ $0.since1970.morning == todayMorning }
let tomorrow = dateCollection.filter{ $0.since1970.morning == tomorrowMorning }

Set up a repeating 24:00:00 countdown timer which stops and resets at 00:00:00

Right now im getting a countdown timer which actually has the form "24:00:00". The problem is that the timer keeps running after being at "00:00:00" and starts counting down negative numbers. What i want is the timer to stop at 0 and repeat at 24 hours. My problem is that the time is of the Data.type Date(). so i don't know how to explain with an if statement that the textlabel should change if the countdown timer == 0. I hope you understand the problem
struct ContentView: View {
// Timer
func countDownString(from referenceDate: Date, until nowDate: Date) -> String
{
let components = calender.dateComponents([.hour, .minute, .second], from: nowDate, to: referenceDate)
return String(format: "%02dh:%02dm:%02ds",
components.hour ?? 00,
components.minute ?? 00,
components.second ?? 00)
}
#State var nowDate: Date = Date()
let referenceDate: Date = Date().addingTimeInterval(20)
let calender = Calendar(identifier: .gregorian)
let formatter = DateFormatter()
var timer: Timer {
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) {_ in
self.nowDate = Date()
}
}
var body: some View {
Text(countDownString(from: referenceDate, until: nowDate)).font(.largeTitle).onAppear(perform: {
_ = self.timer
})
}
}

Reading Data from Firebase into Arrays

Reading snapshots from firebase is fairly simple, although transferring the information to arrays is more complicated. I have this snapshot
Snap (01-08-2019) {
Sleep = "6.25 hrs";
"Time Uploaded" = "07:10 AM";
}
Snap (01-09-2019) {
Sleep = "6.72 hrs";
"Time Uploaded" = "07:19 AM";
}
Snap (01-10-2019) {
Sleep = "6.55 hrs";
"Time Uploaded" = "07:10 AM";
}
How would I be able to make one array for the date, one for the sleep, and one for the time uploaded.
I think you should reconsider how you store your data in firebase. To Look something similar to this.
Also I would consider to create a data model for day that looks something like this.
class Day {
var date: String
var sleep: String
var timeUploaded: String
init(date: String, sleep: String, timeUploaded: String) {
self.date = date
self.sleep = sleep
self.timeUploaded = timeUploaded
}
}
Then you can just fetch your snapshots like this.
var days = [Day]()
private func fetchDays() {
print(days.count)
let ref = Database.database().reference().child("days")
ref.observeSingleEvent(of: .value) { (snapshot) in
guard let days = snapshot.value as? [String: Any] else { return }
for (_,value) in days.enumerated() {
guard let dayDict = value.value as? [String: String] else { return }
let date = dayDict["date"] ?? ""
let sleep = dayDict["sleep"] ?? ""
let timeUploaded = dayDict["time_uploaded"] ?? ""
//If you really want 3 different arrays just add them here
// dateArray.append(date) and so on for the other two arrays
let day = Day(date: date, sleep: sleep, timeUploaded: timeUploaded)
self.days.append(day)
}
print(self.days.count)
}
}
}
Hope this helps. Couldn't comment to ask how your data was structured.
I would suggest not keeping the data in different arrays, it may be better to store the data from each node within a class, and then keep an array of those classes.
Let's start with a class that keeps all of the data
class ChronoClass {
var node_id = ""
var sleep = ""
var time_uploaded = ""
init(withSnapshot: DataSnapshot) {
let nodeId = withSnapshot.key
let someSleep = withSnapshot.childSnapshot(forPath: "sleep").value as? String ?? ""
let someTime = withSnapshot.childSnapshot(forPath: "time_uploaded").value as? String ?? ""
self.node_id = nodeId
self.sleep = someSleep
self.time_uploaded = someTime
}
}
and then a class array to keep all of the classes
var sleepArray = [ChronoClass]()
and finally the code to read in each node, populate the class and store the classes in an array.
func readFirebaseDataAndPopulateArray() {
let sleepNode = self.ref.child("sleep_node")
sleepNode.observeSingleEvent(of: .value, with : { snapshot in
for child in snapshot.children {
let snap = child as! DataSnapshot
let aChrono = ChronoClass(withSnapshot: snap)
self.sleepArray.append(aChrono)
}
for x in self.sleepArray { //just prints out what's in the array
print(x.node_id, x.sleep, x.time_uploaded)
}
})
}
and the output based on your structure
01-08-2019 6.25 hrs 07:10 AM
01-09-2019 6.72 hrs 07:19 AM
01-10-2019 6.55 hrs 07:10 AM
The advantage with using a class is you can sort, search, extrapolate or do a variety of other functions on the objects instead of working with three separate arrays.

Swift display previous entered item in array

I am trying to display the pervious entered exercise attributes in the current workout. If I go to the pervious workout everything shows up but when I go to my current workout the previous exercise attributes don't show and the date label only shows today's date and not the previous workout date. Here are the two functions for the issue. Let me know if i need to post more.
func lastWorkout() -> Workout? {
if let client = currentClient(), let currentWorkout = currentWorkout(), let workouts = client.workouts as? Set<Workout> {
// get all this client's workouts in cronological order
let sortedWorkouts = workouts.sorted { (one, two) -> Bool in
let scheduledTimeOfOne = one.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
let scheduledTimeOfTwo = two.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
return scheduledTimeOfOne > scheduledTimeOfTwo
}
// get the index of this workout
let indexOfTodaysWorkout = sortedWorkouts.index(of: currentWorkout) ?? 0
// go back one workout to find the last workout completed
let lastWorkout: Workout? = (indexOfTodaysWorkout - 1) < 0 ? nil : sortedWorkouts[indexOfTodaysWorkout - 1]
// and return
return lastWorkout
}
return nil
}
/// Last Exercise Info to load previous exercise data
func lastExercise() -> Exercise? {
guard let selectedExercise = currentExerciseInfo() else{
return nil
}
if let exercises = lastWorkout()?.exercises as? Set<Exercise>, let pastExercise = exercises.first(where: { $0.exerciseInfo == selectedExercise }) {
return pastExercise
}
return nil
}
So the array count was off in last workout function. Here is what the working function looks like. I am still not displaying the proper date. it just gives today's date.
func lastWorkout() -> Workout? {
if let client = currentClient(), let currentWorkout = currentWorkout(), let workouts = client.workouts as? Set<Workout> {
// get all this client's workouts in cronological order
let sortedWorkouts = workouts.sorted { (one, two) -> Bool in
let scheduledTimeOfOne = one.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
let scheduledTimeOfTwo = two.appointment?.scheduled ?? Date(timeIntervalSince1970: 0)
return scheduledTimeOfOne > scheduledTimeOfTwo
}
// get the index of this workout
let indexOfTodaysWorkout = sortedWorkouts.index(of: currentWorkout) ?? 0
// go back one workout to find the last workout completed
let lastWorkout: Workout? = sortedWorkouts.count < 2 ? nil : sortedWorkouts[indexOfTodaysWorkout + 1]
// and return
return lastWorkout
}
return nil
}

How to search the minimum positive value in an array?

In my code, checkTheNextTime() function's array contains the strings 00.00 to 23.59. By writing this function I want to find the nearest future time. But when I tried with timeTable(shown in code) it returns 23.30 instead of 23.32(Now is 22.24). I guess the compiler search the array right to left. How can I find the nearest future time?
var timeTable = ["09.00","10.20","10.35","11.55","12.00","12.40","13.20","14.40","14.50", "23.00", "23.30", "23.31", "23.32"]
func checkTheNextTime(array array: Array<String>) -> String{
var nextTime: String?
for index in array {
let generatedString:String = getTimeAsMinToCheck(finalTime: index)
let indexInt = Int(generatedString)
if indexInt > 0{
nextTime = index
}
}
return nextTime!
}
func getTimeAsMinToCheck(finalTime finalTime: String) -> String{
let date = NSDate()
let formatter = NSDateFormatter()
formatter.timeStyle = NSDateFormatterStyle.NoStyle
formatter.dateStyle = NSDateFormatterStyle.ShortStyle
let now = formatter.stringFromDate(date)
formatter.locale = NSLocale.systemLocale()
formatter.dateFormat = "M/dd/yy HH.mm"
let datetofinish = formatter.dateFromString("\(now) \(finalTime)")
let finishDate: NSDate = datetofinish!
let secondsFromNowToFinish = finishDate.timeIntervalSinceNow
let minutes = Int(secondsFromNowToFinish / 60)
return String(minutes)
}
Assuming 23 is the right answer (its not clear from the comments above), here a a solution using swift 2.0 and closures
map your timeTable array into an array of delta's from the current
time (invalid entries are mapped to 0)
add the minimum delta to the time now
let timeNow: Float = 22.24
let timeTable = ["09.00","10.20","10.35","11.55","12.00","12.40","13.20","14.40","14.50", "23.00", "23.30", "23.31", "23.32"]
let minDelta = timeTable
.map { Float(NSNumberFormatter().numberFromString($0) ?? 0.0) - timeNow }
.filter { $0 > 0 }
.minElement()
let nextTime = (minDelta ?? 0) + timeNow
print(nextTime) // 23.0
This code should work for your requirement:
Done in Swift 2.0:
var timeTable = ["09.00","10.20","10.35","11.55","12.00","12.40","13.20","14.40","14.50", "23.00", "23.30", "23.31", "23.32"]
func checkTheNextTime(array array: Array<String>) -> String{
let currentTime:String = getTimeAsMinToCheck(finalTime: "23.24") // set this value by calculating from current time
let currentTimeInt = Int(currentTime)// Int value of currentTime
var nextTime: String? //this will hold the nearest future value
var minDiff: Int = 24*60 //lets start with maximum value
for index in timeTable {
let generatedString:String = getTimeAsMinToCheck(finalTime: index)
let indexInt = Int(generatedString)
if (indexInt > currentTimeInt) { //checking for future time only
let timeDiff = indexInt - currentTimeInt // this will be positive
if (timeDiff < minDiff) {
minDiff = timeDiff //update minDiff as timeDiff is less than minDiff
nextTime = index
}
}
}
return nextTime!
}

Resources