19

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?

Moshe
  • 55,729
  • 73
  • 263
  • 420

7 Answers7

23

EDSunriseSet is an open source and free Objective-C wrapper for the C languages routines created by Paul Schlyter.

Calculation is done entirely by the C-code routines. EDSunrisetSet bridges those calculations to common Cocoa classes (NSDate, NSTimeZone, ...)

Palimondo
  • 6,961
  • 4
  • 37
  • 54
Rinju Jain
  • 1,692
  • 1
  • 14
  • 23
  • 1
    This one works for me. It was recently (Jan 2013) updated for some memory bugs so if you're using it, make sure you get the latest code. – mpemburn Jan 22 '13 at 12:49
8

I've actually ported the KosherJava Library and plan to make it available soon on GitHub!

Edit:

KosherCocoa is now live on GitHub! If you don't need the hebrew calendar related code, you can delete the "calendar" file. The class files are separated nicely into folders based on the kinds of calculations that they do.

Edit: KosherCocoa us due to be replaced with a modern and more complete update as soon as I can. The above link now points at a legacy repo.

Daniel Storm
  • 15,870
  • 6
  • 74
  • 133
Moshe
  • 55,729
  • 73
  • 263
  • 420
6

I have used a library called SUNWAIT. Very simple, effective - easy to use!

Moshe
  • 55,729
  • 73
  • 263
  • 420
Brad
  • 10,528
  • 7
  • 48
  • 68
  • Have you used this with Objective-C/for iOS? Also, are there any getting started tutorials? – Moshe Nov 05 '10 at 16:59
  • No - just straight C. You can compile it as a standard "C" file, and just call into it. (That's what I did in my App, but it wasn't an iPhone app). – Brad Nov 05 '10 at 17:34
  • Brad - Should I compile it separately or include it in my project and use API calls? – Moshe Nov 05 '10 at 20:25
  • The last think I used it for - I just built it as a part of the project. It was pretty straightforward. Just remove the "main" function! ;-) – Brad Nov 05 '10 at 20:27
  • I'm getting some weird values. Are there any tutorials online on how to use it? – Moshe Nov 25 '10 at 19:03
  • There is one minute difference for sunrise time in this example: Using location: 22.250000N, 114.166667E Date: 19 Jan 2011 Local time: 10:08 Current specified time zone: HKT (8 from UTC) Sun transits meridian 1234 HKT Sun rises 0704 HKT, sets 1803 HKT, compare with the result on http://www.hko.gov.hk/contentc.htm and http://aa.usno.navy.mil/cgi-bin/aa_rstablew.pl. – ohho Jan 19 '11 at 02:14
  • There are like 3 different definitions of "sunrise" and "sunset". "Civil", "Astronomical" and "Nautical". You may be comparing two different ones. – Brad Jan 19 '11 at 14:30
  • @Brad I'm looking for civil sunrise for a given location. – Moshe Mar 08 '11 at 01:24
  • Hey Brad, check out my answer, you may be interested. – Moshe Apr 22 '11 at 20:29
4

Try this: https://github.com/mourner/suncalc/

Very clear and easy to implement, although it writen by javascript but it is easy to convert it to
objective-C

It also support for calculate sun, moon position and coordinate.

Nhat Dinh
  • 3,202
  • 2
  • 30
  • 45
2

After not finding a simple Swift alternative, I created Solar: a Swift built micro-library for Sunrise / Sunset.

1

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];
DavidAWalsh
  • 917
  • 7
  • 7
  • It probably doesn't appear to work in Australia because you were given a date in GMT timezone. I bet it had `+0000` instead of `+1000` in the NSDate when you were debugging. Next time use `[date descriptionInLocale:[NSLocale currentLocale]]` to debug dates. :-) – Abhi Beckert Jun 30 '13 at 11:08
  • Hard to remember back that far but I'm pretty sure that wasn't the case, it was more to do with assumptions in the code and not testing on outliers. But anyway, the above works nicely and their example saved me some time. So that's good! :) – DavidAWalsh Jul 04 '13 at 08:39
  • Ah no worries :-) I'm using it in Australia so I hope they fixed the bugs. Seems to work after nearly a week of testing. – Abhi Beckert Jul 04 '13 at 09:24
0

try this one: https://github.com/berkley/ObjectiveCUtil

flypig
  • 1,230
  • 1
  • 18
  • 23