如何在Erlang/OTP中将datetime()转换为timestamp()?

如何在Erlang/OTP中将datetime()转换为timestamp()?,erlang,Erlang,我需要将{{2012,9,21},{13,21,11}}转换为timestamp() 我如何才能做到这一点?更正版本: Seconds = calendar:datetime_to_gregorian_seconds(DateTime) - 62167219200, %% 62167219200 == calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}) {Seconds div 1000000, Seconds re

我需要将
{{2012,9,21},{13,21,11}}
转换为
timestamp()

我如何才能做到这一点?

更正版本:

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}.
你可以用这个

to_timestamp({{Year,Month,Day},{Hours,Minutes,Seconds}}) ->
(calendar:datetime_to_gregorian_seconds(
    {{Year,Month,Day},{Hours,Minutes,Seconds}}
) - 62167219200)*1000000;
这是本课程模块的一部分

Related:没关系,但要注意,如果您使用函数erlang:now()获取时间戳以进行比较或任何计算,if将从1970年1月1日开始计算时间,而calendar:datetime_to_gregorian_seconds/1则从1/1/0开始计算。所以有719528天的差异…哦,你是对的。由于
timestamp()
被定义为从1970年开始,如果可能的话,我的答案是不正确的:)我尝试了上面的函数:timestamp=datetime\u to\u now({2012,9,21},{13,21,00}),然后calendar:now_to\u local\u time(timestamp)返回{{2012,9,21},{17,21,0}。我的时区是+4小时。据我所知,该函数返回UTC时间戳。是吗?@KirillTrofimov是的,就像
now/0
返回的时间戳一样。如果需要本地时间,请先使用日历:本地时间转换为通用时间dst/1。