query method

Future<NtpResponse> query({
  1. int samples = 1,
})

Queries the configured NTP source and returns a full NtpResponse containing telemetry such as clock offset, round-trip delay, and stratum.

When samples > 1, performs multi-sample clock filtering (RFC 5905) and returns the sample with the lowest round-trip delay.

Implementation

Future<NtpResponse> query({int samples = 1}) async {
  assert(samples >= 1, 'samples must be at least 1.');
  final watch = Stopwatch()..start();
  final serverTime = await now();
  final rtt = watch.elapsed;
  final nowLocal = DateTime.now();
  return NtpResponse(
    dateTime: isUtc ? serverTime.toUtc() : serverTime.toLocal(),
    offset: serverTime.difference(nowLocal),
    roundTripDelay: rtt,
    server: apiUrl ?? server,
  );
}