Lua中的时间戳

实现的功能:

  1. 获取Mac OS系统的毫秒数
  2. 获取秒数
  3. 秒数/毫秒数与日期格式的转换

知识点:

  1. 用Lua自带的函数os.time()获取秒数
  2. Lua自带的函数只能获取到秒,要获取到毫秒,需使用lzmq.timer,或者是socket(两个都需要使用luarocks安装)
  3. os.clock返回一个程序使用CPU时间的一个近似值
  4. os.date格式化日期:
local zmq_timer = require("lzmq.timer")
local ms = zmq_timer.absolute_time()
print("ms: "..ms)
--milliseconds: 1573537133261.0
local s = os.time()
print("s: "..s)
--second: 1573537133
local time2date = os.date("%x", os.time())
print("time2date: "..time2date)
--time2date: 11/12/19
local date = os.date("%Y-%m-%d-%H-%M-%S")
print("date: "..date)
--date: 2019-11-12-13-38-53
function time_stamp_from_ms(t_ms)
    local s_from_ms = math.floor(t_ms/1000)
    local s_to_ms = 1000 * math.floor(t_ms/1000)
    local ms_number = t_ms - s_to_ms
    return os.date("%Y-%m-%d %H:%M:%S.", s_from_ms)..string.format("%03d", ms_number)
end
local ms2str = time_stamp_from_ms(ms)
print("ms2timestamp: "..ms2str)
--ms2timestamp: 2019-11-12 13:38:53.261
function time_stamp()
    local ms = zmq_timer.absolute_time() -- use lzmq
    local s = os.time()
    return os.date("%Y-%m-%d %H:%M:%S.", s)..string.format("%03d", (ms-s*1000))
end
local c_ms2str = time_stamp()
print("current_ms_2_timestamp: "..c_ms2str)
--current_ms_2_timestamp: 2019-11-12 13:38:53.262
local socket = require("socket")
local sms = socket.gettime()
print("socket ms: "..sms)
--socket ms: 1573537133.2631
print(os.clock())
--0.005693

os.date()中的时间格式

%a : abbreviated weekday name (e.g., Wed)
%A : full weekday name (e.g., Wednesday)
%b : abbreviated month name (e.g., Sep)
%B : full month name (e.g., September)
%c : date and time (e.g., 09/16/98 23:48:10)
%d : day of the month (16) [01-31]
%H : hour, using a 24-hour clock (23) [00-23]
%I : hour, using a 12-hour clock (11) [01-12]
%M : minute (48) [00-59]
%m : month (09) [01-12]
%p : either "am" or "pm" (pm)
%S : second (10) [00-61]
%w : weekday (3) [0-6 = Sunday-Saturday]
%x : date (e.g., 09/16/98)
%X : time (e.g., 23:48:10)
%Y : full year (1998)
%y : two-digit year (98) [00-99]
%% : the character '%'

Lua math函数库有

函数名描述示例结果
pi圆周率math.pi3.1415926535898
abs取绝对值math.abs(-2012)2012
ceil向上取整math.ceil(9.1)10
floor向下取整math.floor(9.9)9
max取参数最大值math.max(2,4,6,8)8
min取参数最小值math.min(2,4,6,8)2
pow计算x的y次幂math.pow(2,16)65536
sqrt开平方math.sqrt(65536)256
mod取模math.mod(65535,2)1
modf取整数和小数部分math.modf(20.12)20   0.12
randomseed设随机数种子math.randomseed(os.time()) 
random取随机数math.random(5,90)5~90
rad角度转弧度math.rad(180)3.1415926535898
deg弧度转角度math.deg(math.pi)180
expe的x次方math.exp(4)54.598150033144
log计算x的自然对数math.log(54.598150033144)4
log10计算10为底,x的对数math.log10(1000)3
frexp将参数拆成x * (2 ^ y)的形式math.frexp(160)0.625    8
ldexp计算x * (2 ^ y)math.ldexp(0.625,8)160
sin正弦math.sin(math.rad(30))0.5
cos余弦math.cos(math.rad(60))0.5
tan正切math.tan(math.rad(45))1
asin反正弦math.deg(math.asin(0.5))30
acos反余弦math.deg(math.acos(0.5))60
atan反正切math.deg(math.atan(1))45
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

auspark

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值