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.pi 3.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
exp e的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
发布了49 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/auspark/article/details/103027234