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的解释如下:

erlang:now()

If you do not need the return value to be unique and monotonically increasing, use os:timestamp/0 instead to avoid some overhead.

os:timestamp()

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
时间: 2024-08-05 07:06:20

Erlang 日期和时间处理、时间戳转换的相关文章

(原创)lua日期、时间、时间戳的计算和转换

----------------------------------------------日期与时间 print("当前时间戳:") local nowTime = os.time() print(nowTime) print("") print("转换成日期:") --时间戳 转 日期 local nowData = os.date("%Y%m%d%H%M%S",nowTime) print(nowData) --可以单独

Erlang日期与时间处理

在开发过程中,有两个概念是和地区区域相关的:字符编码和时间;编码和时间的规范演变过程中有文化的冲突有历史的遗留,是软件开发中充满人文气息的一角;关于字符编码我之前整理过一篇文章,[Erlang 0024]Erlang二进制数据处理 这部分知识很有意思,特别是格列佛游记所引出的大端小端概念,妙趣横生;平时笔记中也零零散散记录了一些和时间处理相关的内容,今天按图索骥把相关的资料整理汇集于此. 首先把时间相关的概念解释一下: 有关时间的概念 GMT时间 格林尼治标准时间(Greenwich Mean

python 日期和时间之间的转换整理

1.获取当前时间戳 time.time() 2.获取当前时间 time.localtime() 3.获取格式化的日期时间 time.strftime(format,t) 4.将"%a %b %d %H:%M:%S %Y"(如:"Sat Mar 28 22:24:24 2016")格式化日期时间转换为时间戳 time.mktime(time.strptime(t,format)) 原文地址:https://www.cnblogs.com/lhj818/p/116706

JavaScript 时间与时间戳转换

一.获取当前时间戳 var timestamp = new Date().getTime();console.log(timestamp); 二.yyyy-MM-dd hh:mm:ss 格式的当前时间 function getdate() { var now = new Date(); var Y = now.getFullYear(); var M = now.getMonth() + 1; var D = now.getDate(); var m = M < 10 ? "0"

时间格式时间戳转换

                 源码下载 实现代码: [objc] view plaincopyprint? #pragma mark - timeClick - (void)timeClick:(UIButton *)btn { long testTime = [ _timeTextFiled.text intValue];//定义一个长整形变量 NSNumber *nsTime = [NSNumber numberWithLong:testTime];//将基本数据类型转换为NSNumbe

日期和时间(10)

UNIX时间戳 PHP的日期和时间库 验证日期 格式化日期和时间 将时间戳转换用户友好的值 处理时间戳 日期函数 显示本地化的日期和时间 显示网页的最新修改日期 确定当前月份的天数 确定任意给定月份的天数 计算当前日期后X天的日期 DataTime构造函数简介 格式化日期 实例化后设置日期 实例化后设置时间 修改日期和时间 计算两个日期之差 原文地址:https://www.cnblogs.com/xiukang/p/8569894.html

python——时间与时间戳之间的转换

1.将时间转换成时间戳 将如上的时间2017-09-16 11:28:54转换成时间戳 利用strptime()函数将时间转换成时间数组 利用mktime()函数将时间数组转换成时间戳 #!/usr/bin/env python # -*- coding:utf-8 -*- import time dtime= "2017-09-16 11:28:54" #转换成时间数组 timeArray = time.strptime( dtime, "%Y-%m-%d %H:%M:%S

[转]时间与时间戳之间的转换

文章来源:http://blog.csdn.net/google19890102/article/details/51355282 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换为时间戳 重新格式化时间 时间戳转换为时间 获取当前时间及将其转换成时间戳 1.将时间转换成时间戳 将如上的时间2016-05-05 20:28:54转换成时间戳,具体

(十六)PL/SQL日期及时间

PL/SQL提供两个日期和时间相关的数据类型: 1.日期时间(Datetime)数据类型 DATE TIMESTAMP TIMESTAMP WITH TIME ZONE TIMESTAMP WITH LOCAL TIME ZONE 2.间隔数据类型 INTERVAL YEAR TO MONTH INTERVAL DAY TO SECOND   一.日期时间字段值和间隔数据类型这两个日期时间和间隔数据类型包括字段.这些字段的值确定的数据类型的值.下表列出了时间和间隔的字段及其可能的值. 字段名称