batoulapps / adhan-swift

High precision Islamic prayer time library for Swift
MIT License
182 stars 42 forks source link

Add 12/24 Hour format #47

Closed natiq2004 closed 3 years ago

natiq2004 commented 3 years ago

Assalamu aleykum wa rahmatullahi wa barakatuhu.

Please add the option 12 (AM/PM) and 24 Hour format. This is necessary options.

// Example times[.isha].toTime12() // 09:00 PM times[.isha].toTime24() // 21:00

Please look at this: https://github.com/ashikahmad/PrayerTimes-Swift/tree/refactor-calculation

Barakallahu fiik

sgtsquiggs commented 3 years ago

Hi,

Our library returns Date objects. We do not have any convenience functions for date formatting as formatting is very different depending on your locale. It is trivial to implement your own toTime12 or toTime24 extensions to the Date object:

extension Date {
    func toTime12() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "hh:mm a"
        return dateFormatter.string(from: self)
    }

    func toTime24() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        return dateFormatter.string(from: self)
    }
}

The issue now is what about timezone? If we added date formatting functionality to the library we would have to make a lot of assumptions, or we would have to make the date formatting functions capable of handling many situations. Foundation's DateFormatter already does all this so we do not.