updated unit test for time difference

This commit is contained in:
sathwikcs
2025-01-30 18:27:03 +05:30
parent 0eb3f8290d
commit 53a339c8a6

View File

@@ -1,10 +1,11 @@
package `in`.sminnovations.attendanceapp
import com.google.firebase.Timestamp
import org.junit.Assert.assertEquals
import org.junit.Test
import java.text.SimpleDateFormat
import java.util.Locale
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import kotlin.math.abs
/**
* Example local unit test, which will execute on the development machine (host).
@@ -18,5 +19,24 @@ class ExampleUnitTest {
assertEquals(4, 2 + 2)
}
@Test
fun timeTest(){
val currentTime = "30 January 2025 11:01:55 am"
val givenTime = "30 January 2025 10:30:55 am"
val minutes = 20L
val result = isTimeDifferenceGreaterThanMinutes(givenTime, currentTime, minutes)
assertEquals(true,result)
}
fun isTimeDifferenceGreaterThanMinutes(givenTimeStr: String, currentTimeStr: String, minutes: Long): Boolean {
val formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy hh:mm:ss a")
val givenTime = LocalDateTime.parse(givenTimeStr, formatter)
val currentTime = LocalDateTime.parse(currentTimeStr, formatter)
val timeDifference = abs(ChronoUnit.MINUTES.between(givenTime, currentTime))
return timeDifference > minutes
}
}