4

I would like some opinions/help with creating a fairly simple function, here are the requirements:

I need to determine if the current time is day or night (Day is between 6am and 6pm). For example here is some pseudo code:

if(currentTime < 6pm && currentTime > 6am){
     return timeIsDay
}
else{
     return timeIsNight
}

This is for an IOS application in Objective C and MUST be IOS 8.0 compatible.

SJTriggs
  • 476
  • 5
  • 13
  • You will need `NSDate` formatters and maybe to possibly connect to a server to check sunset and sunrise times – Ed3121577 Sep 24 '14 at 04:55
  • Clarify what you have and what you need. Do you really want to check against 6pm and 6am? What's your definition of day and night for the purposes of your app? – rmaddy Sep 24 '14 at 04:56
  • 1
    What are you going to do when they try to run your code at the north and south poles? – paxdiablo Sep 24 '14 at 04:58
  • @paxdiablo This is for a clock-in/clock-out application for logging purposes. The orientation of the sun in the sky does not change that fact that a night shift should start between 6pm and 6am. – SJTriggs Sep 24 '14 at 05:17
  • @rmaddy Does it really matter what my definition of day and night is? The definition of day and night will be editable in the code wont it? – SJTriggs Sep 24 '14 at 05:18
  • Yes, it matters. The answer has everything to do with your answer to that question. Is it a matter of hardcoding two times? Is it based on the actual sunrise and sunset time for the current date and user's location, etc. There are huge differences in how these are calculated. – rmaddy Sep 24 '14 at 05:24
  • @rmaddy Correct me if I am wrong but I really dont think it matters? Calling [NSDate date] returns the current date/time on the local device. So say it is 11pm at the south pole, the sun is still up, however the local time which is set on the device returns 11pm. The definition for 'night shift' is a shift starting between 6pm and 6am and the local time returned by the device is 11pm. This makes the shift 'night shift' even though because of the geographic location, the sun is still in the sky. – SJTriggs Sep 24 '14 at 05:37
  • 1
    Perfect. That sort of info should have been in your question to start with. You asked about night (dark) and day (light) in your question. But based on your latest comment, your question isn't about night and day, it's about checking the current time against two fixed times to determine a shift. Being clear in your requirements is important. You knew what you meant but what you posted could be interpreted several different ways - hence some of the comments requesting more info. Enjoy. – rmaddy Sep 24 '14 at 05:46
  • @rmaddy No problems bud! You are right though, I should have given more information. I assumed that the pseudo code would have been enough information, I guess my downfall is I like to keep things simple :) – SJTriggs Sep 24 '14 at 05:48

1 Answers1

11

Try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH.mm"];
    NSString *strCurrentTime = [dateFormatter stringFromDate:[NSDate date]];

NSLog(@"Check float value: %.2f",[strCurrentTime floatValue]);
    if ([strCurrentTime floatValue] >= 18.00 || [strCurrentTime floatValue]  <= 6.00){
        NSLog(@"It's night time");
    }else{
        NSLog(@"It's day time");
    }
Deepak
  • 1,421
  • 1
  • 16
  • 20
  • What's the point of formatting the date as `HH:mm` if the call to `floatValue` is only going to look at the string up to the colon? – rmaddy Sep 24 '14 at 05:13
  • I do agree with @rmaddy I don't see a point for the formatting of the date, however this is exactly what I was trying to achieve. Great work, nice and simple. – SJTriggs Sep 24 '14 at 05:29
  • @rmaddy We were both wrong, after testing I found setting the format of the date is required, if you don't the strCurrentTime will be empty and the float value will always = 0. However instead of setting the format to "HH.mm", it only needs to be set to "HH". – SJTriggs Sep 24 '14 at 06:10
  • oh and another small bug... a time can never be greater then 18.00 while being smaller then 6.00. Instead it should be: if([strCurrentTime floatValue]<=18.00 && [strCurrentTime floatValue] >= 6.0) – SJTriggs Sep 24 '14 at 06:16
  • @SJTriggs: try to add NSLog(@"Check float value: %.2f",[strCurrentTime floatValue]); before if condition and check what value you get. you will not get 0. – Deepak Sep 24 '14 at 06:24
  • I have edited my answer to fix IF condition. i have replace && with || to allow any one true. – Deepak Sep 24 '14 at 06:26