162
calc_sun2 (WeatherInfo *info, time_t t)
162
calc_sun (GWeatherInfo *info, time_t t)
164
gdouble obsLat = info->location->latitude;
165
gdouble obsLon = info->location->longitude;
164
GWeatherInfoPrivate *priv;
168
169
gdouble gm_hoff, lambda;
175
176
gdouble x, u, dt;
177
178
/* Approximate preceding local midnight at observer's longitude */
178
obsLat = info->location->latitude;
179
obsLon = info->location->longitude;
180
obsLat = priv->location.latitude;
181
obsLon = priv->location.longitude;
180
182
gm_midn = t - (t % 86400);
181
183
gm_hoff = floor ((RADIANS_TO_DEGREES (obsLon) + 7.5) / 15.);
182
184
lcl_midn = gm_midn - 3600. * gm_hoff;
195
197
ecl2equ (lcl_midn + 86400.,
196
198
lambda + DEGREES_TO_RADIANS(SOL_PROGRESSION), 0.,
200
202
* If the observer is within the Arctic or Antarctic Circles then
201
203
* the sun may be above or below the horizon for the full day.
203
205
decl_midn = MIN(decl1,decl2);
204
206
decl_noon = (decl1+decl2)/2.;
206
208
(obsLat > (M_PI/2.-decl_midn)) || (obsLat < (-M_PI/2.-decl_midn));
208
210
(obsLat > (M_PI/2.+decl_noon)) || (obsLat < (-M_PI/2.+decl_noon));
209
if (info->midnightSun || info->polarNight) {
210
info->sunriseValid = info->sunsetValid = FALSE;
211
if (priv->midnightSun || priv->polarNight) {
212
priv->sunriseValid = priv->sunsetValid = FALSE;
261
263
x = DEGREES_TO_RADIANS(0.830725);
262
264
u = acos ( sin(obsLat) / cos(decl2) );
263
265
dt = RADIANS_TO_HOURS ( asin ( sin(x) / sin(u) ) / cos(decl2) );
266
268
* Subtract the correction value from sunrise and add to sunset,
267
269
* then (step 11) convert sideral times to UT
272
274
else if (rise1 >= 24.)
274
info->sunriseValid = ((rise1 >= 0.) && (rise1 < 24.));
275
info->sunrise = (rise1 * 3600.) + lcl_midn;
276
priv->sunriseValid = ((rise1 >= 0.) && (rise1 < 24.));
277
priv->sunrise = (rise1 * 3600.) + lcl_midn;
277
279
set1 = (set1 + dt - tt) * 0.9972695661;
280
282
else if (set1 >= 24.)
282
info->sunsetValid = ((set1 >= 0.) && (set1 < 24.));
283
info->sunset = (set1 * 3600.) + lcl_midn;
285
return (info->sunriseValid || info->sunsetValid);
291
* @info: #WeatherInfo structure containing the observer's latitude
292
* and longitude in radians, fills in the sunrise and sunset times.
295
* Returns: gboolean indicating if the results are valid.
298
calc_sun_time (WeatherInfo *info, time_t t)
300
return info->location->latlon_valid && calc_sun2 (info, t);
305
* @info: #WeatherInfo structure containing the observer's latitude
306
* and longitude in radians, fills in the sunrise and sunset times.
308
* Returns: gboolean indicating if the results are valid.
311
calc_sun (WeatherInfo *info)
313
return calc_sun_time(info, time(NULL));
284
priv->sunsetValid = ((set1 >= 0.) && (set1 < 24.));
285
priv->sunset = (set1 * 3600.) + lcl_midn;
287
return (priv->sunriseValid || priv->sunsetValid);
291
_gweather_info_ensure_sun (GWeatherInfo *info)
293
GWeatherInfoPrivate *priv;
297
if (!priv->sunriseValid && !priv->sunsetValid)
298
calc_sun (info, priv->current_time);
318
302
* weather_info_next_sun_event:
324
308
* - next sunset, when icon changes to nighttime version
327
weather_info_next_sun_event (WeatherInfo *info)
311
gweather_info_next_sun_event (GWeatherInfo *info)
329
313
time_t now = time (NULL);
316
GWeatherInfoPrivate *priv;
333
320
g_return_val_if_fail (info != NULL, -1);
335
if (!calc_sun (info))
322
_gweather_info_ensure_sun (info);
338
324
/* Determine when the next local midnight occurs */
339
325
(void) localtime_r (&now, <m);
344
330
nxtEvent = mktime (<m);
346
if (info->sunsetValid &&
347
(info->sunset > now) && (info->sunset < nxtEvent))
348
nxtEvent = info->sunset;
349
if (info->sunriseValid &&
350
(info->sunrise > now) && (info->sunrise < nxtEvent))
351
nxtEvent = info->sunrise;
332
if (priv->sunsetValid &&
333
(priv->sunset > now) && (priv->sunset < nxtEvent))
334
nxtEvent = priv->sunset;
335
if (priv->sunriseValid &&
336
(priv->sunrise > now) && (priv->sunrise < nxtEvent))
337
nxtEvent = priv->sunrise;
352
338
return (gint)(nxtEvent - now);