When a user changes the date or time on their device, I want to display a message showing the current date and time in IST. However, this approach doesn’t work as expected because when the user adjusts the device’s date or time, both variables (the device’s time and IST) end up being the same.
val ALLOWED_TIME_DIFFERENCE = 5 * 60 * 1000L
LaunchedEffect(Unit) {
val deviceCalendar = Calendar.getInstance()
val deviceTimeMillis = deviceCalendar.timeInMillis
val istZonedDateTime = ZonedDateTime.ofInstant(
Instant.ofEpochMilli(System.currentTimeMillis()),
ZoneId.of("Asia/Kolkata")
)
val istTimeMillis = istZonedDateTime.toInstant().toEpochMilli()
val deviceDate = deviceCalendar.get(Calendar.YEAR) * 10000 + (deviceCalendar.get(Calendar.MONTH) + 1) * 100 + deviceCalendar.get(Calendar.DAY_OF_MONTH)
val istDate = istZonedDateTime.toLocalDate().toEpochDay().toInt()
val dateMismatch = deviceDate != istDate
val timeDifference = abs(deviceTimeMillis - istTimeMillis)
val timeMismatch = timeDifference > ALLOWED_TIME_DIFFERENCE
Log.d("TimeCheck", "Device Time: ${deviceCalendar.time}")
Log.d("TimeCheck", "IST Time: ${istZonedDateTime.toLocalDateTime()}")
Log.d("TimeCheck", "Date Mismatch: $dateMismatch, Time Mismatch: $timeMismatch, Time Difference: $timeDifference")
if (dateMismatch || timeMismatch) {
showTimeWarning = true
}
}