Java教程

Delphi 获取网络时间 稳定方法

本文主要是介绍Delphi 获取网络时间 稳定方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
unit InternetTime;

interface

uses
System.Net.HttpClientComponent, System.SysUtilS, System.DateUtils,
Winapi.Windows;

function GetInternetTime(var _Time: TDateTime): Boolean;

function InternetTimeToSystemTimeW(pSour: LPWSTR; var pst: SYSTEMTIME; dwReserved: DWORD): BOOL; stdcall; external 'wininet.dll';

implementation

function GetInternetTime(var _Time: TDateTime): Boolean;
var
NetHTTPClient: TNetHTTPClient;
_InternetTime: string;
_SystemTime: SYSTEMTIME;
begin
Result := False;
try
NetHTTPClient := TNetHTTPClient.Create(nil);
try
NetHTTPClient.ConnectionTimeout := 3000;
_InternetTime := NetHTTPClient.Head('https://www.baidu.com/').GetHeaderValue('Date');
finally
NetHTTPClient.Free;
end;

if _InternetTime <> '' then
begin
_SystemTime := default(SYSTEMTIME);
if InternetTimeToSystemTimeW(@_InternetTime[1], _SystemTime, 0) then
begin
_Time := IncHour(SystemTimeToDateTime(_SystemTime), 8);
Result := True;
end;
end;
except

end;

end;

end.

  

 

调用百度接口基本不会出问题 

 

这篇关于Delphi 获取网络时间 稳定方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!