【问题标题】:Convert timestamp to datetime in erlang在erlang中将时间戳转换为日期时间
【发布时间】:2010-10-23 22:24:38
【问题描述】:

如何在 Erlang 中将时间戳(自 1970 年 1 月 1 日以来的毫秒数...,又称纪元)转换为 Date 或 DateTime 格式?类似{Year,Month,Day}

【问题讨论】:

  • 日历:system_time_to_universal_time(1598512151718, 1000)。 {{2020,8,27},{7,9,11}}

标签: date time erlang


【解决方案1】:

大致:

msToDate(Milliseconds) ->
   BaseDate      = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
   Seconds       = BaseDate + (Milliseconds div 1000),
   { Date,_Time} = calendar:gregorian_seconds_to_datetime(Seconds),
   Date.

【讨论】:

  • 这太糟糕了。为什么不使用日历:datetime_from_timestamp? erlang 是为 footguns 创建的
  • @IliaSidorenko erlang 20.3 中没有这样的方法“calendar:datetime_from_timestamp”
【解决方案2】:

碰巧我有一个 github gist 和一堆 datetime 实用程序正是为了这个目的:http://gist.github.com/104903。日历拥有这些东西的大部分底层管道。

-module(date_util).
-compile(export_all).

epoch() ->
    now_to_seconds(now())
.   

epoch_hires() ->
    now_to_seconds_hires(now())
.   

now_to_seconds({Mega, Sec, _}) ->
    (Mega * 1000000) + Sec
.   

now_to_milliseconds({Mega, Sec, Micro}) ->
    now_to_seconds({Mega, Sec, Micro}) * 1000
.   

now_to_seconds_hires({Mega, Sec, Micro}) ->
    now_to_seconds({Mega, Sec, Micro}) + (Micro / 1000000)
.   

now_to_milliseconds_hires({Mega, Sec, Micro}) ->
    now_to_seconds_hires({Mega, Sec, Micro}) * 1000
.   

epoch_gregorian_seconds() ->
    calendar:datetime_to_gregorian_seconds({{1970,1,1}, {0,0,0}})
.       

now_to_gregorian_seconds() ->
    epoch_to_gregorian_seconds(now())
.       

epoch_to_gregorian_seconds({Mega, Sec, Micro}) ->
    epoch_to_gregorian_seconds(now_to_seconds({Mega, Sec, Micro}));
epoch_to_gregorian_seconds(Now) ->
    EpochSecs = epoch_gregorian_seconds()
    , Now + EpochSecs
.       

gregorian_seconds_to_epoch(Secs) ->
    EpochSecs = epoch_gregorian_seconds()
    , Secs - EpochSecs
.

date_to_epoch(Date) ->
    datetime_to_epoch({Date, {0,0,0} })
.

datetime_to_epoch({Date, Time}) ->
    gregorian_seconds_to_epoch(
        calendar:datetime_to_gregorian_seconds({Date, Time}))
.

is_older_by(T1, T2, {days, N}) ->
    N1 = day_difference(T1, T2)
    , case N1 of
        N2 when (-N < N2) ->
            true;
        _ ->
            false
    end
.

is_sooner_by(T1, T2, {days, N}) ->
    case day_difference(T1, T2) of
        N1 when N > N1 ->
            true;
        _ ->
            false
    end
.

is_time_older_than({Date, Time}, Mark) ->
    is_time_older_than(calendar:datetime_to_gregorian_seconds({Date, Time})
        , Mark);
is_time_older_than(Time, {DateMark, TimeMark}) ->
    is_time_older_than(Time
        , calendar:datetime_to_gregorian_seconds({DateMark, TimeMark}));
is_time_older_than(Time, Mark)  when is_integer(Time), is_integer(Mark) ->
    Time < Mark
.

day_difference({D1, _}, D2) ->
    day_difference(D1, D2);
day_difference(D1, {D2, _}) ->
    day_difference(D1, D2);
day_difference(D1, D2) ->
    Days1 = calendar:date_to_gregorian_days(D1)
    , Days2 = calendar:date_to_gregorian_days(D2)
    , Days1 - Days2
.

is_time_sooner_than({Date, Time}, Mark) ->
    is_time_sooner_than(calendar:datetime_to_gregorian_seconds({Date, Time})
        , Mark);
is_time_sooner_than(Time, {DateMark, TimeMark}) ->
    is_time_sooner_than(Time
        , calendar:datetime_to_gregorian_seconds({DateMark, TimeMark}));
is_time_sooner_than(Time, Mark)  when is_integer(Time), is_integer(Mark) ->
    Time > Mark
.

subtract(Date, {days, N}) ->
    New = calendar:date_to_gregorian_days(Date) - N
    , calendar:gregorian_days_to_date(New)
.

add(Date, {days, N}) ->
    New = calendar:date_to_gregorian_days(Date) + N
    , calendar:gregorian_days_to_date(New)
.

【讨论】:

    【解决方案3】:

    OTP 21.0 增加了这个功能

    calendar:system_time_to_universal_time(Time, TimeUnit) -> datetime()
    
    Types
        Time = integer()
        TimeUnit = erlang:time_unit()
    
    Converts a specified system time into universal date and time.
    

    例子:

    > os:system_time(1000).   
    1598512151718
    > calendar:system_time_to_universal_time(1598512151718, 1000).
    {{2020,8,27},{7,9,11}}
    

    参考:https://erlang.org/doc/man/calendar.html#system_time_to_universal_time-2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 2022-01-08
      • 1970-01-01
      • 2020-10-19
      • 2016-11-26
      相关资源
      最近更新 更多