batoulapps / adhan-swift

High precision Islamic prayer time library for Swift
MIT License
185 stars 46 forks source link

Prayer times not correct #71

Closed IsamZareer closed 1 year ago

IsamZareer commented 2 years ago

what I have noticed is that in some places like Amman or Istanbul for example, the actual prayer time is behind the prayer time shown by Adhan-swift by almost exactly one hour

Actual Prayer Time Adhan-Swift
Fajr 3:49 AM 4:49 AM
Sunrise 5:32 AM 6:32 AM
Duhr 12:40 PM 1:40 PM
Asr 4:20 PM 5:20 PM
Magrib 7:46 PM 8:46 PM
Sunrise 9:17 PM 10:17 PM

Here is my code:

let cal = Calendar(identifier: Calendar.Identifier.gregorian)
        let date = cal.dateComponents([.year, .month, .day], from: Date())
        @State var coordinates =
        // Amman in Jordan coordinates
        Coordinates(latitude: 31.9539, longitude: 35.9106)
        var params = CalculationMethod.ummAlQura.params
        params.madhab = .shafi
        return PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params)
z3bi commented 2 years ago

@IsamZareer I'm going to guess that your located in a timezone that is 1 hour different from Amman/Istanbul. Our library will provide the correct Date but when you format and display the date you will be responsible for setting the TimeZone in your DateFormatter based on the location the prayers are for. If you look at the Full Example in the README you will see that is what we are doing https://github.com/batoulapps/adhan-swift#full-example

IsamZareer commented 2 years ago

i added it to my date formatter and nothing happened

import SwiftUI
import Adhan

private let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .none
    dateFormatter.timeStyle = .medium
    dateFormatter.timeZone = TimeZone(identifier: "Europe/Istanbul")
    return dateFormatter
}()

struct AthanView3: View {
    @State private var times = prayerTimesIstanbul()
    @StateObject var locationManager = LocationManager()

    var body: some View {
        VStack {
            PrayerTimeView(times: $times)
                .navigationBarTitle("Prayer Times")

        }
    }

    static func prayerTimesIstanbul() -> PrayerTimes? {
        let cal = Calendar(identifier: Calendar.Identifier.gregorian)
        let date = cal.dateComponents([.year, .month, .day], from: Date())
        @State var coordinates = Coordinates(latitude: 41.0082, longitude: 28.9784)
        var params = CalculationMethod.turkey.params
        params.madhab = .shafi
        return PrayerTimes(coordinates: coordinates, date: date, calculationParameters: params)
    }
}

struct PrayerTimeView: View {
    @Binding var times: PrayerTimes?
    let prayers: [Prayer] = [.fajr, .sunrise, .dhuhr, .asr, .maghrib, .isha]
    var body: some View {
        List {
            ForEach(prayers, id: \.self) { prayer in
                HStack {
                    self.formattedPrayerName(prayer: prayer)
                    self.formattedPrayerTime(prayer: prayer, times: self.times)
                }
            }
        }
        .listStyle(InsetGroupedListStyle())
    }

    func formattedPrayerTime(prayer: Prayer, times: PrayerTimes?) -> some View {
        guard let time = times?.time(for: prayer) else {
            return Text("-")
        }

        return Text("\(time, formatter: dateFormatter)").foregroundColor(.red)
    }

    func formattedPrayerName(prayer: Prayer) -> some View {
        switch prayer {
        case .fajr:
            return Text("Fajr:      ")
        case .sunrise:
            return Text("Sunrise:")
        case .dhuhr:
            return Text("Dhuhr:  ")
        case .asr:
            return Text("Asr:       ")
        case .maghrib:
            return Text("Maghrib:")
        case .isha:
            return Text("Isha:       ")
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        AthanView3()
    }
}
basememara commented 2 years ago

I believe you have to use the time zone consistently everywhere, so even in calendar:

var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(identifier: "Europe/Istanbul")
IsamZareer commented 2 years ago

@basememara when I try it it gives me this error if I end it with ! nothing happens

Screen Shot 2022-07-20 at 11 22 06 AM