Reputation: 69
Hello I have trouble creating a list of regular time interval starting from a time until arriving to another. Can you help me? (Swift 2.0)
Example:
I have two times (8:00 - 23:00). I know the duration of the interval (30 minutes). The scope is to create an Array of intervals of time (8: 00-8: 30, 8:30 - 9: 00 etc). I use an object call Time(start:Int, end:Int)
Thank you
Upvotes: 2
Views: 2976
Reputation: 21144
You could create a class like this to generate the intervals like that,
struct Time {
let start: TimeInterval
let end: TimeInterval
let interval: TimeInterval
init(start: TimeInterval, interval: TimeInterval, end: TimeInterval) {
self.start = start
self.interval = interval
self.end = end
}
init(startHour: TimeInterval, intervalMinutes: TimeInterval, endHour: TimeInterval) {
self.start = startHour * 60 * 60
self.end = endHour * 60 * 60
self.interval = intervalMinutes * 60
}
var timeRepresentations: [String] {
let dateComponentFormatter = DateComponentsFormatter()
dateComponentFormatter.unitsStyle = .positional
dateComponentFormatter.allowedUnits = [.minute, .hour]
var dateComponent = DateComponents()
return timeIntervals.map { timeInterval in
dateComponent.second = Int(timeInterval)
return dateComponentFormatter.string(from: dateComponent)!
}
}
var timeIntervals: [TimeInterval]{
return Array(stride(from: start, through: end, by: interval))
}
}
You could then generate array of time representations,
let time = Time(startHour: 8, intervalMinutes: 30, endHour: 23)
print(time.timeRepresentations)
And that generates,
["8:00", "8:30", "9:00", "9:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00"]
And to retrieve the ranges, put this code at the end
let time = Time(startHour: 9, intervalMinutes: 30, endHour: 17)
let ranges = time.timeRepresentations.enumerated().compactMap { index, value -> String? in
if index + 1 < time.timeRepresentations.count {
return value + " - " + time.timeRepresentations[index + 1]
}
return nil
}
print(ranges.joined(separator: "\n"))
And the result would be ,
9:00 - 9:30
9:30 - 10:00
10:00 - 10:30
10:30 - 11:00
11:00 - 11:30
11:30 - 12:00
12:00 - 12:30
12:30 - 13:00
13:00 - 13:30
13:30 - 14:00
14:00 - 14:30
14:30 - 15:00
15:00 - 15:30
15:30 - 16:00
16:00 - 16:30
16:30 - 17:00
Upvotes: 9
Reputation: 3690
The simple array containing tuple of start-end
let startDate = NSDate()
let endDate = NSDate()
var intervalsContainer = [(start: NSDate, end: NSDate)]()
var intervalDate = startDate;
var dateComponents30Min = NSDateComponents()
dateComponents30Min.minute = 30
let calendar = NSCalendar.currentCalendar()
while(endDate.compare(intervalDate) == NSComparisonResult.OrderedDescending) {
let finDate = calendar.dateByAddingComponents(dateComponents30Min, toDate: intervalDate, options: [])!
intervalsContainer.append((intervalDate,finDate))
intervalDate = finDate
}
Upvotes: 0
Reputation: 889
you can create an schedule timer like this in your viewDidLoad method but first define a selector to your method call,
let mySelector: Selector = "time:end:"
NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: mySelector, userInfo: nil, repeats: true)
and then define your time method
func time(start: Int, end: Int) {
// code goes here, this method will called every 30 minutes.
}
so now you can create a list and add the start and end params.
Upvotes: 0