I have an iOS app. One of the sections of the app shows a simple little weather forecast for a specific location. As part of this forecast, the app also shows some images. The images are either day based, in which case they will have a 'sun' image or night based, in which case they will have a 'moon icon'.
Anyway, I have come up with a 'botch-job' little method which can figure out if it is morning/afternoon or evening time. (It also factors in the season as well).
However I am not sure if it is any good and I am wandering if there is an official way of figuring this out? Maybe with some Apple API? What is a better way of approaching my problem? Here is my code:
Note: make sure you scroll through my code, there is quite a bit.
NSDateComponents *component = [[NSCalendar currentCalendar] components:(NSCalendarUnitMonth | NSCalendarUnitHour) fromDate:[NSDate date]];
NSInteger hours = [component hour];
NSInteger month = [component month];
NSLog(@"\n\nHOUR IS: %ld", (long)hours);
NSLog(@"MONTH IS: %ld", (long)month);
NSInteger eveningHour = 0;
switch (month) {
case 12: case 1: case 2:
// It is winter time... so days are very short.
// December, January, February.
eveningHour = 16;
break;
case 3: case 4: case 5:
// Its spring time... days are getting a bit longer.
// March, April, May.
eveningHour = 17;
break;
case 6: case 7: case 8:
// Its summer time... so days are longer.
// June, July, August.
eveningHour = 20;
break;
case 9: case 10: case 11:
// Its fall (autumn)... days are getting shorter.
// September, October, November.
eveningHour = 17;
break;
default: break;
}
if ((hours >= 0) && (hours < 12)) {
// Morning...
// Display normal sun icon.
NSLog(@"Morning");
}
else if ((hours >= 12) && (hours < eveningHour)) {
// Afternoon...
// Display normal sun icon.
NSLog(@"Afternoon");
}
else if ((hours >= eveningHour) && (hours <= 24)) {
// Evening/Night...
// Display moon icon.
NSLog(@"Evening");
}
Thanks for your time, Dan.