forked from martinhering/ICAppearanceDaylightCalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathICAppearanceDaylightCalculator.m
225 lines (178 loc) · 6.37 KB
/
ICAppearanceDaylightCalculator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* MIT License
*
* Copyright (c) 2018 Martin Hering
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#import <CoreLocation/CoreLocation.h>
#import "ICAppearanceDaylightCalculator.h"
#define DEGS (180.0/M_PI)
#define RADS (M_PI/180.0)
#define SUN_DIA 0.53
#define AIR_REFR (34.0/60.0)
@interface ICAppearanceDaylightCalculator ()
@property (nonatomic, strong, readwrite) CLLocation* location;
@property (nonatomic, strong, readwrite) NSDate* sunrise;
@property (nonatomic, strong, readwrite) NSDate* sunset;
@end
@implementation ICAppearanceDaylightCalculator
NS_INLINE double FNday (int y, int m, int d, float h) {
long int luku = - 7 * (y + (m + 9)/12)/4 + 275*m/9 + d;
// type casting necessary on PC DOS and TClite to avoid overflow
luku+= (long int)y*367;
return (double)luku - 730531.5 + h/24.0;
};
// the function below returns an angle in the range
// 0 to 2*M_PI
NS_INLINE double FNrange (double x) {
double b = 0.5*x / M_PI;
double a = 2.0*M_PI * (b - (long)(b));
if (a < 0) a = 2.0*M_PI + a;
return a;
};
// Calculating the hourangle
//
NS_INLINE double f0(double lat, double declin) {
double fo,dfo;
// Correction: different sign at S HS
dfo = RADS*(0.5*SUN_DIA + AIR_REFR); if (lat < 0.0) dfo = -dfo;
fo = tan(declin + dfo) * tan(lat*RADS);
if (fo>0.99999) fo=1.0; // to avoid overflow //
fo = asin(fo) + M_PI/2.0;
return fo;
};
// Calculating the hourangle for twilight times
//
NS_INLINE double f1(double lat, double declin) {
double fi,df1;
// Correction: different sign at S HS
df1 = RADS * 6.0; if (lat < 0.0) df1 = -df1;
fi = tan(declin + df1) * tan(lat*RADS);
if (fi>0.99999) fi=1.0; // to avoid overflow //
fi = asin(fi) + M_PI/2.0;
return fi;
};
// Find the ecliptic longitude of the Sun
NS_INLINE double FNMeanLong (double d)
{
return FNrange(280.461 * RADS + .9856474 * RADS * d);
}
NS_INLINE double FNsun (double d)
{
double L,g;
// mean longitude of the Sun
L = FNMeanLong(d);
// mean anomaly of the Sun
g = FNrange(357.528 * RADS + .9856003 * RADS * d);
// Ecliptic longitude of the Sun
return FNrange(L + 1.915 * RADS * sin(g) + .02 * RADS * sin(2 * g));
};
- (id) initWithWithLocation:(CLLocation*)location date:(NSDate*)date
{
if ((self = [self init])) {
_location = location;
_date = date;
[self _calculate];
}
return self;
}
- (void) setDate:(NSDate *)date
{
if (_date != date) {
_date = date;
[self _calculate];
}
}
- (void) _calculate
{
double y,m,day,h,latit,longit;
double tzone,d,lambda;
double obliq,alpha,delta,LL,equation,ha,hb,twx;
double twam,altmax,noont,settm,riset,twpm;
time_t sekunnit;
struct tm *p;
double L;
double daylen;
// get the date and time from the user
// read system date and extract the year
/** First get time **/
time(&sekunnit);
/** Next get localtime **/
p=localtime(&sekunnit);
y = p->tm_year;
// this is Y2K compliant method
y+= 1900;
m = p->tm_mon + 1;
day = p->tm_mday;
h = 12;
latit = self.location.coordinate.latitude; longit = self.location.coordinate.longitude;
tzone = [[NSTimeZone localTimeZone] secondsFromGMT] / 3600.0;
// testing
// m=6; day=10;
d = FNday(y, m, day, h);
// Use FNsun to find the ecliptic longitude of the
// Sun
lambda = FNsun(d);
L = FNMeanLong(d);
// Obliquity of the ecliptic
obliq = 23.439 * RADS - .0000004 * RADS * d;
// Find the RA and DEC of the Sun
alpha = atan2(cos(obliq) * sin(lambda), cos(lambda));
delta = asin(sin(obliq) * sin(lambda));
// Find the Equation of Time
// in minutes
// Correction suggested by David Smith
LL = L - alpha;
if (L < M_PI) LL += 2.0*M_PI;
equation = 1440.0 * (1.0 - LL / M_PI/2.0);
ha = f0(latit,delta);
hb = f1(latit,delta);
twx = hb - ha; // length of twilight in radians
twx = 12.0*twx/M_PI; // length of twilight in hours
// Conversion of angle to hours and minutes //
daylen = DEGS*ha/7.5;
if (daylen<0.0001) {daylen = 0.0;}
// arctic winter //
riset = 12.0 - 12.0 * ha/M_PI + tzone - longit/15.0 + equation/60.0;
settm = 12.0 + 12.0 * ha/M_PI + tzone - longit/15.0 + equation/60.0;
noont = riset + 12.0 * ha/M_PI;
altmax = 90.0 + delta * DEGS - latit;
// Correction for S HS suggested by David Smith
// to express altitude as degrees from the N horizon
if (latit < delta * DEGS) altmax = 180.0 - altmax;
twam = riset - twx; // morning twilight begin
twpm = settm + twx; // evening twilight end
if (riset > 24.0) riset-= 24.0;
if (settm > 24.0) settm-= 24.0;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* coms = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self.date];
//sunrise
[coms setHour:riset];
[coms setMinute:(riset - (int)riset)*60];
_sunrise = [calendar dateFromComponents:coms];
//sunset
[coms setHour:settm];
[coms setMinute:(settm - (int)settm)*60];
_sunset = [calendar dateFromComponents:coms];
if (settm < riset) {
_sunset = [_sunset dateByAddingTimeInterval:86400];
}
}
@end