本地同步到网络时间

Posted tobetterlife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了本地同步到网络时间相关的知识,希望对你有一定的参考价值。

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.

以上是关于本地同步到网络时间的主要内容,如果未能解决你的问题,请参考以下文章

本地代码上传同步到github

Android:处理同步网络调用

Centos同步网络yum源到本地

[CentOS 7系列]远程数据同步

根据图片的url地址下载图片到本地保存代码片段

centos7矫正时间与网络时间同步