获取飞镖的当前日期 [英] Get current date in Dart

查看:78
本文介绍了获取飞镖的当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Dart中获取与系统日期和时间无关的当前日期?

How can I get the current date independent of system date and time in Dart?

我已经尝试过 DateTime().now ,但是它给出的是系统日期而不是实际日期(以防系统日期被更改).

I have tried DateTime().now but it gives system date not the actual date (in case system date is altered).

推荐答案

如果您不信任系统提供正确的时间,则需要询问其他地方以获取时间.可以根据需要的精度以多种方式完成此操作.例如.最简单的方法是使用API​​调用HTTP服务器以从服务器获取时间.

If you don't trust the system to give the correct time you need to ask somewhere else to get the time. This can be done in multiply ways depending on which accuracy you need. E.g. the easiest way would be to just call a HTTP server with an API to get the time from the server.

如果准确性很重要,您确实需要使用NTP客户端连接到多个时间源(例如,来自ntppool.org).我可以看到这里有一个包装: https://pub.dev/packages/ntp

If accuracy are important you really need to use a NTP client to connect to multiple time sources (e.g. from ntppool.org). I can see there are a package for this here: https://pub.dev/packages/ntp

我想说,在大多数情况下,您应该只信任操作系统.如果用户想要操纵时间,则他/她应该能够做到这一点.而且,如果时间准确性很重要,则您不想信任客户端,而是使用自己服务器的准确性.

I would say that in most cases you should just trust the OS. If a user want to manipulate the time he/she should be able to do that. And if the time accuracy is important you don't want to trust the client but use a accuracy from you own server.

这篇关于获取飞镖的当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆