System time and standard time proofreading of Int network

Posted by arctushar on Fri, 20 Dec 2019 22:12:42 +0100

I. significance of obtaining standard time

When we are programming, for data approval, we need to calibrate parameters with date time values and works. Therefore, it is the best way to keep the client time consistent with the server time and keep their time consistent with the standard time.

II. Technical preparation

1. System time parameter, which is used to set local time

 TSystemTime = record
                                 wYear: Word;
                                 wMonth: Word;
                                 wDayOfWeek: Word;
                                 wDay: Word;
                                 wHour: Word;
                                  wMinute: Word;
                                  wSecond: Word;
                                  wMilliseconds: Word;
                              end; 

2. Time zone information is the parameter of standard time

3. GetTimeZoneInformation(tzInfo) obtains the local time (time zone setting) information of the system
4. TidDayTime control, which can specify the Host to get the standard time value (string), IdDayTime1.DayTimeStr. The format of the string value is as follows:

58396 18-10-05 05:06:04 31 0 0 691.8 UTC(NIST) *

We can get the value of relevant date and time through this string.

Note: for this string, only 2-bit-year values can be obtained. For example, in the 21st century, 2000 will be added.

I tested IdDayTime1.DayTimeStr in XE10. The format is the same, so I don't understand. Why don't I return 4-digit annual value? How nice to change the new version to 4 digits!

5. Set local time (systime), set local time (API)

3. The implementation code is as follows

function TForm1.getDateTime(Host:string):string;          // Time acquisition and synchronization with the network
var IdDayTime1: TIdDayTime;
    tzInfo: Time_Zone_Information;       // system parameter
    hBias,                               // Hour deviation
    mBias: Integer;                      // Minute deviation
    TimeStr:String;                      // Time character
    SysTime: TSystemTime;
                                        // TSystemTime is a system defined structure with 16 bytes:
                                        {  TSystemTime = record
                                             wYear: Word;
                                             wMonth: Word;
                                             wDayOfWeek: Word;
                                             wDay: Word;
                                             wHour: Word;
                                             wMinute: Word;
                                             wSecond: Word;
                                             wMilliseconds: Word;
                                           end; }
begin
  GetTimeZoneInformation(tzInfo);                // Get system local time (time zone setting) information
  hBias:=tzInfo.Bias div 60;                     // Get local time zone and Greenwich small time difference
  mBias:=tzInfo.Bias mod 60;                     // Get the minute difference between the local time zone and Greenwich
  IdDayTime1:=TIdDayTime.Create(Application);    // Create IdDayTime
  try
    IdDayTime1.Host:=Host;                       // Get time from hotspot
    TimeStr:=Trim(IdDayTime1.DayTimeStr);        // Return time string
    Edit5.Text:=IdDayTime1.DayTimeStr;
    Edit6.Text:=IntTostr(Trunc(Now));

// The values of the strings are decomposed to SysTime as follows
    SysTime.wYear:=StrToInt(Copy(TimeStr,7,2));  // Version problem year only takes 2 digits, and 2000 will be added
    SysTime.wMonth:=StrToInt(Copy(TimeStr,10,2));
    SysTime.wDay:=StrToInt(Copy(TimeStr,13,2));
    SysTime.wHour:=StrToInt(Copy(TimeStr,16,2));
    SysTime.wMinute:=StrToInt(Copy(TimeStr,19,2));
    SysTime.wSecond:=StrToInt(Copy(TimeStr,22,2));
    SysTime.wMilliseconds:=StrToInt(Copy(TimeStr,32,3));
  except
    Showmessage('Failed to get time from!');
    Exit;
  end;
                                                 // Modify the acquired SysTime time value
  SysTime.wYear:=SysTime.wYear+2000;             // Add date deviation
  SysTime.wHour:=SysTime.wHour-hBias;            // Add hour deviation
  SysTime.wMinute:=SysTime.wMinute-mBias;        // Add hour deviation
  SetLocalTime(SysTime);                         // Set local time
  Result:=FormatDateTime('yyyy-mm-dd hh:nn:ss zzz',SystemTimeToDateTime(SysTime));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 IdDayTime1.Host := Edit1.Text; //Connecting hosts
 IdDayTime1.Port := StrToIntDef(Edit2.Text,strtoint(Edit2.Text)); //port
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Edit1.Text:=getDateTime('time.nist.gov');
end;

IV. test results, D7 and XE10.2, passed. Success depends on the return rate of the site.

Topics: Programming network