lua-resty-datetime
线程安全,延迟风格,本地日期时间库
$ opm get pronan/lua-resty-datetime
lua-resty-datetime
线程安全,延迟风格,本地日期时间库
环境
您可以在 Windows 或 Linux,OpenResty 或非 OpenResty 环境下使用此库。
特性
一个日期时间对象 t
可以用可读的表 t.table
或字符串 t.string
或数字 t.number
(时间戳)来描述。这些属性是延迟的,这意味着它们在需要时计算,而不是在初始化时计算。
需求
与 openresty 相同
概要
local datetime = require"lua.resty.datetime".datetime
local timedelta = require"lua.resty.datetime".timedelta
local function strfmt(t)
return string.format('%04d-%02d-%02d %02d:%02d:%02d',
t.year, t.month, t.day,
t.hour or 0, t.min or 0, t.sec or 0)
end
local now_table = os.date('*t')
local now_number = os.time(now_table)
local now_string = strfmt(now_table)
print('current localtime is', now_string, 'correspondent timestamp is', now_number)
-- create a datetime object in three ways:
local dtt = datetime.new(now_table)
local dtn = datetime.new(now_number)
local dts = datetime.new(now_string)
assert(dtt==dtn and dtn==dts, 'these three datetime object should be equal')
local diff_days = 2
local diff_hours = 1
-- seconds = 2*24*3600 + 3600 = 176400
local diff = timedelta.new{day=diff_days, hour=diff_hours}
print(string.format('%s days and %s hour after %s is %s(by table)', diff_days, diff_hours, dtt, dtt+diff))
print(string.format('%s days and %s hour after %s is %s(by number)', diff_days, diff_hours, dtn, dtn+diff))
print(string.format('%s days and %s hour after %s is %s(by string)', diff_days, diff_hours, dts, dts+diff))
print(string.format('%s days and %s hour before %s is %s(by table)', diff_days, diff_hours, dtt, dtt-diff))
print(string.format('%s days and %s hour before %s is %s(by number)', diff_days, diff_hours, dtn, dtn-diff))
print(string.format('%s days and %s hour before %s is %s(by string)', diff_days, diff_hours, dts, dts-diff))
local dto = dtt + timedelta.new{hour=1, sec=1}
print(string.format('%s is later than %s by %s seconds', dto, dtt, (dto - dtt).total_seconds))
local dto = dtt - timedelta.new{hour=1, sec=2}
print(string.format('%s is earlier than %s by %s seconds', dto, dtt, (dtt - dto).total_seconds))
作者
Nan Xiang(@pronan)
许可证
2bsd
依赖
luajit
版本
-
线程安全,延迟风格,本地日期时间库 2016-10-13 03:41:36
-
线程安全,延迟风格,基于 ffi 的本地时间日期时间库 2016-10-07 10:19:38
-
线程安全,延迟风格,基于 ffi 的本地时间日期时间库 2016-10-03 00:13:53