2017-08-07 21 views
0

我找不到解決方案將以下代碼轉換爲在OSX下運行。有誰知道我可以在OSX下獲得unix時間戳(UTC)嗎?Delphi Firemonkey OSX如何轉換下面的代碼?

function GetUnixTimestamp: int64; 
var 
    st: TSystemTime; 
    dt: TDateTime; 
    ut: int64; 
begin 
    dt := SystemTimeToDateTime(st); 
    ut := DateTimeToUnix(dt); 
    result := ut; 
end; 

回答

1

沒關係,我只是普通的笨......下面的代碼不正是我想要的:

result:=DateTimeToUnix(now,true); 
+1

您確定'現在'返回UTC日期時間:P? – Victoria

+0

不,我不是,我認爲這當然不會,我誤解了有關UTC的標誌。所以這個問題仍然是開放的,我如何獲得以firemonkey和OSX爲目標的unix時間戳? – Softtouch

1

添加System.DateUtils到您的使用條款。

然後使用此代碼:

//Get the current local time 
dtLocal:=Now; 
//Convert to UTC 
dtUTC:=TTimeZone.Local.ToUniversalTime(dtLocal); 
//Convert UTC time to Unix format 
dtUTCUnix:=DateTimeToUnix(dtUTC, True); 

注意TTimeZone.Local具有其他屬性,包括UTC偏移量,夏令等。使用適合您的需求。

+0

@softtouch - 上面的答案是否解決了您的問題? – Rohit