UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Date / Time Formatter from JSON data

Forums > SwiftUI

The times are in string format and are always 1 hr off "sunrise": "11:31", "sunset": "00:07",

Is there a better implementation to what I have below?

Text("\(DateExtensions.shared.convertTime(passedDate: passedWeatherDetails.sunrise))")

func convertTime(passedTime: String) -> String  {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    guard let date = dateFormatter.date(from: passedTime) else { return ""}
    guard let modDate = Calendar.current.date(byAdding: .hour, value: +1, to: date) else { return "" }
    dateFormatter.dateFormat = "h:mm a"
    dateFormatter.timeZone = TimeZone.current
    let theDate = dateFormatter.string(from: modDate)

    return theDate
}

2      

After messing around a bit, I found it doesn't account for daylight savings. This seems to work...

func convertTime(passedTime: String, timeZone: String) -> String  {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    guard let time = dateFormatter.date(from: passedTime) else { return "" }
    guard let checkTimeZone = TimeZone(identifier: timeZone) else { return "" }
    if checkTimeZone.isDaylightSavingTime(for: Date()) {
        guard let modTime = Calendar.current.date(byAdding: .hour, value: +1, to: time) else { return "" }
        dateFormatter.dateFormat = "h:mm a"
        dateFormatter.timeZone = TimeZone(identifier: timeZone)
        let theTime = dateFormatter.string(from: modTime)

        return theTime
    } else {
        dateFormatter.dateFormat = "h:mm a"
        dateFormatter.timeZone = TimeZone(identifier: timeZone)
        let theTime = dateFormatter.string(from: time)

        return theTime
    }
}
let sunrise1 = convertTime(passedTime: "13:09", timeZone: "America/Phoenix")
let sunset1 = convertTime(passedTime: "01:37", timeZone: "America/Phoenix")
print("Sunrise (Scottsdale, AZ): \(sunrise1)")
print("Sunset (Scottsdale, AZ): \(sunset1)")

// "timezone": "America/Chicago" // Lake In The Hills: "sunrise": "11:31", "sunset": "00:07"

let sunrise2 = convertTime(passedTime: "11:31", timeZone: "America/Chicago")
let sunset2 = convertTime(passedTime: "00:07", timeZone: "America/Chicago")
print("Sunrise (Lake In The Hills, IL): \(sunrise2)")
print("Sunset (Lake In The Hills, IL): \(sunset2)")

// "timezone": "America/New_York" // New York: "sunrise": "10:34", "sunset": "23:09"

let sunrise3 = convertTime(passedTime: "10:34", timeZone: "America/New_York")
let sunset3 = convertTime(passedTime: "23:09", timeZone: "America/New_York")
print("Sunrise (New York, NY): \(sunrise3)")
print("Sunset (New York, NY): \(sunset3)")

// "timezone": "Pacific/Honolulu" // Honolulu: "sunrise": "16:17", "sunset": "04:36"

let sunrise4 = convertTime(passedTime: "16:17", timeZone: "Pacific/Honolulu")
let sunset4 = convertTime(passedTime: "04:36", timeZone: "Pacific/Honolulu")
print("Sunrise (Honolulu, HI): \(sunrise4)")
print("Sunset (Honolulu, HI): \(sunset4)")

// "timezone": "Europe/London" // London: "sunrise": "05:31", "sunset": "18:20"

let sunrise5 = convertTime(passedTime: "05:31", timeZone: "Europe/London")
let sunset5 = convertTime(passedTime: "18:20", timeZone: "Europe/London")
print("Sunrise (London, UK): \(sunrise5)")
print("Sunset (London, UK): \(sunset5)")
########################################
Sunrise (Scottsdale, AZ): 6:09 AM
Sunset (Scottsdale, AZ): 6:37 PM
########################################
Sunrise (Lake In The Hills, IL): 6:31 AM
Sunset (Lake In The Hills, IL): 7:07 PM
########################################
Sunrise (New York, NY): 6:34 AM
Sunset (New York, NY): 7:09 PM
########################################
Sunrise (Honolulu, HI): 6:17 AM
Sunset (Honolulu, HI): 6:36 PM
########################################
Sunrise (London, UK): 6:31 AM
Sunset (London, UK): 7:20 PM

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.