Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over a year to get each days NSDate object in Swift

Hello I have a method that returns an array of times for each day.

prayTimesDate(date: NSDate, latitide : Double, longitude : Double, timeZone : Double) -> NSMutableArray

I need to iterate through a whole year or maybe a date range to get an array of times for each day in a whole year. I found alot of references in ruby and python on how to do this but I couldn't find anything for swift or objective-c. Is there any built in methods in swift that will accomplish this? If not can someone help me out as I am still new in programming. Any input is greatly appreciated.

This is the objective-c code for the method I'm linking to my swift project

- (NSMutableArray *)prayerTimesDate:(NSDate *)date latitude:(double)latitude longitude:(double)longitude andTimezone:(double)timezone
{
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];

    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];

    return [self getDatePrayerTimesForYear:year month:month day:day latitude:latitude longitude:longitude andtimeZone:timezone];
}
like image 594
gnm1978 Avatar asked Feb 12 '23 02:02

gnm1978


1 Answers

Assuming your prayerTimesDate: method is already returning the expected result, you can loop through each day of the year while repeatedly call prayerTimesDate: to get an array containing the prayer times for each day, ex:

func yearlyPrayerDatesFromCurrentDate (latitude:Double, longitude:Double, timezone:Double) -> NSMutableArray {

    // Set "date" to equal the current day
    var date:NSDate! = NSDate()

    // Increment "date" by one year to calculate the ending
    // date for the loop
    let gregorian:NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
    let dateComponents = NSDateComponents()
    dateComponents.year = 1
    let endingDate:NSDate! = gregorian.dateByAddingComponents(dateComponents, toDate: date, options: nil)

    // Create an array to hold *all* the returned 
    // results for the year
    var datesArray = NSMutableArray()

    // Loop through each date until the ending date is
    // reached
    while date.compare(endingDate) != NSComparisonResult.OrderedDescending {
        // Call your prayerTimesDate: method on the current
        // date to get that date's prayer times and add the
        // times from the returned array to the datesArray
        datesArray.addObjectsFromArray(prayerTimesDate(date, latitude: latitude, longitude: longitude, andTimezone: timezone))

        // increment the date by 1 day
        let dateComponents = NSDateComponents()
        dateComponents.day = 1
        date = gregorian.dateByAddingComponents(dateComponents, toDate: date, options: nil)
    }

    return datesArray
}
like image 68
Lyndsey Scott Avatar answered Feb 13 '23 16:02

Lyndsey Scott