Search code examples
erlangejabberdutcmnesia

sending time in UTC format from ejabberd


I am sending a xmpp message to client from server(ejabberd).This message contains a field which contains date(Basically seconds since 1970) in UTC format.How to get a date in UTC format in erlang?Any pointers


Solution

  • Use calendar:universal_time/0

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

    Like this

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

    You can refer to this question to convert datetime() to unix timestamp. You'll just need to convert timestamp to seconds since 1970/01/01:

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