1 // Caluculate the time of sunrise and sunset
9 // Calculate the time of sunrise in timezone UTC.
11 hour Location::calculate_sunrise(date when)
17 hour midday(12, 0, 0);
19 double labda_y = latitude * M_PI / 180.0;
21 // Declanation of the sun as determined by the tilt of the earth's axis.
23 Declanation = -23.44 * cos(2 * M_PI * (when.YearDay() + 10) / 365);
25 // The earth rotates 15 degrees in 1 hour
27 solaroffset = longitude / 15.0 * 3600; // In seconds
29 h0 = acos ( (-1 * tan (labda_y) * tan (Declanation * M_PI / 180.0)));
31 long sunseconds = h0 * 12.0 / M_PI * 3600;
32 hour sunhours(sunseconds / 3600, (sunseconds / 60) % 60, sunseconds % 60);
34 // Calculate the time of sunrise
36 hour sunrise = midday - sunseconds;
37 sunrise = sunrise - hour(long(solaroffset));
42 // Calculate the time of sunset in timezone UTC.
44 hour Location::calculate_sunset(date when)
50 hour midday(12, 0, 0);
52 double labda_y = latitude * M_PI / 180.0;
54 // Declanation of the sun as determined by the tilt of the earth's axis.
56 Declanation = -23.44 * cos(2 * M_PI * (when.YearDay() + 10) / 365);
58 // The earth rotates 15 degrees in 1 hour
60 solaroffset = longitude / 15.0 * 3600; // In seconds
62 h0 = acos ( (-1 * tan (labda_y) * tan (Declanation * M_PI / 180.0)));
64 long sunseconds = h0 * 12.0 / M_PI * 3600;
65 hour sunhours(sunseconds / 3600, (sunseconds / 60) % 60, sunseconds % 60);
67 // Calculate the time of sunset
69 hour sunset = midday + sunseconds;
70 sunset = sunset - hour(long(solaroffset));
75 UTC Location::utc_to_localtime(date when, hour utc_time)
82 tp_utc.tm_year = when.Year() - 1900;
83 tp_utc.tm_mon = when.Month() - 1;
84 tp_utc.tm_mday = when.Day();
85 tp_utc.tm_hour = utc_time.Hour();
86 tp_utc.tm_min = utc_time.Minute();
87 tp_utc.tm_sec = utc_time.Second();
90 utc_t = timegm(&tp_utc);
92 local_tm = localtime(&utc_t);
93 UTC local_time(mktime(local_tm));