javascript - 获取用户的实时时间

标签 javascript angular google-api

我面临着获取真正“可信”用户时间的问题,以防止他们在更改计算机时间时进行作弊。

天气我使用简单的日期对象,或者时刻时区甚至谷歌时区API,如果我尝试操纵当前时间,我只是无法获取用户的“真实”时间。

如果我们在 20:00(无论位置如何)并且用户脾气暴躁地将其设置为 11:00,那么我总是以该时间结束,而不是真实的时间,或者通过

const time = new Date();
const timestamp = (time.getTime() / 1000 + time.getTimezoneOffset() * 60);
const url = 'https://maps.googleapis.com/maps/api/timezone/json?location=-31.369926900000003,-64.2218601&timestamp=1568396292&key=MY_API_KEY';
this.httpDuplicate.get(url ).subscribe((res: any) => {
   if (res) {
      const dst = res.dstOffset;
      const raw = res.rawOffset;
      const fixed = (timestamp + raw + dst) * 1000;
      const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
      const currentTime = momenttz().tz(timezone).format();
      console.warn('initial time ', new Date(time), 
                   ' - google time ', new Date(fixed), 
                   ' - moment timezone ', currentTime);
      // all these values are wrong and point to 11:00 rather than 20:00
   }
});

有办法实现这一点吗?我最终想要的是为某个位置获得正确的时间...显然不信任用户的系统时间,但确实信任其位置(尽管也有方法可以改变这一点)

最佳答案

失败的原因是您引用了根据用户客户端时间('time'变量)创建的 Date 对象。

如果您不信任系统时间,则必须从其他地方获取时间,例如您信任的外部服务器,或者应用程序的后端(如果有的话)(服务器时间)。 例如:http://worldtimeapi.org/

当然,在这种情况下您还需要用户的位置。我可以想象你也不相信这一点,因为你也不相信时间,所以在这种情况下没有办法做到这一点。

关于javascript - 获取用户的实时时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57928906/

相关文章:

java - 打开 Activity 而不打扰用户

javascript - 在创建函数存储在散列中的 JS 对象时,“this”被限定为窗口对象

javascript - js对象和属性

angular - 单击行扩展 prime-ng 的 p 表行

javascript - 错误 TS2304 : Cannot find name 'createNewEvent'

php - 无法在 PHP 中刷新 OAuth2 token ,授予无效

android - 用户退出 : clearing the default Google account does not cause the account picker to show up in Android app

javascript - 我可以将 html5shiv.js 和 respond.min.js 合并到一个文件中吗?

javascript - 使用 JavaScript 过滤 Firebase 查询

angular - 没有ManagedZone的提供者