Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
Updated current forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
devmaximilian committed Feb 14, 2021
1 parent c66e3a3 commit e44b68f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Sources/Weather/Models/Weather.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,28 @@ extension Weather {
}

public var forecasts: [Forecast] {
let now = Date()
let now: Date = .oneHourAgo
return timeSeries.sorted(by: { $0.validTime < $1.validTime })
.filter { forecast -> Bool in
forecast.validTime >= now
}
}

/// - Returns: The most relevant `Forecast`
public func get(by date: Date = .init(timeIntervalSinceNow: 60 * 60 * -1)) -> Forecast? {
public func get(by date: Date? = nil) -> Forecast? {
let date: Date = date ?? .oneHourAgo
return forecasts.first { forecast -> Bool in
forecast.validTime >= date
}
}
}

extension Date {
fileprivate static var oneHourAgo: Date {
return Date(timeIntervalSinceNow: 60 * 60 * -1)
}
}

extension Weather {
public static func publisher(latitude: Double, longitude: Double) -> WeatherPublisher {
return WeatherPublisher(
Expand Down

0 comments on commit e44b68f

Please sign in to comment.