isDateTimeValid method
Validates the device's system time by comparing it with the network time (NTP).
Returns true if the difference between the device time and the network time
is within the specified toleranceInSeconds. Defaults to 86400 seconds (24 hours).
When the latest NTP fetch fails, a cached value is reused only when it can
be advanced with platform monotonic elapsed time. If no reliable cached
value is available, the method returns true to keep the app usable; call
sites that must fail closed should layer additional checks on top.
Implementation
Future<bool> isDateTimeValid({int toleranceInSeconds = 86400}) async {
networkTime = await getNetworkTime();
DateTime deviceTime = _deviceTimeProvider();
safeLog('Device time: $deviceTime');
if (networkTime == null) {
safeLog(
'Network time unavailable; proceeding with device time validation bypass.',
);
return true;
}
// Calculate absolute difference in seconds
final difference = deviceTime.difference(networkTime!).inSeconds.abs();
// Check if within tolerance
return difference <= toleranceInSeconds;
}