如何检查用户日期和时间是否设置正确

时间:2018-11-14 06:36:52

标签: dart flutter

我想知道如何检查用户电话datetime的设置是否正确。

1 个答案:

答案 0 :(得分:1)

您可以将NTP软件包用于网络时间协议格式http://pool.ntp.org

使用此软件包,您可以计算用户时间与网络时间之间的偏移量

文档示例

import 'package:ntp/ntp.dart';

Future<void> main() async {
  DateTime _myTime;
  DateTime _ntpTime;

  /// Or you could get NTP current (It will call DateTime.now() and add NTP offset to it)
  _myTime = await NTP.now();

  /// Or get NTP offset (in milliseconds) and add it yourself
  final int offset = await NTP.getNtpOffset(localTime: DateTime.now());
  _ntpTime = _myTime.add(Duration(milliseconds: offset));

  print('My time: $_myTime');
  print('NTP time: $_ntpTime');
  print('Difference: ${_myTime.difference(_ntpTime).inMilliseconds}ms');
}