Uses Winapi.msxml, system.dateutils; The function that obtains the network time in real time, obtains is the GMT;
The default is obtained from the Sohu server because it is the fastest, averaging only 15 milliseconds function getnettime (aurl:widestring = ' http://www.sohu.com '): string;
Begin with Coxmlhttp.create do Beginopen (' Post ', Aurl, False, Emptyparam, Emptyparam);
Send (Emptyparam);
Result: = getResponseHeader (' Date ');
End
End
GMT (string) conversion to Beijing time function gmt2bjdatetime (const gmt:string): Tdatetime;
var a:tarray<string>; Begin A: = GMT. Split ([', ', '], excludeempty); XE4 support with tstringlist.create dobegincommatext: = ' jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,n
Ov=11,dec=12 ';
A[2]: = values[a[2]];
Free;
End
Result: = Strtodatetime (Format ('%s/%s/%s%s ', [a[3], a[2], a[1], a[4]), Formatsettings.create (2052)); Result: = result + 8/24;
Converted into Beijing time end;
Test procedure Tform1.button1click (sender:tobject);
var strgmt:string;
Bjdatetime:tdatetime;
Begin STRGMT: = Getnettime ();
Bjdatetime: = Gmt2bjdatetime (STRGMT);
Showmessagefmt ('%s ' #13 #10 '%s ', [Strgmt, Datetimetostr (bjdatetime)]);
End
In real practice, I replaced the Gmt2bjdatetime function with:
function Gmt2bjdatetime (const GMT:
string): Tdatetime;
var
a:tarray<string>;
Y,m,d,h,n,s:word;
Begin
A: = GMT. Split ([', ', ', ', ': '], excludeempty);
With tstringlist.create dobegincommatext: =
' Jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov =11,dec=12 ';
A[2]: = Values[a[2]];
Free;
End;
Y: = Strtointdef (A[3], yearof (now));
M: = Strtointdef (a[2], Monthof (now));
D: = Strtointdef (A[1], dayof (now));
H: = Strtointdef (A[4], hourof (now));
N: = Strtointdef (A[5], minuteof (now));
S: = Strtointdef (A[6], secondof (now));
Result: = Encodedatetime (Y, M, D, H, N, S, 0);
Result: = result + 8/24; Converted into Beijing time end;