Objective-C Library for Sunrise and Sunset?

前端 未结 7 480
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 06:14

Is there an Objective-C (or C) library (that is compatible with core location) that can tell me the time of sunrise and sunset for any given calendar day?

相关标签:
7条回答
  • 2021-02-01 07:13

    Just a note.. if you use the Berkley one... well it doesn't work (in Australia as least). It does include the Paul Schlyter C code though, which is great.

    If you want it to work anywhere, best to just calc the dates in UTC.

    In SunriseAndSunset.m, replace the code from double rise; double set; as follows:

    sun_rise_set(theYear, theMonth, theDay, lon, lat, &rise, &set);
    int hours = HOURS(rise);
    int mins = MINUTES(rise);
    int sethrs = HOURS(set);
    int setmins = MINUTES(set);
    
    NSTimeInterval riseOffset = ((hours * 60) + mins) * 60;
    NSTimeInterval setOffset = ((sethrs * 60) + setmins) * 60;
    
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSString *dateStr = [NSString stringWithFormat:@"%i-%02d-%02dT00:00:00+0000", theYear, theMonth, theDay];
    NSDate *utcMidnight = [formatter dateFromString:dateStr];
    
    NSDate *utcSunrise = [utcMidnight dateByAddingTimeInterval:riseOffset];
    NSDate *utcSunset = [utcMidnight dateByAddingTimeInterval:setOffset];
    
    [formatter release];
    [gregorian release];
    
    return [NSDictionary dictionaryWithObjectsAndKeys:utcSunrise, @"sunrise", utcSunset, @"sunset", nil];
    
    0 讨论(0)
提交回复
热议问题