Array of times for each day of the week in iOS - ios

I have times that I would like to compare to the current time for each day of the week. From what I've come across, it sounds like the best thing to do would be to have an array for each day of the week with the given times I want. For example:
mondayTimes(2:00:00, 5:00:00, 9:00:00, 14:00:00)
tuesdayTimes(3:00:00, 6:00:00, 10:00:00, 15:00:00)
etc...
I want to find out the given day of the week using the current date, and then depending on what day it is, use the array of times for that given day. Then use the current time to find which time is next in the array.
Basically it is like an "alarm clock" that always has set times for every day of the week.
Do I use NSStrings to populate the dates in each array and convert them so I am able to compare them to the current time? What is the best route to go about this?
Thanks!

To get the current date, use NSDate currentDate = [NSDate date];
Then, to extract the weekday: initialize a NSCalendar of your choice, then call [calendar components:NSWeekdayCalendarUnit fromDate:currentDate];, where calendar is your calendar instance.
This produces a number between 1 and 7 (for the Gregorian calendar) where 1 is Sunday and 7 is Saturday. I would then advise you put all of your times in an array of arrays, with the 1 index containing an array of your sundayTimes, 2 containing an array of mondayTimes, ... 7 containing an array of your saturdayTimes.
Then using [allTimes objectAtIndex weekday] will return a NSArray of your times. All that's left is to compare the times, which I'm sure you can figure out.

Related

Why does ordinalityOfUnit use Monday as the first day of the week when using NSCalendarUnitEra?

I'm trying to calculate the number of calendar weeks between two dates. I'm using the following code:
[cal ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitEra forDate:thenDate];
[cal ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitEra forDate:nowDate];
A date of 15-1-2017, a Sunday, results in 105192 weeks, and 16-1-2017, a Monday, results in 105193. That would indicate it is using Monday as the first day of the week. I have verified that cal.firstWeekday is 1. If I change the inUnit from NSCalendarUnitEra to NSCalendarUnitYear it works correctly. Is there a way around this?
I suppose I could subtract one day from both dates but that seems very hackie.
After much experimenting, if you make sure all dates are in GMT and you set cal timeZone to GMT it works as one would expect.

Translate NSCalendar.weekdaySymbols index to NSDateComponents.weekday

I'm writing a feature of my iOS app that involves scheduling weekly repeating local notifications on particular days of the week. I'd like to use the NSCalendar APIs properly so I don't assume things I shouldn't. But I don't see any property on NSCalendar about how many days there are in a week.
I'm displaying days of the week using weekdaySymbols, and letting the user choose which days they want notifications on. Then I'm scheduling local notifications based on that.
The real question is: How do I translate a day's index in weekdaySymbols into the value I give to NSDateComponents for weekday?
This is a bit of a simplification, but you can think of days of the week as belonging to the Gregorian calendar, independent of what calendar is actually in use. Those calendars largely govern day/month/year arithmetic, with the days of the week proceeding independently of the particulars of each calendar's number and length of months.
If you accept that as a given, then you just need to correct for the off-by-one difference between a calendar's weekdaySymbols (indexed from 0 to 6) and NSDateComponents.weekday (a value from 1 to 7).
extension DateComponents { // NSDateComponents in Swift < 3
func weekdaySymbol(in calendar: Calendar) -> String? {
guard let weekday = self.weekday else { return nil }
return calendar.weekdaySymbols[weekday - 1]
}
}

HKStatisticsCollectionQuery pulling data for each week of the month

I'm trying to create a chart that displays a month's worth of data vs the past month's data broken up by week.
For example viewing the chart today would show July vs June, there would be 5 points on each graph with the following dates:
June: 1-8, 9-15, 16-22, 23-29, 30
July: 1-8, 9-15, 16-22, 23-29, 30-31
The predicate looks like
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:self.pastStartDate endDate:self.currentEndDate options:HKQueryOptionStrictStartDate];
where self.pastStartDate is the first of last month and self.currentEndDate is the last of the current month.
We then set up the query like so
HKStatisticsCollectionQuery *query = [[HKStatisticsCollectionQuery alloc] initWithQuantityType:quantityType quantitySamplePredicate:predicate options:HKStatisticsOptionDiscreteAverage anchorDate:self.pastStartDate intervalComponents:self.interval];
So for the month, self.interval.day = 7 is set so that we pull a week's worth of data at every interval. The problem is that this interval is not calendar aware, so it doesn't know that the final data point for the last month should only have 1 day, therefore we are getting some overlap.
We have also tried self.interval.weekOfMonth = 1 but again, the interval does not know which month it is in so this is not working either.
Is there a way to create the interval based on the calendar so that it doesn't include more than the end of the month in a given interval?

Get current date past midnight, Objective C

I want to get the current date, even if the time has passed midnight. Imagine it's friday night the 6th of June 2014 - we check the date Saturday at 2 am, but we still want this to count as being friday. How would I go about this?
Let's just say we cut it at 9am the next day. I.e. we will assume previous date until the time has passed 9 am. Yes, this is software used at a nightclub, as you can imagine.
I guess this would involve something like subtracting 1 day from the current date if the hour is less than 10?
You can use interval to specific date like
[[NSDate date] dateByAddingTimeInterval:interval];
Assuming the cutoff is 9am, all you need to do is create an NSDate that's 9 hours earlier than the actual time.
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:-(9 * 3600)];
That way every day begins and ends at 9am.

same day of last month ios

Is there any way using NSCalender you can get same day of last month?
I am using a calender which shows user 1 month like the iPad calender when they click on the button I want to move to previous month but should select the same day as before.
I want to just do
[components setMonth:([components month] - 1)];
but this will create problems when I are moving from a month with 31 days to month with 30 days and selected day is 31st.
I was able to find examples for android but not iOS.
android example
Any help would be appreciated
Check out this link.
It's about adding one month, but you could probably do the same with subtracting.
Change
[dateComponents setMonth:1];
into
[dateComponents setMonth:-1];
Seems there is no obvious "right" answer and no "built-in" answer.
As Chris's "simple" idea may lead to invalid dates, you may have to handle the edge-cases.
Pseudocode to deal with day-month-year:
if month = December start with day-1-(year-1), else
day-(month-1)-year [using dateComponents]
check if this a valid date (using NSDateFormatter like in this question
repeat subtracting one day until you reach a valid date
Another idea:
prevMonthDate = startDate;
Repeat
prevMonthDate = prevMonthDate - 1 day
Until (Month(prevMonthDate) < Month(startDate) Or Year(prevMonthDate) < Year(startDate))
And (Day(prevMonthDate) <= Day(startDate))
This requires working with NSDate and NSDateComponents, check out the Date and Time Programming Guide.

Resources