blob: 8d5a7e63f365e02e05b4dcf4b314fd6e1d4c3c21 (
plain) (
blame)
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
|
#include <math.h>
#include <glib.h>
#include "sun.h"
#define ZENITH -.83
float to_rad (float n) {
return (M_PI/180) * n;
}
float to_deg (float n) {
return (180/M_PI) * n;
}
// http://edwilliams.org/sunrise_sunset_algorithm.htm
// if the sunset is desired, set sunset to >= 1
// returns -1 if the sun never rises/sets in the specified location
float calculateSun(int year, int month, int day, float lat, float lng, int localOffset, int sunset) {
if (sunset > 1)
sunset = 1;
float N1 = floor(275 * month / 9);
float N2 = floor((month + 9) / 12);
float N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3));
float N = N1 - (N2 * N3) + day - 30;
float lngHour = lng / 15.0;
float t = N + ((sunset == 0 ? 6 : 18 - lngHour) / 24);
float M = (0.9856 * t) - 3.289;
float L = fmod(M + (1.916 * sin(to_rad(M))) + (0.020 * sin(2 *to_rad(M))) + 282.634,360.0);
float RA = fmod(to_deg(atan(0.91764 * tan(to_rad(L)))),360.0);
float Lquadrant = floor( L/90) * 90;
float RAquadrant = floor(RA/90) * 90;
RA = RA + (Lquadrant - RAquadrant);
RA = RA / 15;
float sinDec = 0.39782 * sin(to_rad(L));
float cosDec = cos(asin(sinDec));
float cosH = (sin(to_rad(ZENITH)) - (sinDec * sin(to_rad(lat)))) / (cosDec * cos(to_rad(lat)));
if (cosH > 1)
return -1;
else if (cosH < -1)
return -1;
float H = sunset == 0 ? (360 - to_deg(acos(cosH))) : to_deg(acos(cosH));
H = H / 15;
float T = H + RA - (0.06571 * t) - 6.622;
float UT = fmod(T - lngHour,24.0);
if ( UT < 0 )
UT = UT+24;
else if (UT > 24)
UT = UT-24;
return UT + localOffset;
}
|