parseNtpTimestamps static method

({DateTime originTimestamp, DateTime receiveTimestamp, DateTime transmitTimestamp}) parseNtpTimestamps(
  1. Uint8List data
)

Parses the NTP server's response and extracts the Origin (T1), Receive (T2), and Transmit (T3) timestamps, with RFC 4330 Era 1 (2036 rollover) support.

  • T1: Origin Timestamp (bytes 24-31)
  • T2: Receive Timestamp (bytes 32-39)
  • T3: Transmit Timestamp (bytes 40-47)

Implementation

static ({
  DateTime originTimestamp,
  DateTime receiveTimestamp,
  DateTime transmitTimestamp,
}) parseNtpTimestamps(Uint8List data) {
  if (data.length < 48) {
    throw Exception(
      'Malformed NTP response packet: length is ${data.length}',
    );
  }
  final byteData =
      ByteData.view(data.buffer, data.offsetInBytes, data.lengthInBytes);

  return (
    originTimestamp: DateTime.fromMicrosecondsSinceEpoch(
      _parseTimestampMicros(byteData, 24),
      isUtc: true,
    ),
    receiveTimestamp: DateTime.fromMicrosecondsSinceEpoch(
      _parseTimestampMicros(byteData, 32),
      isUtc: true,
    ),
    transmitTimestamp: DateTime.fromMicrosecondsSinceEpoch(
      _parseTimestampMicros(byteData, 40),
      isUtc: true,
    ),
  );
}