Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

In a 5 day weather array how does one isolate a single time slot per day using swift?

I am trying to get just the high temp, day icon etc for the peak of the day, which would be noon. I think this is done through the DateFormatter, here is what I have so far and it is returning the 5 day forecast no problem, but it updates every 3 hours. Is there anyway to disable this and/or just make a call for just 12:00 noon using this code?

func getDayOfWeek(today:String)->String? {
    let formatter  = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let todayDate = formatter.date(from: today)
    formatter.dateFormat = "EEEE" // "eeee" -> Friday
    let weekDay = formatter.string(from: todayDate!)

    return weekDay
}

All the weather data:

{
    "cod": "200",
    "message": 0.0054,
    "cnt": 40,
    "list": [{
                "dt": 1525100400,
                "main": {
                    "temp": 12.43,
                    "temp_min": 10.44,
                    "temp_max": 12.43,
                    "pressure": 1015.9,
                    "sea_level": 1035.33,
                    "grnd_level": 1015.9,
                    "humidity": 64,
                    "temp_kf": 1.99
                },
                "weather": [{
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }],
                "clouds": {
                    "all": 0
                },
                "wind": {
                    "speed": 4.66,
                    "deg": 307.008
                },
                "sys": {
                    "pod": "d"
                },
                "dt_txt": "2018-04-30 15:00:00"
            }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a very simple example as starting point showing only the relevant data.

The example decodes the list array into a custom struct. the dt key is decoded into Date. Then it creates a DateComponents instance with noon in the current time zone and filters the dates.

struct WeatherData : Decodable {
    let cod : String
    let list : [List]
}

struct List : Decodable {
    let dt : Date
}

let jsonString = """
{ "cod": "200", "message": 0.0054, "cnt": 40, "list": [{ "dt": 1525100400, "main": {"temp": 12.43, "temp_min": 10.44, "temp_max": 12.43, "pressure": 1015.9, "sea_level": 1035.33, grnd_level": 1015.9, "humidity": 64, "temp_kf": 1.99 }, "weather": [{"id": 800, "main": "Clear", "description": "clear sky", "icon": "01d"}],    "clouds": {"all": 0}, "wind": {"speed": 4.66, "deg": 307.008},"sys": {"pod": "d"},"dt_txt": "2018-04-30 15:00:00"}]}
"""

let data = Data(jsonString.utf8)
do {
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .secondsSince1970
    let result = try decoder.decode(WeatherData.self, from: data)
    let utcDifference = TimeZone.current.secondsFromGMT() / 3600
    let noonComponents = DateComponents(hour: 12 + utcDifference, minute: 0, second: 0)
    let noonDates = result.list.filter { Calendar.current.date($0.dt, matchesComponents:noonComponents) }
    print(noonDates)

} catch {
    print("error:", error)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...