检测傍晚/夜晚 - NSDate - iOS

时间:2015-05-22 17:26:24

标签: ios objective-c time nsdate nscalendar

我有一个iOS应用程序。该应用程序的其中一个部分显示了针对特定位置的简单小天气预报。作为此预测的一部分,该应用程序还会显示一些图像。图像是基于日期的,在这种情况下,它们将具有“太阳”图像或夜晚图像,在这种情况下,它们将具有“月亮图标”。

无论如何,我已经提出了一个“拙劣的工作”方法,可以判断它是上午/下午还是晚上。 (这也是本赛季的因素)。

然而,我不确定它是否有任何好处,如果有正式的解决方法,我会徘徊?也许有一些Apple API?有什么更好的方法来解决我的问题?这是我的代码:

注意:请确保滚动我的代码,有很多。

 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");
 }

谢谢你的时间,Dan。

2 个答案:

答案 0 :(得分:3)

以防您被允许使用Weather API。

您可以使用以下库:OpenWeatherMapAPI

它以下列格式提供数据:

NSDictionary看起来像这样(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
}

答案 1 :(得分:0)

-(void)ShowTimeMessage
{
// For calculating the current date
NSDate *date = [NSDate date];

// Make Date Formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh a EEEE"];

// hh for hour mm for minutes and a will show you AM or PM
NSString *str = [dateFormatter stringFromDate:date];
// NSLog(@"%@", str);

// Sperate str by space i.e. you will get time and AM/PM at index 0 and 1 respectively
NSArray *array = [str componentsSeparatedByString:@" "];

// Now you can check it by 12. If < 12 means Its morning > 12 means its evening or night

NSString *message;
NSString *timeInHour;
NSString *am_pm;

NSString *DayOfWeek;
if (array.count>2)
{
    // am pm case
    timeInHour = array[0];
    am_pm = array[1];
    DayOfWeek  = array[2];
}
else if (array.count>1)
{
    // 24 hours case
    timeInHour = array[0];
    DayOfWeek = array[1];
}

if (am_pm)
{

    if ([timeInHour integerValue]>=4 && [timeInHour integerValue]<=9 && [am_pm isEqualToString:@"AM"])
    {
        message = [NSString stringWithFormat:@"Morning"];
    }
    else if (([timeInHour integerValue]>=10 && [timeInHour integerValue]!=12 && [am_pm isEqualToString:@"AM"]) || (([timeInHour integerValue]<4 || [timeInHour integerValue]==12) && [am_pm isEqualToString:@"PM"]))
    {
        message = [NSString stringWithFormat:@"Afternoon"];
    }
    else if ([timeInHour integerValue]>=4 && [timeInHour integerValue]<=9 && [am_pm isEqualToString:@"PM"])
    {
        message = [NSString stringWithFormat:@"Evening"];

    }
    else if (([timeInHour integerValue]>=10 && [timeInHour integerValue]!=12 && [am_pm isEqualToString:@"PM"]) || (([timeInHour integerValue]<4 || [timeInHour integerValue]==12) && [am_pm isEqualToString:@"AM"]))
    {
        message = [NSString stringWithFormat:@"Night"];
    }

}
else
{
    if ([timeInHour integerValue]>=4 && [timeInHour integerValue]<10)
    {
        message = [NSString stringWithFormat:@"Morning"];

    }
    else if ([timeInHour integerValue]>=10 && [timeInHour integerValue]<16)
    {
        message = [NSString stringWithFormat:@"Afternoon"];

    }
    else if ([timeInHour integerValue]>=16 && [timeInHour integerValue]<22)
    {
        message = [NSString stringWithFormat:@"Evening"];

    }
    else
    {
        message = [NSString stringWithFormat:@"Night"];

    }
}

if (DayOfWeek)
{
    _timeLbl.text=[NSString stringWithFormat:@"%@ %@",DayOfWeek,message];
}

}