在erlang中将时间戳转换为日期时间

时间:2021-10-23 11:27:24

How can I convert a timestamp (number of milliseconds since 1 Jan 1970...) to Date or DateTime format in Erlang? Something like {Year,Month,Day}.

如何将时间戳(1970年1月1日以来的毫秒数...)转换为Erlang中的Date或DateTime格式?像{年,月,日}这样的东西。

2 个解决方案

#1


Roughly:

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.

#2


It just so happens that I have a github gist with a bunch of datetime utilities for exactly this purpose: http://gist.github.com/104903. Calendar has most of the low level plumbing for this stuff.

事实上,我有一个带有一堆日期时间实用程序的github gist用于此目的:http://gist.github.com/104903。日历中包含大部分低级管道。

#1


Roughly:

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.

#2


It just so happens that I have a github gist with a bunch of datetime utilities for exactly this purpose: http://gist.github.com/104903. Calendar has most of the low level plumbing for this stuff.

事实上,我有一个带有一堆日期时间实用程序的github gist用于此目的:http://gist.github.com/104903。日历中包含大部分低级管道。