batoulapps / adhan-kotlin

High precision Islamic prayer time library for Java
MIT License
157 stars 47 forks source link

Disrepencies between Java and Kotlin Multiplatform Versions #41

Closed hsravat-4590 closed 2 years ago

hsravat-4590 commented 2 years ago

Assalam Mualaykum,

I've been trying to implement the Kotlin Multiplatform branch and have come across some disrepencies between the Java Version and KMP versions:

Java

Test Code:

...
public class JavaTest {
    @Test
    public void testAberdeen() {
        final var coordinates = new Coordinates(57.1499, 2.0938);
        final var date = new DateComponents(2022, 06, 25);
        final var prayerTimes = new PrayerTimes(coordinates, date, CalculationMethod.KARACHI.getParameters());
    }
}

This produces the following result: image

Kotlin Multi Platfrom

    @Test
    fun testForDefault() {
        val params = CalculationMethod.KARACHI.parameters
        val coordinates = Coordinates(57.1499, 2.0938);
        val testDate = LocalDate(year = 2022, monthNumber = 6, dayOfMonth = 25)
        val prayerTimes = PrayerTimes(coordinates, DateComponents.fromLocalDate(testDate), params)
}

note: I added a simple method to DateComponents to allow creation with LocalDate

This produces the following result: image

Can anyone offer a hand on why this might be? I suspect it's to do with LocalDate converisons as I wasn't able to find anything different in the Astronomical and CalendricalHelper Classes

ahmedre commented 2 years ago

Wa3laikum alsalam, I can't repro this - here's what I did (modifying the sample of both the KMP and non-KMP versions) -

Kotlin Multiplatform

object Example {
  private fun Instant.asDate() = Date(toEpochMilliseconds())

  @JvmStatic
  fun main(args: Array<String>) {
    val coordinates = Coordinates(57.1499, 2.0938)
    val dateComponents = DateComponents(2022, 6, 25)
    val parameters: CalculationParameters = CalculationMethod.KARACHI.parameters

    val prayerTimes = PrayerTimes(coordinates, dateComponents, parameters)

    val formatter = SimpleDateFormat("hh:mm a")
    formatter.timeZone = TimeZone.getTimeZone("Asia/Riyadh")

    println("Fajr: " + formatter.format(prayerTimes.fajr.asDate()))
    println("Sunrise: " + formatter.format(prayerTimes.sunrise.asDate()))
    println("Dhuhr: " + formatter.format(prayerTimes.dhuhr.asDate()))
    println("Asr: " + formatter.format(prayerTimes.asr.asDate()))
    println("Maghrib: " + formatter.format(prayerTimes.maghrib.asDate()))
    println("Isha: " + formatter.format(prayerTimes.isha.asDate()))
  }
}

output:

Fajr: 02:54 AM
Sunrise: 05:57 AM
Dhuhr: 02:55 PM
Asr: 07:29 PM
Maghrib: 11:52 PM
Isha: 02:54 AM

Java:

public class Example {

  public static void main(String[] args) {
    final Coordinates coordinates = new Coordinates(57.1499, 2.0938);
    final DateComponents dateComponents = new DateComponents(2022, 6, 25);
    final CalculationParameters parameters =
        CalculationMethod.KARACHI.getParameters();

    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");
    formatter.setTimeZone(TimeZone.getTimeZone("Asia/Riyadh"));

    PrayerTimes prayerTimes = new PrayerTimes(coordinates, dateComponents, parameters);
    System.out.println("Fajr: " + formatter.format(prayerTimes.fajr));
    System.out.println("Sunrise: " + formatter.format(prayerTimes.sunrise));
    System.out.println("Dhuhr: " + formatter.format(prayerTimes.dhuhr));
    System.out.println("Asr: " + formatter.format(prayerTimes.asr));
    System.out.println("Maghrib: " + formatter.format(prayerTimes.maghrib));
    System.out.println("Isha: " + formatter.format(prayerTimes.isha));
  }
}

output:

Fajr: 02:54 AM
Sunrise: 05:57 AM
Dhuhr: 02:55 PM
Asr: 07:29 PM
Maghrib: 11:52 PM
Isha: 02:54 AM
ahmedre commented 2 years ago

I would be curious as to the implementation of your DateComponents.fromLocalDate(testDate) function, since that's the only piece that seems to be different.

For what it's worth, I also tried using the built in method for converting a LocalDateTime to a DateComponents (which got me the correct result also):

DateComponents.fromLocalDateTime(LocalDateTime(year = 2022, monthNumber = 6, dayOfMonth = 25, hour = 12, minute = 0, 0))
ahmedre commented 2 years ago

closing since this is working for me