1、首先,说说时间相关的基础函数,类似于其它语言中的获取时间戳函数。
查下API文档,描述如下:
erlang:now()
timestamp()
MegaSecs
可以看到,这个就是erl中的获取时间戳的方法,知识她精确到了微妙。erl系统确保这个api的返回值不会重复,这个特性很厉害,据说pid创建就与这个为基础。
结果以tuple形式形式返回。Secs为秒的简写,Mega宏达的意思,Micro极小的意思,MegaSecs兆秒,MicroSecs微妙。
为了和其它语言方便通信,有时候需要转换成标准时间戳(从UTC时1970年1月1日0时0分0秒起至现在的总秒数)。Now转换成标准时间戳的方法。
now_to_s({MegaSecs,Secs,_})->
MegaSecs*1000000+Secs.
date()
Date
Returns
time()
Hour
Returns
标准函数中的date和time分别返回当地的日期时间,对应于calendar:local_time()。
calendar:local_time()的返回值为{date(),time()}。标准函数中有date和time了,calendar
中为什么还要设计local_time呢。文档上描述是为了避免先调用time(),再调用date(),正好在隔日的分界点,就会出现问题了。
3、erlang日期时间转换为时间戳的方法。
calendar:datetime_to_gregorian_seconds(DateTime)
DateTime
Seconds
这个返回的是格林尼治标准时间秒数。
格林尼治标准时间(GMT),这个秒数是当前到0,,0,0
时间戳用的是utc时间。综上,方法如下:
calendar:datetime_to_gregorian_seconds(LocalDateTime)-calendar:datetime_to_gregorian_seconds({{1970,1,1},{8,0,0}}).
如果存储的不是本地时间,则:
calendar:datetime_to_gregorian_seconds(DateTime)-calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}).
552

被折叠的 条评论
为什么被折叠?



