isDateTimeValid method

Future<bool> isDateTimeValid({
  1. int toleranceInSeconds = 86400,
})

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 but a cached value exists, the cached time is reused. If no 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 = DateTime.now();
  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;
}