当前位置:首页 > Windows程序 > 正文

Delphi日期时间 UNIX

2021-03-26 Windows程序

-------------------------------------------------------------------

标准UTC时间, 

世界统一时间,世界标准时间,国际协调时间,简称UTC 

不属于任意时区 

中国大陆、中国香港、中国澳门、中国台湾、蒙古国、新加坡、马来西亚、菲律宾、西澳大利亚州的时间与UTC的时差均为+8,也就是UTC+8。

-------------------------------------------==============

时区,北京是东8区,就是要加 8个小时,8*60*60=28800

注册表有个日期值,1398998255

procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Clear; with TRegistry.Create(KEY_WOW64_64KEY or KEY_ALL_ACCESS) do try RootKey := HKEY_LOCAL_MACHINE; if OpenKey(SOFTWARE\Microsoft\Windows NT\CurrentVersion, False) then begin Memo1.Lines.Add(IntToStr(ReadInteger(InstallDate)));//1398998255 Memo1.Lines.Add(DateTimeToStr(UnixToDateTime(ReadInteger(InstallDate))));//2014-05-02 2:37:35 Memo1.Lines.Add(DateTimeToStr(UnixToDateTime(ReadInteger(InstallDate) + 28800))); //2014-05-02 10:37:35 Memo1.Lines.Add(DateTimeToStr(JavaToDelphiDateTime(ReadInteger(InstallDate))));// 1970-01-17 12:36:38 end; finally Free end; end;

Unix时间戳转换成Delphi的TDateTime function UnixDateToDateTime(const USec: Longint): TDateTime; const cUnixStartDate: TDateTime = 25569.0; // 1970/01/01 begin Result := (Usec / 86400) + cUnixStartDate; end;

{ Unix date conversion support } RTL

HoursPerDay = 24;
MinsPerHour = 60;
SecsPerMin = 60;
MSecsPerSec = 1000;
MinsPerDay = HoursPerDay * MinsPerHour;
SecsPerDay = MinsPerDay * SecsPerMin;
MSecsPerDay = SecsPerDay * MSecsPerSec;

{ Days between 1/1/0001 and 12/31/1899 }

DateDelta = 693594;

{ Days between TDateTime basis (12/31/1899) and Unix time_t basis (1/1/1970) }

UnixDateDelta = 25569;


function DateTimeToUnix(const AValue: TDateTime): Int64; begin Result := Round((AValue - UnixDateDelta) * SecsPerDay); end; function UnixToDateTime(const AValue: Int64): TDateTime; begin Result := AValue / SecsPerDay + UnixDateDelta; end;

Unix时间戳(Unix timestamp)转换工具

uses DateUtils;
DateTimeToUnix(Now)

 

可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60
这是因为Now是当前时区的时间,,c语言中time()是按格林威治时间计算的,
北京时间比格林威治时间多了8小时
DateTimeToUnix(Now)-8*60*60 就和c语言中time()得到的一样了 

 

但我进一步研究DateTimeToUnix函数时发现,Delphi中的时间没有经过任何的转换,是直接读取系统时间

我试着改变当前计算机时区,发现Delphi返回值没有任何改变。

而在C++中,改变计算机时区,则time()返回值也随着改变,这就说明C++中标准时间是通过本地时间和当前时区进行计算得来的。

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/68084.html