How to synchronize the client time (this multi-layer architecture system should be such) multi-layer system development, how to keep the client and database or application server time synchronization is a problem that must be resolved, I found that many systems get the client's local time, It is different from multiple clients to operate simultaneously. Many people use frequent forms that are frequently taken from the database, increase server load pressure. Of course, some people say that when data is saved, you should take the time in the database, not bad, but involve the operation between the local data, you should not go to the database. In view of this, this article uses an API function of the Windows system to synchronize. Specific reference category: (Delphi syntax implementation, of course)
Note: The GetTickCount API has a limit, which is clear after 49 days. If you don't do it, you will make an error ****** call -------------- Var mydatetime: TMYDATETIME ; // global variables for the entire project // -------------------------------------- -------------- Procedure InitialDateTimeWhensystemStart; // ------------------------------------------------------------------------------------------------------------------------ String MyDateTime.AppServerDataTiMestr: = GetServerDateTimeStr; MyDateTime.initialDateTimeSpan; // Initialization End; // ------------------------------ ---------------------------- TMYDATETIME = Class Private // fdatetimeInterval: double; fdatetimeinterval: tdatetime; FAPPSERVERDATASTR: STRING
FServerDateTime: TDATETIME; / / The time for the first login server fsystickcount: Double;
Procedure setDateTime; function getdateTime_: tdatetime; advance constructor create; procedure initialdatetimespan; // --- Interval of initialization local time and system time
Property CurrentDataTime: TDATETIME read getdatetime_;
Property AppserDataTiMestr: String Read FAPPServerDataTimestr Write FAPPServerDataTiMestr; End; Implement {TMYDATETIME}
function TmyDateTime.GetDateTime_: TDateTime; var FSysTickCount_: Double; begin FSysTickCount_: = GetTickCount; Result: = FServerDateTime (FSysTickCount_ - FSysTickCount) / (24 * 60 * 60 * 1000); {if FDateTimeInterval <0 then Result: = (Now - fdatetimeInterval) Else Result: = (now fdatetimeInterval);
Procedure TMYDATETIME.INITIALDATETIMESPAN; // --- Initialization Local Time and System Time Var Wyear, WMONTH, WDAY, WHOR, WMINUTE, WSECOND, WMILI: Word; Begin Try Wyear: = STRTOINT (Copy (FAPPSERVERDATIMESTR, 1, 4) ); Wmonth: = start (Copy (FAPPServerDataTiMestr, 5, 2)); wday: = start (Copy (FAPPSERVERDATASTR, 7, 2)); WHOUR: = STRTOINT (Copy (FAPPSERVERDATIMESTR, 9, 2); wminute: = StrToInt (copy (FAppserverDataTimeStr, 11, 2)); wSecond: = StrToInt (copy (FAppserverDataTimeStr, 13, 2)); wMili: = 100; FServerDateTime: = EncodeDateTime (wYear, wMonth, wDAy, wHour, wMinute, wSecond, wMili ); Fsystickcount: = gettickcount; // fdatetimeInterval: = MilliseCondspan (now, fserverDatetime); // fDateTimeInterval: = now - fserverDateTime; Except // fdatetimeInterval: = 0; end; end;
Procated TMYDATETIME.SETDATIME; VAR WYEAR, WMONTH, WDAY, WHOR, WAMINUTE, WORD, WMILISENCOND: WORD; SMONTH, SDAY, SHOUR, SAMINUTE, SSECOND: STRING
ExactDateTime: TDateTime; FSysTickCount_: Double; begin // ExactDateTime: = FloatToDateTime (Now FDateTimeInterval); {if FDateTimeInterval <0 then ExactDateTime: = (Now - FDateTimeInterval) else ExactDateTime: = (Now - FDateTimeInterval);} // ExactDateTime: = (Now - FDateTimeInterval); FSysTickCount_: = GetTickCount; ExactDateTime: = FServerDateTime (FSysTickCount_ - FSysTickCount) / (24 * 60 * 60 * 1000); DecodeDateTime (ExactDateTime, wyear, wmonth, wday, whour, waminute, wsecond, wmilisencond SMONTH: = INTOSTR (WMONTH); if Length (SMONTH) <2 THEN SMONTH: = '0' smhood; sday: = INTOSTR (WDAY); if longth (sday) <2 Then SDAY: = '0' SDAY; SHOUR: = INTOSTOSTR (WHOUR); if longth (shour) <2 Then Shour: = '0' shour; saminute: = INTOSTR (Waminute); if longth (saminute) <2 Then Saminute: = '0' Saminute; ssecond: = INTOSTR (WSECOND); if longth (ssecond) <2 Then Ssecond: = '0' ssecond; fappserverDataTiMestr: = INTOSTR (Wyear) SMONTH SDAY SHOUR SAMINUTE SSECOND; // smilisencond;