> Erlang中文手册 > gregorian_seconds_to_datetime/1 从给定的公历秒数里计算出日期和时间

calendar:gregorian_seconds_to_datetime/1

从给定的公历秒数里计算出日期和时间

用法:

gregorian_seconds_to_datetime(Seconds) -> datetime()

内部实现:

%% gregorian_seconds_to_datetime(Secs)
%%
-spec gregorian_seconds_to_datetime(Seconds) -> datetime() when
      Seconds :: non_neg_integer().
gregorian_seconds_to_datetime(Secs) when Secs >= 0 ->
    Days = Secs div ?SECONDS_PER_DAY,
    Rest = Secs rem ?SECONDS_PER_DAY,
    {gregorian_days_to_date(Days), seconds_to_time(Rest)}.

把给定的公历秒数 Seconds (从 1970-1-1 00:00:00 开始)转换成日期和时间。

StartTime = calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}),
{MegaSecs, Secs, _MicroSecs} = erlang:now(),
Seconds = MegaSecs * 1000000 + Secs,
calendar:gregorian_seconds_to_datetime(Seconds + StartTime).
StartTime = calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}),
calendar:gregorian_seconds_to_datetime(1 + StartTime).
StartTime = calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}}),
calendar:gregorian_seconds_to_datetime(1234567890 + StartTime).