Skip to main content

dio_server_clock 0.1.0 copy "dio_server_clock: ^0.1.0" to clipboard
dio_server_clock: ^0.1.0 copied to clipboard

Tamper-resistant server time for Dio apps. Reads the HTTP Date header from responses you already make and anchors it to a monotonic clock.

example/main.dart

import 'package:dio/dio.dart';
import 'package:dio_server_clock/dio_server_clock.dart';

Future<void> main() async {
  final serverClock = ServerClock();

  final dio = Dio(BaseOptions(baseUrl: 'https://api.github.com'))
    // Add it last, after auth and caching interceptors.
    ..interceptors.add(ServerClockInterceptor(serverClock));

  serverClock.onAnchor.listen((sample) => print('Anchored: $sample'));

  try {
    await dio.get<dynamic>('/');
  } on Exception catch (_) {
    // Error responses still carry a Date header and are sampled.
  }

  print('Synced:      ${serverClock.isSynced}');
  print('Server UTC:  ${serverClock.now}');
  print('Device skew: ${serverClock.deviceSkew}');
  print('Uncertainty: ${serverClock.uncertainty}');

  // A fixed offset, for example India Standard Time.
  const ist = Duration(hours: 5, minutes: 30);
  final t = serverClock.nowIn(ist);
  print('IST:         ${t.hour}:${t.minute.toString().padLeft(2, '0')}');

  await serverClock.dispose();
}
1
likes
160
points
825
downloads

Documentation

Documentation
API reference

Publisher

verified publisherbalu.dev

Weekly Downloads

Tamper-resistant server time for Dio apps. Reads the HTTP Date header from responses you already make and anchors it to a monotonic clock.

Homepage
Repository (GitHub)
View/report issues

Topics

#dio #time #clock #http #interceptor

License

MIT (license)

Dependencies

clock, dio, http_parser

More

Packages that depend on dio_server_clock