WeatherKit forecast dates - what are they?

I don’t understand what the times are that are returned in forecasts.

guard let (current, hourly, daily) = try? await weatherService.weather(for: location, including: .current, .hourly, .daily(startDate: startDate, endDate: endDate))

I’m expecting this to be the time to which the forecast applies.

let utc = hourly.forecast[hour].date

When I convert it from UTC to local time I get something unusual for localDate

let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "ha"
let localDateString = dateFormatter.string(from: utc)

What are these times? Shouldn’t the hourly be times increasing in one hour increments from startDate? Am I doing something incorrectly? Thanks

Post not yet marked as solved Up vote post of spiff Down vote post of spiff
2.0k views
  • To be clear, I'm setting startDate and endDate like this:

    let startDate = Date() let endDate = Calendar.current.date(byAdding: .weekOfYear, value: 1, to: startDate)

Add a Comment

Replies

Beuller?

Yes, HourWeather.date should be the start of each hour. What are you seeing instead?

  • Cool, so Apple engineers are watching the forums... how come no comments on any of the threads about the very frequent 401 errors, and overall instability vs the Dark Sky API many of us have been forced to switch from?

Add a Comment

Hi, thanks for your response. My question is not whether the date is at the start of the hour.

Shouldn’t the hourly be times increasing in one hour increments from startDate?

I'm seeing times that unconditionally start at 10PM. If I run a query at 7AM I expect the hourly forecast entries to be 8AM, 9AM, etc.

// at 7AM after conversion I expect date to indicate 8AM
let utc = hourly.forecast[0].date

It appears that I am doing something wrong in my conversion Could you please provide a code fragment that would yield dates as I describe? Thanks.

  • The daily forecasts always start at midnight in the timezone of the location you specify. If you specify a start later than midnight it is rounded down to midnight on that day in the location's timezone. This means your forecast will start at a consistent hour for a given location.

  • A "forecast" is supposed to give you data in the future, not in the past. Always starting at midnight is an unnecessary complication. It should start at the current hour, like it was in the darksky api. Bad decision to change that.

Add a Comment

I understand, thank you! This is different from other models I'm used to where future whether is an offset from current time. This is equally sensible, just didn't occur to me.

how can I get the hourly weather for a future date? apparently the hourly is only for the current date?

Hourly weather is available for the next ten days. Just specify the startDate and endDate for hourly just as you do for daily:

.hourly(startDate: startDate, endDate: endDate)

See https://developer.apple.com/documentation/weatherkit/weatherquery/hourly for details.