本地同步到网络时间

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdTime, IdUDPBase, IdUDPClient, IdSNTP, IdTimeUDP, IdHTTP;

type
  TForm1 = class(TForm)
    btn1: TButton;
    IdHTTP1: TIdHTTP;
    mmo1: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses DateUtils;

procedure TForm1.btn1Click(Sender: TObject);
var
  IdHttp : TIdHTTP;
  Url : string;//请求地址
  ResponseStream : TStringStream; //返回信息
  ResponseDateTime : string; //服务器生成网页的时间
  ResponseStr:string;//服务器返回的网页信息

  sl:TStringList;
  syear,smonth,sday,stime:string;
  i:integer;
  dDate:TDateTime;
  settings: TFormatSettings;

  systemtime:Tsystemtime;
begin
  IdHttp := TIdHTTP.Create(nil); //创建IDHTTP控件
  ResponseStream := TStringStream.Create('');   //TStringStream对象用于保存响应信息
  sl:=TStringList.Create;
  try
    Url := 'http://dict.youdao.com/';  //请求地址
    try
      IdHttp.Get(Url,ResponseStream);
    except
      on e : Exception do
      begin
        ShowMessage(e.Message);
      end;
    end;
    ResponseDateTime := IdHttp.Response.RawHeaders.Values['Date']; //获取网页的网络时间
    ResponseStr := ResponseStream.DataString;
    ResponseStr := UTF8Decode(ResponseStr); //网页中的存在中文时,需要进行UTF8解码
    self.Caption:= ResponseDateTime;

    {Fri
      22
      Feb
      2019
      06:37:39
      GMT
      }
    sl.DelimitedText:= StringReplace(ResponseDateTime,'GMT','',[rfReplaceAll]);
    //mmo1.Lines.Assign(sl);

    syear:= sl[3];
    smonth:= sl[2];
    sday:= sl[1];

    if smonth = 'Jan' then
      smonth := '01'  
    else if smonth = 'Feb' then  
      smonth := '02'  
    else if smonth = 'Mar' then  
      smonth := '03'  
    else if smonth = 'Apr' then  
      smonth := '04'  
    else if smonth = 'Mar' then  
      smonth := '05'  
    else if smonth = 'Jun' then  
      smonth := '06'  
    else if smonth = 'Jul' then  
      smonth := '07'
    else if smonth = 'Aug' then  
      smonth := '08'  
    else if smonth = 'Sep' then  
      smonth := '09'  
    else if smonth = 'Oct' then  
      smonth := '10'  
    else if smonth = 'Nov' then
      smonth := '11'  
    else if smonth = 'Dec' then  
      smonth := '12';


    stime:= sl[4];
    Self.Caption:= syear+'-'+smonth+'-'+sday+' '+stime;


    GetLocaleFormatSettings(GetUserDefaultLCID, settings);
    settings.DateSeparator := '-';
    settings.TimeSeparator := ':';
    settings.ShortDateFormat := 'yyyy-mm-dd';
    settings.ShortTimeFormat := 'hh:nn:ss';


    dDate:= StrToDateTime(self.Caption,settings);
    ddate:= IncHour(ddate,8); //转换时区
    self.Caption:= DateTimeToStr(ddate);

    //设置时间
    SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, 'yyyy.MM.dd');
    Application.UpdateFormatSettings := False;
    DateTimeToSystemTime(ddate,systemtime);
    SetLocalTime(systemtime);
  finally
    sl.Free;
    IdHttp.Free;
    ResponseStream.Free;
  end;
end;

{procedure TForm1.btn1Click(Sender: TObject);
var
 systemtime:Tsystemtime;
  DateTime:TDateTime;
begin
  SetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SSHORTDATE, 'yyyy:MM:dd');
  Application.UpdateFormatSettings := False;
  DateTime:=Now;
  DateTime:= IncMinute(DateTime,-1);
  DateTimeToSystemTime(DateTime,systemtime);
  SetLocalTime(systemtime);
end;}
end.
posted @ 2020-01-09 10:08  创新创造学习整合套路  阅读(194)  评论(0编辑  收藏  举报