zoukankan      html  css  js  c++  java
  • 本地同步到网络时间

    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.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    009——数组(九) each list array_map array_walk array_walk_recursive
    008——数组(八)删除添加数组 得到数组键名键值
    laravel怎么获取到public路径
    laravel中composer镜像服务的方式
    logback的使用和logback.xml详解
    log4j-over-slf4j工作原理详解
    java
    【Log】logback指定配置文件(二)
    logback 中文手册
    logback logback.xml常用配置详解(三) <filter>
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12169881.html
Copyright © 2011-2022 走看看