WEATHERKIT - hourly forecast

I'm using the following code to access WeatherKit. All services (except for .hourly) are working. For .hourly, the time blocks are 243 hours starting at 10pm, regardless of the time of time the request is made.

What am I doing wrong?

let weather = try await weatherService.weather(for: thisLocation, including: .daily, .hourly, .current, .alerts)

// 0, 1, 2, 3 is the order of the "including" above
let dailyForecast = weather.0.forecast
let hourlyForecast = weather.1.forecast
let currentWeather = weather.2
var alertWeather = [WeatherAlert]()
if (weather.3 != nil)
{
  alertWeather = weather.3!
}

Accepted Reply

This is the default behavior when you do not specify a time range for your request. When you request the hourly and daily forecast together and do not specify start and end times for the hourly forecast, WeatherKit returns an hourly forecast including all the hours that contribute to the daily forecast. That includes two hours before the start of the daily forecast itself--which always starts at midnight local time--and one hour after the end of the daily forecast to allow fitting a smooth curve to hourly data values. Therefore, if you don't specify your own start and end times the default is to return hours starting at 10PM local time.

You can control this behavior by specifying a startDate and endDate on your hourly forecast query. See the documentation for WeatherQuery

  • Is there sample code on how to use startDate and endDate?

  • Never mind. I was overcomplicating it. It is simply: .hourly(startDate: <#T##Date#>, endDate: <#T##Date#>)

  • @EdwardD20 where is the .hourly() function?

Add a Comment

Replies

This is the default behavior when you do not specify a time range for your request. When you request the hourly and daily forecast together and do not specify start and end times for the hourly forecast, WeatherKit returns an hourly forecast including all the hours that contribute to the daily forecast. That includes two hours before the start of the daily forecast itself--which always starts at midnight local time--and one hour after the end of the daily forecast to allow fitting a smooth curve to hourly data values. Therefore, if you don't specify your own start and end times the default is to return hours starting at 10PM local time.

You can control this behavior by specifying a startDate and endDate on your hourly forecast query. See the documentation for WeatherQuery

  • Is there sample code on how to use startDate and endDate?

  • Never mind. I was overcomplicating it. It is simply: .hourly(startDate: <#T##Date#>, endDate: <#T##Date#>)

  • @EdwardD20 where is the .hourly() function?

Add a Comment