then S:=Copy(TimeStr,First,Last-First) // 获取一字符
else S:=Copy(TimeStr,First,Last-First+1); // 包含最后字符
S:=Trim(S); // 取消两端空格
if S<>'' then
begin
Top:=Top+1;
// showmessage(IntToStr(Top)+': '+S);
if Top=2 then // 处理月
begin
if (S='一月') or (S='Jan') then iMonth:=1;
if (S='二月') or (S='Feb') then iMonth:=2;
if (S='三月') or (S='Mar') then iMonth:=3;
if (S='四月') or (S='Apr') then iMonth:=4;
if (S='五月') or (S='May') then iMonth:=5;
if (S='六月') or (S='Jun') then iMonth:=6;
if (S='七月') or (S='Jul') then iMonth:=7;
if (S='八月') or (S='Aug') then iMonth:=8;
if (S='九月') or (S='Sep') then iMonth:=9;
if (S='十月') or (S='Oct') then iMonth:=10;
if (S='十一月') or (S='Nev') then iMonth:=11;
if (S='十二月') or (S='Dec') then iMonth:=12;
end;
if Top=3 then // 处理日
iDay:=StrToInt(S);
if Top=4 then // 处理年
iYear:=StrToInt(S);
if Top=5 then // 处理时
iH:=StrToInt(S);
if Top=6 then // 处理分
iM:=StrToInt(S);
if Top=7 then // 处理秒
iSS:=StrToInt(S);
end;
end;
end;
end;
// 下面分别向SysTime分解字符串的值
SysTime.wYear:=iYear;
SysTime.wMonth:=iMonth;
SysTime.wDay:=iDay;
SysTime.wHour:=iH;
SysTime.wMinute:=iM;
SysTime.wSecond:=iSS;
SysTime.wMilliseconds:=0;
// 对获取的SysTime时间值 进行修正
SysTime.wYear:=SysTime.wYear; // 加入日期偏差
SysTime.wHour:=SysTime.wHour-hBias; // 加入小时偏差
SysTime.wMinute:=SysTime.wMinute-mBias; // 加入小时偏差
Result:=FormatDateTime('yyyy-mm-dd hh:nn:ss zzz',SystemTimeToDateTime(SysTime));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IdDayTime1.Host := Edit1.Text; // 连接主机
IdDayTime1.Port := StrToIntDef(Edit2.Text,strtoint(Edit2.Text)); // 端口
end;
procedure TForm1.Button2Click(Sender: TObject);
var sDateTime:string;
DateTime:TDateTime;
SystemTime: TSystemTime;
begin
DateSeparator:='-';
ShortDateFormat:='yyyy-MM-dd';
LongDateFormat:='yyyy''年'',MM''月'',dd''日''';
TimeSeparator:=':';
sDateTime:=IdDayTime1.DayTimeStr;
sDateTime:=CopyDateTime(sDateTime);
Edit4.Text:=sDateTime;
DateTime:=StrToDateTime(SDateTime); // 获得时间(TDateTime格式)
DateTimeToSystemTime(DateTime,systemtime); // TDateTime格式转化TSystemTime
SetLocalTime(SystemTime); // 设置系统时间
GetLocalTime(SystemTime); // 读取系统时间
DateTime:=SystemTimeToDateTime(systemtime); // TSystemTime格式转化TDateTime
Edit5.Text:=DateTimetoStr(DateTime);
end;
procedure TForm1.Timer1Timer(Sender: TObject); // 定时同步
var sDateTime:string;
DateTime:TDateTime;
SystemTime: TSystemTime;
begin
sDateTime:=IdDayTime1.DayTimeStr;
sDateTime:=CopyDateTime(sDateTime);
DateTime:=StrToDateTime(SDateTime); // 获得时间(TDateTime格式)
DateTimeToSystemTime(DateTime,systemtime); // TDateTime格式转化TSystemTime格式
SetLocalTime(SystemTime); // 设置系统时间
StatusBar1.Panels[1].Text:='校正时间:'+sDateTime;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then
begin
Button1Click(Sender);
Timer1.Enabled:=True;
end
else
Timer1.Enabled:=False;
end;
————————————————
原文链接:https://blog.csdn.net/lyhoo163/article/details/82950824