2012-09-21 45 views

回答

15

修正版本:

Seconds = calendar:datetime_to_gregorian_seconds(DateTime) - 62167219200, 
%% 62167219200 == calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}) 
{Seconds div 1000000, Seconds rem 1000000, 0}. 
+3

沒問題,但是要注意,如果你正在使用函數erlang:now()來獲取比較的時間戳或者y計算,如果從1970年1月1日開始計算時間,而日曆:datetime_to_gregorian_seconds/1計算1/1/0。所以有719528天的差異... – Pascal

+0

糟糕,你是對的。因爲'timestamp()'被定義爲從1970年開始,如果可能的話,我的答案是_not_ ok :) –

+1

我已經嘗試了上面的函數:Timestamp = datetime_to_now({{2012,9,21},{13,21,00 }}),則日曆:now_to_local_time(Timestamp)返回{{2012,9,21},{17,21,0}}。我的時區是+ 4小時。據我所知,該函數返回UTC中的時間戳。對? –

1

您可以使用此

to_timestamp({{Year,Month,Day},{Hours,Minutes,Seconds}}) -> 
(calendar:datetime_to_gregorian_seconds(
    {{Year,Month,Day},{Hours,Minutes,Seconds}} 
) - 62167219200)*1000000; 

這是從這個 Github/Arboreus

相關問題