Erlang 日期和时间处理、时间戳转换
http://www.csdn 123.com/html/blogs/20131113/95993.htm
获取当前时间
erlang:now()得到的是从1970年1月1日零时起,到现在经过的时间,结果为{MegaSecs, Secs, MicroSecs}。有个问题要注意,还有另外一个函数可以实现同样的功能:os:timestamp()
那么erlang:now()和os:timestamp()的区别是什么?
erlang的解释如下:
If you do not need the return value to be unique and monotonically increasing, use os:timestamp/0 instead to avoid some overhead.
The difference is that this function returns what the operating system thinks (a.k.a. the wall clock time) without any attempts at time correction. The result of two different calls to this function is not guaranteed to be different.
大致的意思是:erlang:timestamp()获取到的时间更接近于操作系统时间,erlang:now()每次获取都会生成唯一的时间,看来erlang:now()在实现上对时间多做了一个校正,相对erlang:timestamp()有点失真,同时也会有多余的开销。但是erlang:now()每次都生成唯一的值,有时候对我们来说也是一大利好的
获取当前时间戳
timestamp() ->
{M, S, _} = os:timestamp(),
M * 1000000 + S.
获取当前时间
% 本地时间
local_time() ->
calendar:local_time(). % 世界时间
world_time() ->
calendar:universal_time().
获取年月日,时分秒
1> {{Year, Month, Day}, {Hour, Minite, Second}} = calendar:local_time().
{{2013,11,13},{20,10,10}}
2> Day.
13
3> Second.
10
时间转换
% 秒转时间
11> calendar:seconds_to_daystime(86000).
{0,{23,53,20}}
12> calendar:seconds_to_daystime(86400).
{1,{0,0,0}}
13> calendar:seconds_to_daystime(1086400).
{12,{13,46,40}} % 时间转秒
14> calendar:time_to_seconds({23,53,20}).
86000
时间与时间戳的转换
% 时间转时间戳,格式:{{2013,11,13}, {18,0,0}}
datetime_to_timestamp(DateTime) ->
calendar:datetime_to_gregorian_seconds(DateTime) -
calendar:datetime_to_gregorian_seconds({{1970,1,1}, {0,0,0}}).
% 时间戳转时间
timestamp_to_datetime(Timestamp) ->
calendar:gregorian_seconds_to_datetime(Timestamp +
calendar:datetime_to_gregorian_seconds({{1970,1,1}, {0,0,0}})).
时间格式化
-module(print_time).
-export([format_utc_timestamp/0]).
format_utc_timestamp() ->
TS = {_,_,Micro} = os:timestamp(),
{{Year,Month,Day},{Hour,Minute,Second}} =
calendar:now_to_universal_time(TS),
Mstr = element(Month,{"Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec"}),
io_lib:format("~2w ~s ~4w ~2w:~2..0w:~2..0w.~6..0w",
[Day,Mstr,Year,Hour,Minute,Second,Micro]).
编译这个模式,运行如下:
1> io:format("~s~n",[print_time:format_utc_timestamp()]).
29 Apr 2009 9:55:30.051711
Erlang 日期和时间处理、时间戳转换的更多相关文章
- (原创)lua日期、时间、时间戳的计算和转换
----------------------------------------------日期与时间 print("当前时间戳:") local nowTime = os.tim ...
- python3 进行字符串、日期、时间、时间戳相关转换
1.字符串转换成时间戳 2. 日期转换成时间戳
- JavaScript 时间与时间戳转换
一.获取yyyy-MM-dd hh:mm:ss 格式的时间 function getdate(timeStamp) { if (timeStamp) { var now = new Date(time ...
- Js获取当前的日期和时间以及时间戳转化为时间
/** *获取当前时间 *format=1精确到天 *format=2精确到分 */ function getCurrentDate(format) { var now = new Date(); v ...
- 【python-时间戳】时间与时间戳之间的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- python 时间和时间戳的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- SQL记录-PLSQL日期与时间
PL/SQL日期及时间 PL/SQL提供两个日期和时间相关的数据类型: 日期时间(Datetime)数据类型 间隔数据类型 datetime数据类型有: DATE TIMESTAMP TIMESTAM ...
随机推荐
- C#做完一个网站怎么发布?
前段时间在局域网上发布了一个自己做的网站,发布过程中遇到了不少问题.下面就发布过程和发布过程中遇到的问题与(你)大家一起分享一下,希望对(你)大家有所帮助吧! 在将ASP.NET网站发布到服务器之前需 ...
- OpenAL音频播放
// // OpenALPlayer.m // live // // Created by lujunjie on 2016/11/5. // Copyright © 2016年 lujunjie. ...
- Django的命令
安装django : pip install django 创建django项目 :django-admin startproject projectname 启动django项 ...
- c++中的相对路径
今天在vs2010里读取相对路径下的图片文件出了点问题.于是查了一下相对路径的编程知识,记录下来分享给大家: 问题描写叙述:path=".\\TrainData\\& ...
- 如何使用 PyCharm 将代码上传到GitHub上(详细图解)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 一丶说明 测试条件:需要有GitHub账号以及在本地安装了Git工具,无论是Linux环境还是Windows都是一样的 如果还没有 ...
- .NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一)
原文:.NetCore微服务Surging新手傻瓜式 入门教程 学习日志---先让程序跑起来(一) 写下此文章只为了记录Surging微服务学习过程,并且分享给广大想学习surging的基友,方便广大 ...
- 零基础学python-2.3 凝视
在python里面,使用"#"号表示凝视的開始,一整行到结束就是凝视,他的主要作用提示这段代码到底有什么用处 print("---------欢迎来到猜数字的地方.请開始 ...
- vue 用 :style动态修改带中划线的样式属性
今天在项目中遇到要用:style动态设置margin-top值,直接写发现报错.后来改成驼峰就成功了,记录一下 错误示范: <div class="testLeft ulData&qu ...
- 9.10 Binder系统_Java实现_hello服务
怎么做?2.1 定义接口: 写IHelloService.aidl文件, 上传, 编译, 得到IHelloService.java 里面有Stub : onTransact, 它会分辨收到数据然后调用 ...
- Spring 定时器 No qualifying bean of type [org.springframework.scheduling.TaskScheduler] is defined(转)
最近项目里面,用了spring的定时任务,一直以来,项目运行的不错.定时器也能正常使用.可是,今天启动项目测试的时候,盯着启动Log看了一阵子,突然间发现,启动的Log中居然有一个异常,虽然一闪而过, ...