function weather_get_weather in Weather 7.3
Same name and namespace in other branches
- 7.2 weather.common.inc \weather_get_weather()
Returns a weather object for the specified GeoID.
Parameters
string $geoid: GeoID of the place for which the weather is desired.
int $days: Return weather for specified number of days (0 = all available days).
bool $detailed: Return detailed forecasts or just one forecast per day.
Return value
array Weather array with forecast information.
5 calls to weather_get_weather()
- WeatherParserTestCase::testDifferentDaysOfForecasts in tests/parser.test 
- Test the parser with different days of forecast data.
- WeatherParserTestCase::_getInfoAboutForecast in tests/parser.test 
- Internal helper function for getting information about a forecast.
- weather_block_view in ./weather.module 
- Implements hook_block_view().
- weather_show_detailed_forecast in ./weather.common.inc 
- Display a detailed weather forecast for a given place.
- weather_views_pre_execute in ./weather.views.inc 
- Implements hook_views_pre_execute().
File
- ./weather.common.inc, line 326 
- Common functions in a separate file to reduce size of weather.module.
Code
function weather_get_weather($geoid, $days = 0, $detailed = TRUE) {
  // Support testing of module with fixed times instead of current time.
  $time = variable_get('weather_time_for_testing', REQUEST_TIME);
  module_load_include('inc', 'weather', 'weather_parser');
  // Get the scheduled time of next update. If there is no entry for
  // the specified GeoID, $meta will be FALSE.
  $meta = db_query('SELECT * FROM {weather_forecast_information} WHERE geoid = :geoid', array(
    ':geoid' => $geoid,
  ))
    ->fetchAssoc();
  $current_utc_time = gmdate('Y-m-d H:i:s', $time);
  // If the next scheduled download is due, try to get forecasts.
  if ($meta === FALSE or $current_utc_time >= $meta['next_download_attempt']) {
    $result = _weather_download_forecast($geoid);
    // If that worked, get the next scheduled update time.
    $meta = db_query('SELECT * FROM {weather_forecast_information} WHERE geoid = :geoid', array(
      ':geoid' => $geoid,
    ))
      ->fetchAssoc();
    // If there is no entry yet, set up initial values.
    if ($meta === FALSE) {
      $meta['geoid'] = $geoid;
      $meta['last_update'] = $current_utc_time;
      $meta['next_update'] = $current_utc_time;
      $meta['next_download_attempt'] = $current_utc_time;
      $meta['utc_offset'] = 0;
    }
    // The second check is needed if the download did succeed, but
    // the returned forecast is old and the next update is overdue.
    // In that case, the download attempt needs to wait as well,
    // otherwise, a new download will occur on every page load.
    if ($result == FALSE or $current_utc_time >= $meta['next_update']) {
      // The download did not succeed. Set next download attempt accordingly.
      // Calculate the UTC timestamp.
      $next_update = strtotime($meta['next_update'] . ' UTC');
      // Initial retry after 675 seconds (11.25 minutes).
      // This way, the doubling on the first day leads to exactly 86400
      // seconds (one day) update interval.
      $seconds_to_retry = 675;
      while ($next_update + $seconds_to_retry <= $time) {
        if ($seconds_to_retry < 86400) {
          $seconds_to_retry = $seconds_to_retry * 2;
        }
        else {
          $seconds_to_retry = $seconds_to_retry + 86400;
        }
      }
      // Finally, calculate the UTC time of the next download attempt.
      $meta['next_download_attempt'] = gmdate('Y-m-d H:i:s', $next_update + $seconds_to_retry);
      // Remove meta information for this location.
      db_delete('weather_forecast_information')
        ->condition('geoid', $meta['geoid'])
        ->execute();
      // Write new entry.
      db_insert('weather_forecast_information')
        ->fields($meta)
        ->execute();
    }
  }
  $return_array['forecasts'] = weather_get_forecasts_from_database($geoid, $meta['utc_offset'], $days, $detailed, $time);
  $return_array['utc_offset'] = $meta['utc_offset'];
  return $return_array;
}