delphi unix时间戳 10位 13位 转换


function DateTimeToUnix(ConvDate: TDateTime;Len:integer=10): string;
var
  zi : TTimeZoneInformation;
  unixtime:Int64;
  ATimeStamp: TTimeStamp;
  timezzz:string;
begin
 GetTimeZoneInformation(zi);
 unixtime := Round((ConvDate - UnixDateDelta) * 86400) + 60*(zi.Bias + zi.DaylightBias);
 Result := IntToStr(unixtime);
 if Len=13 then
 begin
  ATimeStamp := DateTimeToTimeStamp(ConvDate);
  timezzz := IntToStr(ATimeStamp.Time);
  timezzz := Copy(timezzz,Length(timezzz)-2,Length(timezzz));
  Result := Result+timezzz;
 end;
end;
 
function UnixToDateTime(USec: string;len:integer=10): string;
var
  zi : TTimeZoneInformation;
  atime:Int64;
  adatetime:TDateTime;
begin
  atime := StrToInt64(Copy(USec,1,10));
  GetTimeZoneInformation(zi);
  adatetime := ((atime - 60*zi.Bias) / 86400) + UnixDateDelta;

  Result := FormatDateTime('yyyy-dd-mm hh:mm:ss', adatetime);
  if Length(USec) = 13 then
    Result := FormatDateTime('yyyy-dd-mm hh:mm:ss', adatetime)+'.'+Copy(USec,11,13);
end;

 

//获取UnixTime 时间 主要跟上面区别的是在获取本地时区上
function GetUnixTime:string;
 //获取本地时区差
 function LocalTimeZoneBias: Integer;
 var
   TimeZoneInformation: TTimeZoneInformation;
   Bias: Longint;
 begin
   case GetTimeZoneInformation(TimeZoneInformation) of
     TIME_ZONE_ID_STANDARD: Bias := TimeZoneInformation.Bias + TimeZoneInformation.StandardBias;
     TIME_ZONE_ID_DAYLIGHT: Bias := TimeZoneInformation.Bias + ((TimeZoneInformation.DaylightBias div 60) * -100);
   else
     Bias := TimeZoneInformation.Bias;
   end;
   Result := Bias;
 end;
begin
 Result := inttostr(Round((Now - UnixDateDelta) * 86400)+60*LocalTimeZoneBias );
end;

 

posted @ 2021-12-22 17:03  Tag  阅读(391)  评论(0编辑  收藏  举报