Erlang日期时间相关

一、erlang:now()

erlang:now() -> timestamp()

timestamp()={MegaSecs,Secs,MicroSecs}
MegaSecs=Secs=MicroSecs=integer()>=0

BIF erlang:now()返回的是从标准时间(1970年1月1日0时0分0秒至现在的总秒数)

  • MegaSecs:兆秒
  • Secs:秒
  • MicroSecs:微秒
    所以如下转换为标准时间
now_to_seconds({MegaSecs,Secs,_})->
	MegaSecs*1000000+Secs.

二、几个时间函数对比

  • 1、erlang:localtime()
erlang:localtime()-> DateTime

DateTime=calendar:datetime()

eg: erlang:localtime()->{{2021,3,19},{14,26,59}}

  • 2、calendar:local_time()
calendar:local_time()->DateTime
DateTime=calendar:datetime()

eg: calendar:local_time()->{{2021,3,19},{14,27,29}}

  • 3、calendar:universal_time() returns Universal Coordinated Time (UTC)
calendar:universal_time()->DateTime
DateTime=calendar:datetime()

eg: calendar:universal_time()->{{2021,3,19},{6,27,29}}

三、日期转换为时间戳

calendar:datetime_to_gregorian_seconds(DateTime) -> Seconds

DateTime = calendar:datetime()

Seconds = integer() >= 0

时间戳用的是utc时间。综上,方法如下:

dateTime_to_seconds(LocalDateTime)->
	calendar:datetime_to_gregorian_seconds(LocalDateTime)-calendar:datetime_to_gregorian_seconds({{1970,1,1},{8,0,0}}).

如果存储的不是本地时间,则:

dateTime_to_seconds(DateTime)->
	calendar:datetime_to_gregorian_seconds(DateTime)-calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值