【问题标题】:sending time in UTC format from ejabberd从 ejabberd 以 UTC 格式发送时间
【发布时间】:2016-01-07 02:28:48
【问题描述】:

我正在从服务器(ejabberd)向客户端发送 xmpp 消息。此消息包含一个字段,其中包含 UTC 格式的日期(基本上是 1970 年以来的秒数)。如何在 erlang 中以 UTC 格式获取日期?任何指针

【问题讨论】:

    标签: erlang ejabberd utc mnesia


    【解决方案1】:

    Usecalendar:universal_time/0

    universal_time() -> datetime()
    
    datetime() = {date(), time()}
    date() = {year(), month(), day()}
    time() = {hour(), minute(), second()}
    

    这样

    {Date, _Time} = DateTime = calendar:universal_time()
    

    您可以参考this 问题将datetime() 转换为unix 时间戳。您只需将时间戳转换为自 1970/01/01 以来的秒数:

    Seconds = calendar:datetime_to_gregorian_seconds(DateTime) - 62167219200,
    %% 62167219200 == calendar:datetime_to_gregorian_seconds({{1970, 1, 1}, {0, 0, 0}})
    

    【讨论】:

    • @Geek 然后首先在您的问题中说明您要求从 1970 年开始以秒为单位的日期。
    • 我已经修改了问题:)。
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2020-03-09
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2019-07-27
    相关资源
    最近更新 更多