Well, I refused this. Here's what I'm trying to do: I have sunrise, sunset, and current time in a specific time zone. I want to know if it is day or night, figuring out if the current time is between sunrise and sunset.
Here is what I have:
NSLog(@"%@ - %@ - %@",currTime,sunrise,sunset);
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter1 setDateFormat:@"hh:mm a"];
[formatter2 setDateFormat:@"EEE, dd MMM yyyy h:mm a z"];
NSDate *rise = [formatter1 dateFromString:sunrise];
NSDate *set = [formatter1 dateFromString:sunset];
NSDate *time = [formatter2 dateFromString:currTime];
[formatter1 release];
[formatter2 release];
unsigned int flags = NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components1 = [calendar components:flags fromDate:rise];
NSDateComponents *components2 = [calendar components:flags fromDate:set];
NSDateComponents *components3 = [calendar components:flags fromDate:time];
NSDate *Sunrise = [calendar dateFromComponents:components1];
NSDate *Sunset = [calendar dateFromComponents:components2];
NSDate *Time = [calendar dateFromComponents:components3];
NSLog(@"\nSunrise: %@ \nSunset:%@ \nTime:%@",rise,set,time);
NSLog(@"\nSunrise: %@ \nSunset:%@ \nTime:%@",Sunrise,Sunset,Time);
Here's the first conclusion:
Fri, Jun 10, 2011 4:00 a.m. SAST - 7:46 - 17:41
And here is the second (before doing this only regarding time, not date)
Sunrise: 1969-12-31 22:46:00 +0000
Sunset: 1970-01-01 08:41:00 +0000
Time: 2011-06-10 02:00:00 +0000
And finally, here is the final conclusion (note how the time went wrong?):
Sunrise: 0001-12-31 22:27:01 +0000
Sunset: 0001-01-01 08:22:01 +0000
Time: 0001-01-01 01:41:01 +0000
, , , :
+(BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate {
return (([date compare:beginDate] != NSOrderedAscending) && ([date compare:endDate] != NSOrderedDescending));
}
, , , .:/ ! ?
, NSDate . . , BoopMeister, , . :
, :
NSInteger riseHour = [components1 hour];
NSInteger setHour = [components2 hour];
NSInteger timeHour = [components3 hour];
NSLog(@"Rise: %i Set: %i Time: %i",riseHour,setHour,timeHour);
, :
: Fri, 10 Jun 2011 9:07 am CDT
: 6:33 am
: 8:32 pm
, , :
: 6 : 20 : 23
?