Detailed Description
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present application. The present application may be embodied in many other forms than those herein described, and those skilled in the art will readily appreciate that the present application may be similarly embodied without departing from the spirit or essential characteristics thereof, and therefore the present application is not limited to the specific embodiments disclosed below.
The terminology used in the one or more embodiments of the application is for the purpose of describing particular embodiments only and is not intended to be limiting of the one or more embodiments of the application. As used in one or more embodiments of the application and the appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It should also be understood that the term "and/or" as used in one or more embodiments of the present application refers to and encompasses any or all possible combinations of one or more of the associated listed items.
It should be understood that, although the terms first, second, etc. may be used in one or more embodiments of the application to describe various information, these information should not be limited by these terms. These terms are only used to distinguish one type of information from another. For example, a first may also be referred to as a second, and similarly, a second may also be referred to as a first, without departing from the scope of one or more embodiments of the application. The term "if" as used herein may be interpreted as "at..once" or "when..once" or "in response to a determination", depending on the context.
It should be noted that, the user information (including but not limited to user equipment information, user personal information, etc.) and the data (including but not limited to data for analysis, stored data, presented data, etc.) related to the present application are information and data authorized by the user or fully authorized by each party, and the collection, use and processing of the related data need to comply with the related laws and regulations and standards of the related region, and provide corresponding operation entries for the user to select authorization or rejection.
First, terms related to one or more embodiments of the present application will be explained.
Network time protocol (Network Time Protocol, NTP) is an application layer protocol within the TCP/IP protocol suite that is used to synchronize clocks between clients and servers, providing highly accurate time correction. The NTP algorithm measures the time difference between the client and the server by sending and receiving the time stamps, calculates the round trip time, i.e. the network delay, and determines the time offset between the two clocks. The client then uses this information to fine tune its own clock to keep pace with the server, and this process is repeated to accommodate changes in network conditions.
Current NTP implementations are primarily focused on providing accurate time synchronization functionality to calibrate the local clock by calculating round trip delay and time offset between client and server through UDP communication. The working principle of the NTP protocol involves exchanging time stamps between clients and servers and calculating the exact time difference by means of a specific algorithm, and the NTP protocol collates the clocks of the clients by calculating the round trip delay and time offset. When facing a scene with non-ideal network conditions, discontinuous speed and slow jitter occurs in real time due to network delay.
Based on this, in the present application, a time synchronization method is provided, and the present application relates to a time synchronization apparatus and system, a computing device, a computer readable storage medium and a computer program product, which are described in detail in the following embodiments one by one.
Fig. 1 shows a flowchart of a time synchronization method according to an embodiment of the present application, where the time synchronization method is applied to a client, and specifically includes the following steps:
And 102, receiving a time synchronization instruction and calculating time offset information between the server and the time synchronization instruction based on the time synchronization instruction.
In this case, in the time-synchronized application scenario, two terminals are involved, one being a client (also called a calibrated terminal) and one being a server (also called a master clock). The method provided by the application is applied to the client and is used for adjusting the time of the client to be synchronous with the server.
A time synchronization instruction may be understood as an instruction for controlling a client to perform time synchronization, which may be triggered automatically based on a certain trigger condition, e.g. once every fixed time period, or once when executing certain specific tasks. Or may be based on instructions sent by a user or other client. In the method provided by the application, the acquisition mode of the time synchronization instruction is not limited, and the method is based on practical application.
The time offset information can be understood as a time offset between the client and the server calculated by a time synchronization algorithm. The time synchronization method provided by the application, the client performs time interaction synchronization with the server based on the received time synchronization instruction, calculates time offset information between the client and the server, specifically calculates the time offset information between the client and the server based on the time synchronization instruction, and comprises the following steps:
Responding to the time synchronization instruction, sending a time synchronization message to a server, and recording a first timestamp of the time synchronization message;
Receiving a synchronous response message returned by the server aiming at the time synchronous message, and recording a fourth time stamp for receiving the synchronous response message, wherein the synchronous response message is recorded with a second time stamp for receiving the time synchronous message by the server and a third time stamp for sending the synchronous response message;
And calculating the current network delay and the current time deviation between the client and the server according to the first time stamp, the second time stamp, the third time stamp and the fourth time stamp.
In a specific embodiment provided by the application, the initial time offset information between the client and the server is calculated and obtained by using an NTP (Network Time Protocol ) algorithm based on the time synchronization instruction. NTP is a protocol that synchronizes device clocks over a computer network. Its main purpose is to ensure that the same time standard can be used by each device in the network to coordinate various network activities. The core function of NTP is to enable network devices to obtain highly accurate and consistent time information to ensure the synchronicity of various network operations.
The NTP architecture employs a master-slave architecture in which one or more NTP servers act as master clock sources, while other devices act as slave clock sources. The host server is typically connected to a high precision clock source, such as an atomic clock, to provide accurate time information. The clock hierarchy involves forming a hierarchy in the network in which devices of each hierarchy provide time information to devices of the previous hierarchy by synchronizing operations, which hierarchy helps ensure that devices in the entire network receive accurate time information.
In practical application, a client sends an NTP message (time synchronization message) to a server, where the time synchronization message carries a first timestamp T1 of the client sending the time synchronization message.
When the time synchronization message arrives at the server, a second timestamp T2 of the time synchronization message received by the server is added in the time synchronization message in the server.
The server gives a corresponding response based on the time synchronization message, namely, sends a synchronization response message based on the time synchronization message, and simultaneously records a third timestamp T3 of the server sending the synchronization response message. The first timestamp T1, the second timestamp T2 and the third timestamp T3 are written into the synchronous response message.
The client receives the synchronous response message and records a fourth timestamp T4 of the synchronous response message.
Up to this point, the first timestamp T1, the second timestamp T2, the third timestamp T3 and the fourth timestamp T4 are acquired in the client. The current network Delay (Delay) and the current time Offset (Offset) of the client with respect to the server can be further calculated from these four time stamps.
Specifically, referring to the following formula 1 and the following formula 2, the current network delay is calculated based on the following formula 1, and the current time deviation is calculated based on the following formula 2.
Delay= (T4-T1) - (T3-T2) formula 1
Offset= ((T2-T1) + (T3-T4))/2 equation 2
Wherein Delay represents the current network Delay of the client with respect to the server, offset represents the current time Offset of the client with respect to the server, T1 is a first time stamp, T2 is a second time stamp, T3 is a third time stamp, and T4 is a fourth time stamp. The current network delay and the current time offset may be regarded as time offset information corresponding to the time synchronization instruction.
Step 104, acquiring historical time offset information under the condition that the time offset information is determined to be abnormal information.
After the time offset information is acquired, it is further determined whether the time offset information is abnormal information. In the method provided by the application, the abnormal information can be understood as the abnormal condition in the time offset information.
In a specific embodiment of the present application, the time offset information includes a current network delay and a current time offset, and in this embodiment, whether the current network delay is abnormal information is further determined, specifically, the method further includes:
Determining that the time offset information is abnormal information under the condition that the current network delay is larger than a preset delay threshold value;
And under the condition that the current network delay is smaller than or equal to the preset delay threshold value, determining the time offset information as normal information.
The preset delay threshold is a delay threshold for determining whether the time offset information is abnormal, and in general, the time offset information includes a current network delay. If the current network delay is greater than the preset delay threshold, the time offset information is described as abnormal information, and if the current network delay is less than or equal to the preset delay threshold, the time offset information can be determined as normal information.
For example, taking the preset delay threshold of the system as 5 ms as an example, if the current network delay in the calculated time offset information in the above step is 3 ms, it is indicated that the time offset information is normal information, and the current client time of the client may be updated directly according to the current time offset in the time offset information. If the current network delay in the calculated time offset information is 6 ms in the above steps, it is indicated that the time offset information is abnormal information, and an operation of acquiring the historical time offset information needs to be performed.
The history time offset information specifically refers to history time offset information corresponding to a plurality of time synchronization instructions preceding the current time synchronization instruction, and for example, time offset information corresponding to the past 10 time synchronization instructions is counted as history time offset information before the current time synchronization instruction. Or counting the time offset information corresponding to the past 20 time synchronization instructions as the historical time offset information.
In practical applications, a data structure with a preset length, such as an array or a data queue, may be set. And storing historical time offset information in a data structure with a preset length, and when the time offset information corresponding to the time synchronization instruction at the current time is determined to be abnormal information, taking out the historical time offset information from the data structure with the preset length.
In another embodiment of the present application, obtaining historical time offset information includes:
Acquiring a historical time deviation array and a historical network delay array, wherein a preset number of historical time deviations are stored in the historical time deviation array, and a preset number of historical network delays are stored in the historical network delay array.
Further, the time offset information includes network delay and time offset. Accordingly, the historical time offset information also further includes historical network delay information and historical time offset information. Meanwhile, the data structure with the preset length can be divided into a network delay data structure and a time deviation data structure, wherein a preset number of historical network delays are stored in the network delay data structure, and a preset number of historical time deviations are stored in the time deviation data structure.
Taking a loop array with a data structure as an example and a length of 50 as a length, a network delay loop array and a time deviation loop array are arranged in the system, wherein the network delay loop array stores network delay corresponding to the past 50 time synchronization instructions, and the time deviation loop array stores time deviation corresponding to the past 50 time synchronization instructions. The loop array stores a preset amount of information, and when new data is added to the loop array, the data which enters the loop array at the earliest time is removed. In the method provided by the application, the preset length of the data structure is even.
And 106, determining a reference time offset interval according to the historical time offset information.
The reference time offset interval may be understood as a range interval for determining whether the time offset information is abnormal. The time offset information may be considered acceptable if it is within the reference time offset interval, and unacceptable if it is outside the reference time offset interval.
Therefore, determining the reference time offset interval is important in the method provided by the present application, in which the reference time offset interval is determined according to the historical time offset information. The historical time offset information records time offset information corresponding to time synchronization instructions of the preset times in the past, and the reference time offset interval can be better and more accurately embodied on the recent network state of the client through the determination of the historical time offset information.
Based on this, in another specific embodiment provided by the present application, determining the reference time offset interval according to the historical time offset information includes:
determining a first time offset and a second time offset in the historical time offset array, and determining a first network delay and a second network delay in the historical network delay array;
determining a reference time deviation interval according to the first time deviation and the second time deviation;
A reference network delay interval is determined from the first network delay and the second network delay.
In practical application, the historical time offset information comprises a historical time offset and a historical network delay, the first time offset and the second time offset are determined from the historical time offset, and the first network delay and the second network delay are determined from historical network delay data.
It should be noted that the first time deviation, the second time deviation, the first network delay and the second network delay are specific values in the historical time deviation information, and the historical time deviation information includes a plurality of historical time deviations and a plurality of network delays, and the corresponding first time deviation, second time deviation, first network delay and second network delay are selected from the historical time deviation information.
In order to better demonstrate the accuracy of selecting the first time offset, the second time offset, the first network delay and the second network delay from the historical time offset information, the first time offset and the second time offset are determined in the historical time offset array, and the first network delay and the second network delay are determined in the historical network delay array, which comprises the following steps:
Sorting the historical time deviations in the historical time deviation array according to the order from small to large, selecting the value of the lower quartile as a first time deviation and the value of the upper quartile as a second time deviation from the time deviation sorting result;
and sequencing the historical network delays in the historical network delay array according to the order from small to large, selecting the value of the lower quartile as the first network delay and the value of the upper quartile as the second network delay from the network delay sequencing result.
In practical application, the historical time deviations in the historical time deviation array are ordered in order from small to large, and the value of the quartile is selected as the first time deviation and the second time deviation according to the time deviation ordering result. Quartiles, also called quartiles, refers to statistically ranking and dividing values from small to large into four equal parts, at 25% and 75% positions. Wherein the value at 25% is the lower quartile and the value at 75% is the upper quartile. The distribution condition of the data can be better understood through quartiles, and the data analysis can be better performed.
Specifically, the value of the lower quartile (the number of 25% positions) is selected from the time bias sequencing result as a first time bias, the value of the upper quartile (the number of 75% positions) is selected as a second time bias, the value of the lower quartile (the number of 25% positions) is selected from the network delay sequencing result as a first network delay, and the value of the upper quartile (the number of 75% positions) is selected as a second network delay.
After determining the first time offset, the second time offset, the first network delay and the second network delay, a reference time offset interval may be determined based on the first time offset and the second time offset, and a reference network delay interval may be determined based on the first network delay and the second network delay.
In practical applications, the first time deviation and the second time deviation may be used as the minimum value and the maximum value of the reference time deviation interval, and the first network delay and the second network delay may be used as the minimum value and the maximum value of the reference network delay interval. The reference time offset interval and the reference network delay interval may also be determined according to an algorithm for time offset intervals provided in embodiments of the present application.
In one embodiment of the present application, determining a reference time deviation interval according to the first time deviation and the second time deviation includes:
Calculating a reference time deviation maximum value according to a preset interval upper bound formula, the first time deviation and the second time deviation;
calculating a minimum value of the reference time deviation according to a preset interval lower bound formula, the first time deviation and the second time deviation;
And determining a reference time deviation interval according to the reference time deviation maximum value and the reference time deviation minimum value.
The preset interval upper bound formula is used for determining the maximum value of a certain interval. Specifically, the preset interval upper bound formula is shown in the following formula 3:
max=d2+v (D2-D1) equation 3
Wherein, max is the maximum value of the interval, D1 is the lower quartile, D2 is the upper quartile, and V is the fluctuation threshold, which is a constant in the method provided by the application.
The maximum value of the interval can be calculated according to the above equation 3. Taking the calculation of the maximum value of the reference time deviation as an example, max is the maximum value of the reference time deviation, D1 is the first time deviation, and D2 is the second time deviation. V is a fluctuation threshold, and is a constant of 1.5 in the method provided by the embodiment of the application. The reference time deviation maximum value can be calculated by the above formula 3.
The preset interval lower bound formula is used for determining the minimum value of a certain interval, and concretely, the preset interval lower bound formula is shown in the following formula 4:
min=d1-V (D2-D1) equation 4
Wherein Min is the minimum value of the interval, D1 is the lower quartile, D2 is the upper quartile, and V is the fluctuation threshold, which is a constant in the method provided by the application.
The minimum value of the interval can be calculated according to the above equation 4. Taking the maximum value of the calculated reference time deviation as an example, min is the minimum value of the reference time deviation, D1 is the first time deviation, and D2 is the second time deviation. V is a fluctuation threshold, and is a constant of 1.5 in the method provided by the embodiment of the application. The reference time deviation minimum value can be calculated by the above formula 4.
The minimum value and the maximum value of the reference time deviation can be calculated by the above-mentioned formulas 3 and 4, and the first time deviation and the second time deviation. And determining the reference time deviation interval according to the reference time deviation minimum value and the reference time deviation maximum value.
In another embodiment provided by the present application, determining a reference network delay interval from the first network delay and the second network delay includes:
Calculating a reference network delay maximum value according to a preset interval upper bound formula, the first network delay and the second network delay;
Calculating a reference network delay minimum value according to a preset interval lower bound formula, the first network delay and the second network delay;
And determining a reference network delay interval according to the reference network delay maximum value and the reference network delay minimum value.
Based on the same calculation as the above-described reference time deviation minimum value and reference time deviation maximum value, the reference network delay maximum value and reference network delay minimum value can be calculated using the above-described equations 3 and 4, and the first network delay and the second network delay, and the reference network delay section can be further determined by the reference network delay maximum value and the reference network delay minimum value.
By the method for determining the reference time deviation interval and the reference network delay interval, the time deviation or the fluctuation interval of the network delay can be better calculated, the recent network fluctuation condition of the client can be counted through the historical time deviation information, and therefore whether the network fluctuation of the time deviation information is in a normal range or not can be better judged, and the subsequent time synchronization processing can be conveniently carried out.
In one embodiment of the present application, determining the reference time offset interval according to the historical time offset information includes:
determining first time offset information and second time offset information in the historical time offset information;
calculating a reference time offset maximum value according to a preset interval upper bound formula, the first time offset information and the second time offset information;
Calculating a reference time offset minimum value according to a preset interval lower bound formula, the first time offset information and the second time offset information;
and determining a reference time offset interval according to the reference time offset maximum value and the reference time offset minimum value.
Further, determining the first time offset information and the second time offset information from the historical time offset information includes:
sorting the historical time offset information according to the order from small to large;
selecting the value of the lower quartile as first time offset information and the value of the upper quartile as second time offset information in the sequencing result.
It should be noted that, for the specific implementation manner of determining the first time offset information and the second time offset information, reference is made to the above description about the first time offset and the second time offset, and for the specific implementation manner of determining the reference time offset interval, reference is made to the above description about the reference time offset interval, which is not repeated herein.
And step 108, updating the current client time of the client based on the time offset information under the condition that the time offset information meets the reference time offset interval.
After the above steps, the reference time offset interval can be obtained. Therefore, whether the time offset information meets the reference time offset interval can be further judged, and if the time offset information meets the reference time offset interval, the current network fluctuation is indicated to be at a normal level. Otherwise, if the time offset information is outside the reference time offset interval, it is indicated that the current network fluctuation is abnormal.
If the current network fluctuation is at a normal level, a synchronization command may be executed to use the calculated time offset information for updating the current client time of the client. If the current network fluctuation is abnormal, the time synchronization instruction is not processed.
In a specific embodiment provided by the application, the reference time offset interval comprises a reference time offset interval and a reference network delay interval, and the time offset information comprises a current network delay and a current time offset;
The method further comprises the steps of:
determining that the time offset information satisfies the reference time offset interval when the current time offset is within the reference time offset interval and the current network delay is within the reference network delay interval;
and determining that the time offset information does not meet the reference time offset interval when the current time offset is outside the reference time offset interval or the current network delay is outside the reference network delay interval.
In practical applications, the reference time offset interval specifically includes a reference time offset interval and a reference network delay interval, and the time offset information specifically includes a current network delay and a current time offset. Specifically, when the current network delay is within the reference network delay interval and the current time deviation is within the reference time deviation interval, it is indicated that the current network fluctuation is at a normal level, and it may be determined that the time offset information satisfies the reference time offset interval.
Otherwise, if the current time deviation is outside the reference time deviation interval or the current network delay is outside the reference network delay interval, the current network fluctuation is at an abnormal level, and the time offset information is determined to not meet the reference time offset interval.
In another embodiment provided by the present application, the method further comprises:
and if the time offset information does not meet the reference time offset interval, not updating the current client time of the client based on the time offset information.
When the time offset information does not meet the reference time offset interval, the time synchronization operation of the response is not executed on the current time synchronization instruction. I.e. the current client time of the client is not updated based on the time offset information.
And under the condition that the time offset information meets the reference time offset interval, updating the current client time of the client according to the time offset information, and further updating the current client time of the client according to the current time deviation in the time offset information.
In practical application, no matter whether the time synchronization instruction is executed or not, the time offset information corresponding to the time synchronization instruction is acquired, the time offset information records the network fluctuation condition between the client and the server under the current condition, and in order to better record the network fluctuation information, data support is provided for subsequent data processing, and the method further comprises:
And updating the historical time offset information according to the time offset information.
In other words, in the method provided by the application, the historical time offset information is updated according to the time offset information corresponding to each time of the time synchronization instruction. Specifically, the time offset information is added to the historical time offset information, and if the number of the historical time offset information is insufficient, the time offset information can be directly added. If the number in the historical time offset information has reached the threshold value, the earliest historical time offset information is taken out from the historical time offset information, and the current time offset information is added to the historical time offset information.
The time synchronization method provided by the embodiment of the application is applied to a client and comprises the steps of receiving a time synchronization instruction, calculating time offset information between the client and a server based on the time synchronization instruction, acquiring historical time offset information under the condition that the time offset information is abnormal information, determining a reference time offset interval according to the historical time offset information, and updating the current client time of the client based on the time offset information under the condition that the time offset information meets the reference time offset interval.
By the method provided by the embodiment of the application, after the current time synchronization instruction is obtained and the time offset information between the client and the server is calculated, the time offset information is further screened to judge whether the time offset information is abnormal information, and if the time offset information is abnormal information, the time offset information is required to be further judged whether the network fluctuation in a normal range is further judged according to the historical time offset information. The current client time is updated based on the time offset information only in case of network fluctuations within the normal range when the time offset information is time shifted, otherwise the current time offset information is discarded. By the method, the application of error time offset information can be effectively reduced, the time jump of the client caused by abnormal fluctuation of the network is avoided, and the continuity and stability of the system time are enhanced.
The time synchronization method is further described below with reference to fig. 2. Fig. 2 is a schematic diagram of a time synchronization method according to an embodiment of the application.
As shown in fig. 2, the present time synchronization method is applied to a client, and the client receives a time synchronization instruction, performs sending a time synchronization message to a server based on the time synchronization instruction, and adds a first timestamp T1 of the client to the time synchronization message.
The server receives the time synchronization message sent by the client and records a second timestamp T2 of the message received by the server in the time synchronization message. The server responds to the time synchronization message to send a synchronization response message to the client, adds the first time stamp and the second time stamp to the synchronization response message, and adds a third time stamp T3 for the server to send the synchronization response message to the synchronization response message. It should be noted that the second timestamp T2 and the third timestamp T3 are server timestamps of the server.
After receiving the synchronization response message sent by the server, the client records a fourth timestamp T4 of the message received by the client. To this end, time offset information for calculating the client compared to the server is collected in the client. The time Offset information specifically includes a current network Delay (Delay) and a current time Offset (Offset).
And in the case that the current network delay is less than or equal to the preset delay threshold, updating the local client clock of the client by using the current time deviation.
And under the condition that the current network delay is larger than a preset delay threshold value, acquiring the historical time deviation and the historical network delay, determining a reference time deviation interval according to the historical time deviation, and determining a reference network delay interval according to the historical network delay.
And further judging whether the current network delay is in a reference network delay interval or not, and whether the current time deviation is in a reference time deviation interval or not. The local client clock of the client is updated with the current time offset if the current network delay is in the reference network delay interval and the current time offset is in the reference time offset interval. Otherwise, the clock synchronization of the current time is not performed.
Referring to fig. 3, fig. 3 shows a flowchart of a time synchronization method applied to a rail transit integrated monitoring system according to an embodiment of the present application, where the method is applied to a traffic object terminal in the rail transit integrated monitoring system. The method specifically comprises the following steps:
Step 302, receiving a time synchronization instruction of the current time, and calculating a current network delay and a current time deviation between the server and the server based on the time synchronization instruction.
Step 304, determining whether the current network delay is greater than a preset delay threshold, if yes, executing step 306, and if not, executing step 314.
Step 306, acquiring a historical time deviation array and a historical network delay array, acquiring at least one historical time deviation from the historical time deviation array, and acquiring at least one historical network delay from the historical network delay array.
Step 308, sorting the historical time deviations from small to large, selecting the value of the lower quartile as the first time deviation, selecting the value of the upper quartile as the second time deviation in the time deviation sorting result, and determining a reference time deviation interval according to a preset interval upper boundary formula and a preset interval lower boundary formula.
Step 310, sorting the historical network delays according to the order from small to large, selecting the lower quartile value as the first network delay and the upper quartile value as the second network delay in the network delay sorting result, and determining the reference network delay interval according to the preset interval upper boundary formula and the preset interval lower boundary formula.
Step 312, determining whether the current time deviation is within the reference time deviation interval, and the current network delay is within the reference network delay interval, if yes, executing step 314, and if no, executing step 316.
And step 314, updating the current terminal time based on the current time deviation.
Step 316, suspending the current time synchronization instruction.
Step 318, adding the current time offset to the historical time offset array and adding the current network delay to the historical network delay array.
By the method provided by the embodiment of the application, after the current time synchronization instruction is obtained and the time offset information between the client and the server is calculated, the time offset information is further screened to judge whether the time offset information is abnormal information, and if the time offset information is abnormal information, the time offset information is required to be further judged whether the network fluctuation in a normal range is further judged according to the historical time offset information. The current client time is updated based on the time offset information only in case of network fluctuations within the normal range when the time offset information is time shifted, otherwise the current time offset information is discarded. By the method, the application of error time offset information can be effectively reduced, the time jump of the client caused by abnormal fluctuation of the network is avoided, and the continuity and stability of the system time are enhanced.
Corresponding to the above method embodiment, the present application further provides an embodiment of a time synchronization device, and fig. 4 shows a schematic structural diagram of a time synchronization device according to an embodiment of the present application. As shown in fig. 4, the apparatus includes:
a receiving module 402 configured to receive a time synchronization instruction and calculate time offset information with a server based on the time synchronization instruction;
An obtaining module 404 configured to obtain historical time offset information in the case where the time offset information is determined to be abnormal information;
A determining module 406 configured to determine a reference time offset interval from the historical time offset information;
an updating module 408 configured to update a current client time of the client based on the time offset information if the time offset information satisfies the reference time offset interval.
Optionally, the receiving module 402 is further configured to:
Responding to the time synchronization instruction, sending a time synchronization message to a server, and recording a first timestamp of the time synchronization message;
Receiving a synchronous response message returned by the server aiming at the time synchronous message, and recording a fourth time stamp for receiving the synchronous response message, wherein the synchronous response message is recorded with a second time stamp for receiving the time synchronous message by the server and a third time stamp for sending the synchronous response message;
And calculating the current network delay and the current time deviation between the client and the server according to the first time stamp, the second time stamp, the third time stamp and the fourth time stamp.
Optionally, the apparatus further includes an anomaly information determining module configured to:
Determining that the time offset information is abnormal information under the condition that the current network delay is larger than a preset delay threshold value;
And under the condition that the current network delay is smaller than or equal to the preset delay threshold value, determining the time offset information as normal information.
Optionally, the obtaining module 404 is further configured to:
Acquiring a historical time deviation array and a historical network delay array, wherein a preset number of historical time deviations are stored in the historical time deviation array, and a preset number of historical network delays are stored in the historical network delay array.
Optionally, the determining module 406 is further configured to:
determining a first time offset and a second time offset in the historical time offset array, and determining a first network delay and a second network delay in the historical network delay array;
determining a reference time deviation interval according to the first time deviation and the second time deviation;
A reference network delay interval is determined from the first network delay and the second network delay.
Optionally, the determining module 406 is further configured to:
Sorting the historical time deviations in the historical time deviation array according to the order from small to large, selecting the value of the lower quartile as a first time deviation and the value of the upper quartile as a second time deviation from the time deviation sorting result;
and sequencing the historical network delays in the historical network delay array according to the order from small to large, selecting the value of the lower quartile as the first network delay and the value of the upper quartile as the second network delay from the network delay sequencing result.
Optionally, the determining module 406 is further configured to:
Calculating a reference time deviation maximum value according to a preset interval upper bound formula, the first time deviation and the second time deviation;
calculating a minimum value of the reference time deviation according to a preset interval lower bound formula, the first time deviation and the second time deviation;
And determining a reference time deviation interval according to the reference time deviation maximum value and the reference time deviation minimum value.
Optionally, the determining module 406 is further configured to:
Calculating a reference network delay maximum value according to a preset interval upper bound formula, the first network delay and the second network delay;
Calculating a reference network delay minimum value according to a preset interval lower bound formula, the first network delay and the second network delay;
And determining a reference network delay interval according to the reference network delay maximum value and the reference network delay minimum value.
Optionally, the reference time offset interval includes a reference time offset interval and a reference network delay interval, and the time offset information includes a current network delay and a current time offset;
the apparatus further includes a determination module configured to:
determining that the time offset information satisfies the reference time offset interval when the current time offset is within the reference time offset interval and the current network delay is within the reference network delay interval;
and determining that the time offset information does not meet the reference time offset interval when the current time offset is outside the reference time offset interval or the current network delay is outside the reference network delay interval.
Optionally, the apparatus further comprises an abort module configured to:
and if the time offset information does not meet the reference time offset interval, not updating the current client time of the client based on the time offset information.
Optionally, the apparatus further comprises an update module configured to:
And updating the historical time offset information according to the time offset information.
By the device provided by the embodiment of the application, after the current time synchronization instruction is obtained and the time offset information between the client and the server is calculated, the time offset information is further filtered to judge whether the time offset information is abnormal information, and if the time offset information is abnormal information, the network fluctuation in the normal range is further judged according to the historical time offset information. The current client time is updated based on the time offset information only in case of network fluctuations within the normal range when the time offset information is time shifted, otherwise the current time offset information is discarded. By the method, the application of error time offset information can be effectively reduced, the time jump of the client caused by abnormal fluctuation of the network is avoided, and the continuity and stability of the system time are enhanced.
Fig. 5 shows a schematic structural diagram of a time synchronization system according to an embodiment of the present application, and as shown in fig. 5, the time synchronization system includes a client 502 and a server 504.
The client 502 is configured to send a time synchronization message to the server 504 based on a time synchronization instruction;
The server 504 is configured to send a synchronization response message to the client 502 in response to the time synchronization message;
The client 502 is further configured to calculate time offset information between the client and the server according to the time synchronization message and the synchronization response message, acquire historical time offset information if the time offset information is determined to be abnormal information, determine a reference time offset interval according to the historical time offset information, and update current client time of the client based on the time offset information if the time offset information meets the reference time offset interval.
According to the system provided by the embodiment of the application, after the current time synchronization instruction is obtained and the time offset information between the client and the server is calculated, the time offset information is further filtered to judge whether the time offset information is abnormal information, and if the time offset information is abnormal information, the network fluctuation in the normal range is further judged according to the historical time offset information. The current client time is updated based on the time offset information only in case of network fluctuations within the normal range when the time offset information is time shifted, otherwise the current time offset information is discarded. By the method, the application of error time offset information can be effectively reduced, the time jump of the client caused by abnormal fluctuation of the network is avoided, and the continuity and stability of the system time are enhanced.
The above is an exemplary scheme of a time synchronization apparatus of the present embodiment. It should be noted that, the technical solution of the time synchronization device and the technical solution of the time synchronization method belong to the same concept, and details of the technical solution of the time synchronization device, which are not described in detail, can be referred to the description of the technical solution of the time synchronization method.
Fig. 6 illustrates a block diagram of a computing device 600 provided in accordance with an embodiment of the present application. The components of computing device 600 include, but are not limited to, memory 610 and processor 620. The processor 620 is coupled to the memory 610 via a bus 630 and a database 650 is used to hold data.
Computing device 600 also includes access device 640, access device 640 enabling computing device 600 to communicate via one or more networks 660. Examples of such networks include public switched telephone networks (PSTN, public Switched Telephone Network), local area networks (LAN, local Area Network), wide area networks (WAN, wide Area Network), personal area networks (PAN, personal Area Network), or combinations of communication networks such as the internet. The access device 640 may include one or more of any type of network interface, wired or wireless, such as a network interface card (NIC, network interface controller), such as an IEEE802.11 wireless local area network (WLAN, wireless Local Area Network) wireless interface, a worldwide interoperability for microwave access (Wi-MAX, worldwide Interoperability for Microwave Access) interface, an ethernet interface, a universal serial bus (USB, universal Serial Bus) interface, a cellular network interface, a bluetooth interface, a Near Field Communication (NFC) interface, and so forth.
In one embodiment of the application, the above-described components of computing device 600, as well as other components not shown in FIG. 6, may also be connected to each other, such as by a bus. It should be understood that the block diagram of the computing device illustrated in FIG. 6 is for exemplary purposes only and is not intended to limit the scope of the present application. Those skilled in the art may add or replace other components as desired.
Computing device 600 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., tablet, personal digital assistant, laptop, notebook, netbook, etc.), mobile phone (e.g., smart phone), wearable computing device (e.g., smart watch, smart glasses, etc.), or other type of mobile device, or a stationary computing device such as a desktop computer or personal computer (PC, personal Computer). Computing device 600 may also be a mobile or stationary server.
Wherein the processor 620 is adapted to execute computer programs/instructions which, when executed by the processor, implement the steps of the time synchronization method described above.
The foregoing is a schematic illustration of a computing device of this embodiment. It should be noted that, the technical solution of the computing device and the technical solution of the time synchronization method belong to the same concept, and details of the technical solution of the computing device, which are not described in detail, can be referred to the description of the technical solution of the time synchronization method.
An embodiment of the present disclosure also provides a computer-readable storage medium storing a computer program/instruction that, when executed by a processor, implements the steps of the above-described time synchronization method.
The above is an exemplary version of a computer-readable storage medium of the present embodiment. It should be noted that, the technical solution of the storage medium and the technical solution of the time synchronization method belong to the same concept, and details of the technical solution of the storage medium, which are not described in detail, can be referred to the description of the technical solution of the time synchronization method.
An embodiment of the present specification also provides a computer program product comprising computer programs/instructions which, when executed by a processor, implement the steps of the above-described time synchronization method.
The foregoing is a schematic version of a computer program product of this embodiment. It should be noted that, the technical solution of the computer program product and the technical solution of the time synchronization method belong to the same concept, and details of the technical solution of the computer program product, which are not described in detail, can be referred to the description of the technical solution of the time synchronization method.
The foregoing describes certain embodiments of the present application. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims can be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
The computer instructions include computer program code that may be in source code form, object code form, executable file or some intermediate form, etc. The computer readable medium may include any entity or device capable of carrying the computer program code, a recording medium, a U disk, a removable hard disk, a magnetic disk, an optical disk, a computer Memory, a Read-Only Memory (ROM), a random access Memory (RAM, random Access Memory), an electrical carrier signal, a telecommunications signal, a software distribution medium, and so forth. It should be noted that the content of the computer readable medium can be increased or decreased appropriately according to the requirements of the patent practice, for example, in some areas, according to the patent practice, the computer readable medium does not include an electric carrier signal and a telecommunication signal.
It should be noted that, for the sake of simplicity of description, the foregoing method embodiments are all expressed as a series of combinations of actions, but it should be understood by those skilled in the art that the present application is not limited by the order of actions described, as some steps may be performed in other order or simultaneously in accordance with the present application. Further, those skilled in the art will appreciate that the embodiments described in the specification are all preferred embodiments, and that the acts and modules referred to are not necessarily all required for the present application.
In the foregoing embodiments, the descriptions of the embodiments are emphasized, and for parts of one embodiment that are not described in detail, reference may be made to the related descriptions of other embodiments.
The preferred embodiments of the application disclosed above are intended only to assist in the explanation of the application. Alternative embodiments are not intended to be exhaustive or to limit the application to the precise form disclosed. Obviously, many modifications and variations are possible in light of the above teaching. The embodiments were chosen and described in order to best explain the principles of the application and the practical application, to thereby enable others skilled in the art to best understand and utilize the application. The application is limited only by the claims and the full scope and equivalents thereof.