Search code examples
iosobjective-ctimensdatenscalendar

Detect when it is evening/night - NSDate - iOS


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.


Solution

  • Just in case you are allowed to use Weather API.

    You can use the following library: OpenWeatherMapAPI

    It gives the data in the following format:

    NSDictionary that looks like this (json):

    {
        coord: {
            lon: 10.38831,
            lat: 55.395939
        },
        sys: {
            country: "DK",
            sunrise: 1371695759, // this is an NSDate
            sunset: 1371758660   // this is also converted to a NSDate
        },
        weather: [
            {
                id: 800,
                main: "Clear",
                description: "Sky is Clear",
                icon: "01d"
            }
        ],
        base: "global stations",
        main: {
            temp: 295.006,      // this is the the temperature format you´ve selected
            temp_min: 295.006,  //                 --"--
            temp_max: 295.006,  //                 --"--
            pressure: 1020.58,
            sea_level: 1023.73,
            grnd_level: 1020.58,
            humidity: 80
        },
        wind: {
            speed: 6.47,
            deg: 40.0018
        },
        clouds: {
            all: 0
        },
        dt: 1371756382,
        id: 2615876,
        name: "Odense",
        cod: 200
    }