Delphi gets Greenwich and converts to the GMT

Source: Internet
Author: User
Tags split
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.