The invention is to overcome the shortcomings of the client and time synchronization in the prior art, and provide an implementation method based on the system clock and the time service relying on the automatic repair of the server which can not rely on the reference time and effectively reduce the error. The steps of the method include: the first time the customer service terminal calls the interface of the server, the acquisition time is T1; at the same time, the server obtains the current running time from the current system startup to the current running time through the systemclock class; when the client obtains the current time, the acquisition time from the current system startup to the current running time through the systemclock class is T3; the calculation of the current time of the client, t = T1 + (T3 And calculate the current time of the server t = t + X.
【技术实现步骤摘要】
一种基于SystemClock,并且依赖服务端自动修复的时间服务的实现方法
本专利技术涉及互联网
,尤其涉及客户端和服务端时间同步
技术介绍
计算机安全领域大部分使用时间来做通信过期的标识,服务端和客户端时间误差尽可能的小,有利于避免出现别人恶意攻击导致的软件安全问题。目前比较常用的方法就是根据基准时间使用setTimeout或setInterval来计算最新的时间,这样的问题在于setTimeout与setInterval的时间精度比较低,经测试一分钟大概能相差几秒(与电脑性能以及运行的任务也相关),这样的精度在某些需求下是无法满足的。除此之外,如果要获得比较准确的时间可以定期与服务器进行校准,只是这样实现的成本大一些。因为目前需要的是减小服务端和客户端之间的时间误差,因此本专利技术提供一种能够不依赖于基准时间,并且有效减少误差的服务端自动修复的时间服务的实现方法。
技术实现思路
本专利技术是为了克服现有技术中的客户端和时间同步的不足之处,提供一种能够不依赖于基准时间,并且有效减少误差的服务端自动修复的基于SystemClock,并且依赖服务端自动修复的时间服务的实现方法。为实现上述目的,本专利技术采用以下技术方案:本专利技术的一种基于SystemClock,并且依赖服务端自动修复的时间服务的实现方法,其特征是,所述方法包括以下步骤:客服端第一次调用服务端的接口获取的时间为t1;同时服务端通过SystemClock类获取当前 ...
【技术保护点】
1.一种基于System Clock,并且依赖服务端自动修复的时间服务的实现方法,其特征是,所述方法包括以下步骤:/n客服端第一次调用服务端的接口获取的时间为t1;/n同时服务端通过SystemClock类获取当前系统开机启动到当前的运行时间为t2;/n当客户端获取当前时间时,在通过SystemClock类获取当前系统的开机启动到当前的运行时间为t3;/n计算客户端当前时间t=t1+(t3-t2),并计算服务端当前时间T=t+x。/n
【技术特征摘要】
1.一种基于SystemClock,并且依赖服务端自动修复的时间服务的实现方法,其特征是,所述方法包括以下步骤:
客服端第一次调用服务端的接口获取的时间为t1;
同时服务端通过SystemClock类获取当前系统开机启动到当前的运行时间为t2;
当客户端获取当前时间时,在通过SystemClock类获取当前系统的开机启动到当前的运行时间为t3;
计算客户端当前时间t=t1+(t3-t2),并计算服务端当前时间T=t+x。
2.根据权利要求1所述的方法,其特征是,所述x为服务端取到时间之后返回给客户端到系统SystemClock获取到结果的时间,误差与网络有关,在网络良好的情况下,x大约为0.5s以内。
3.根据权利要求1所述的方法,其特征是,所述步骤之后还包括以下步骤:
客户端每次请求接口的时候,在请求参数中都会带着客户端时间t,如果服务端判断该时间已超过服务端所允许的时间误差范围t4,则判断客户端时间需要修正。
4.根据权利要求1所述的方法,其特征是,所述步骤之后还包括以下步骤:
若判断客户端时间需要修正,服务器则会返回服务端当前时间T;
客户端根据服务端当前时间T进行修正本地时间。
5.一种基于SystemClock,...
【专利技术属性】
技术研发人员:曹玉鹏,
申请(专利权)人:杭州涂鸦信息技术有限公司,
类型:发明
国别省市:浙江;33
还没有人留言评论。发表了对其他浏览者有用的留言会获得科技券。