function _weather_calculate_sunrise_sunset in Weather 7
Same name and namespace in other branches
- 5.6 weather_parser.inc \_weather_calculate_sunrise_sunset()
- 6.5 weather_parser.inc \_weather_calculate_sunrise_sunset()
Calculate the times of sunrise and sunset.
The times are GMT, so it's possible for the sunrise being at 16:48 while the sun sets at 7:06.
Parameters
object $metar: METAR data object, may be altered.
1 call to _weather_calculate_sunrise_sunset()
- weather_parse_metar in ./
weather_parser.inc - Parses a raw METAR data string.
File
- ./
weather_parser.inc, line 626 - Retrieves and parses raw METAR data and stores result in database.
Code
function _weather_calculate_sunrise_sunset(&$metar) {
// Get the coordinates for this weather station.
$location = db_query("SELECT latitude, longitude FROM {weather_icao} WHERE icao=:icao", array(
':icao' => $metar->icao,
))
->fetchAssoc();
$reported_on = $metar->reported_on;
// Initialize sunrise and sunset times.
$metar->sunrise_on = NULL;
$metar->sunset_on = NULL;
$suninfo = date_sun_info($reported_on, $location['latitude'], $location['longitude']);
// Handle special cases (no sunrise or sunset at all).
if ($suninfo['sunrise'] == 0 and $suninfo['sunset'] == 0) {
// Sun is always below the horizon. To indicate that the sun
// does not rise, let sunrise_on be NULL and set sunset_on to today.
$condition = 'night';
$metar->sunset_on = $metar->reported_on;
}
elseif ($suninfo['sunrise'] == 1 and $suninfo['sunset'] == 1) {
// Sun is always above the horizon. To indicate that the sun
// does not set, let sunset_on be NULL and set sunrise_on to today.
$condition = 'day';
$metar->sunrise_on = $metar->reported_on;
}
else {
// There is a sunrise and a sunset.
// We don't need the exact second of the sunrise and sunset. Therefore, the
// times are rounded to the next minute. We add 30 seconds and cut off the
// seconds part.
$metar->sunrise_on = round($suninfo['sunrise'] / 60) * 60;
$metar->sunset_on = round($suninfo['sunset'] / 60) * 60;
// Correctly handle northern and southern hemisphere.
if ($suninfo['sunrise'] <= $suninfo['sunset']) {
// This should be on the northern hemisphere.
if ($reported_on >= $suninfo['sunrise'] and $reported_on < $suninfo['sunset']) {
$condition = 'day';
}
else {
$condition = 'night';
}
}
else {
// This should be on the southern hemisphere.
if ($reported_on >= $suninfo['sunrise'] or $reported_on <= $suninfo['sunset']) {
$condition = 'day';
}
else {
$condition = 'night';
}
}
}
$metar->daytime_condition = $condition;
}