ntp_dart 1.2.0 copy "ntp_dart: ^1.2.0" to clipboard
ntp_dart: ^1.2.0 copied to clipboard

Lightweight Dart package to fetch accurate UTC time from NTP servers with no external dependencies.

example/main.dart

import 'dart:async';
import 'dart:convert';
import 'package:ntp_dart/ntp_dart.dart';

/// Entry point of the example application.
Future<void> main() async {
  _printHeader('⏱️  NTP DART EXAMPLE');
  print('Local System Time: ${DateTime.now()}');
  print('Local UTC Time:    ${DateTime.now().toUtc()}');

  await _runDirectNtpFetch();
  await _runCustomWebApi();
  await _runAccurateTimeDemo();
  await _runTimezoneCheck();

  _printHeader('🏁  Example Completed');
}

/// 1. Direct NTP Fetch
Future<void> _runDirectNtpFetch() async {
  _printSection('1. Direct NTP Fetch');
  print('Fetching time from default NTP server...');
  try {
    final now = await NtpClient().now();
    print('✅ NTP Time (Default): $now');
  } catch (e) {
    print('❌ NTP Fetch Error: $e');
  }
}

/// 2. Custom Web API (Web Only Demo)
Future<void> _runCustomWebApi() async {
  _printSection('2. Custom Web API (Web-only features)');
  print('Fetching time from WorldTimeAPI...');
  try {
    final customTime = await NtpClient(
      apiUrl: 'https://worldtimeapi.org/api/timezone/Etc/UTC',
      parseResponse: (response) {
        final json = jsonDecode(response.body);
        return DateTime.parse(json['utc_datetime']);
      },
      timeout: 10,
    ).now();
    print('✅ Custom API Time:   $customTime');
  } catch (e) {
    print('❌ Custom API Error:  $e');
  }
}

/// 3. AccurateTime (Cached & Auto-Sync)
Future<void> _runAccurateTimeDemo() async {
  _printSection('3. AccurateTime (Cached & Auto-Sync)');
  print('Initializing AccurateTime with 5s sync interval...');

  // Set a short interval to demonstrate re-syncing behavior
  AccurateTime.setSyncInterval(const Duration(seconds: 5));

  // First call fetches from network and caches result
  final accurateInitial = await AccurateTime.now();
  print('✅ AccurateTime (Network): $accurateInitial');

  // Verify synchronous access (uses cache)
  print('✅ AccurateTime (Cached):  ${AccurateTime.nowSync()}');

  print('\nWaiting for 6 seconds to trigger auto-resync...');
  await Future.delayed(const Duration(seconds: 6));

  // Next call will trigger a background sync because cache is stale
  final accurateResync = await AccurateTime.now();
  print('✅ AccurateTime (Resync):  $accurateResync');
}

/// 4. Timezone Support
Future<void> _runTimezoneCheck() async {
  _printSection('4. Timezone Support');
  print('Checking UTC vs Local time retrieval...');
  try {
    final utcTime = await NtpClient(isUtc: true).now();
    print('✅ NTP Time (UTC):   $utcTime\t(isUtc: ${utcTime.isUtc})');

    final localTime = await NtpClient(isUtc: false).now();
    print('✅ NTP Time (Local): $localTime\t(isUtc: ${localTime.isUtc})');
  } catch (e) {
    print('❌ Timezone Check Error: $e');
  }
}

// --- Helpers ---

void _printHeader(String title) {
  print('\n================================================================');
  print(title);
  print('================================================================\n');
}

void _printSection(String title) {
  print('\n----------------------------------------------------------------');
  print(title);
  print('----------------------------------------------------------------');
}
4
likes
150
points
1.41k
downloads

Publisher

verified publisherenzodesimone.dev

Weekly Downloads

Lightweight Dart package to fetch accurate UTC time from NTP servers with no external dependencies.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

http, intl

More

Packages that depend on ntp_dart