Lua学习与交流——Lua os.date 和 os.time 分享
2014-05-26 20:50
489 查看
Lua os.date os.time 分享:
不知道大家在工作中有没有常常用到这两个库函数。比如:界面中需要显示日期相关的文本,或者用到时间相关的判断。那这两个函数会帮上大忙。
os.date():
os.date ([format [, time]])
Returns a string or a table containing date and time, formatted according to the given string format.
If the
timeargument is present, this is the time to be formatted (see the
os.timefunction
for a description of this value). Otherwise,
dateformats the current time.
If
formatstarts with '
!', then the date is formatted in Coordinated Universal Time. After this optional character, if
formatis the string "
*t", then
datereturns a table with the following fields:
year(four
digits),
month(1--12),
day(1--31),
hour(0--23),
min(0--59),
sec(0--61),
wday(weekday, Sunday is 1),
yday(day of the year), and
isdst(daylight saving
flag, a boolean).
If
formatis not "
*t", then
datereturns the date as a string, formatted according to the same rules as the C function
strftime.
When called without arguments,
datereturns a reasonable date and time representation that depends on the host system and on the current locale (that is,
os.date()is equivalent to
os.date("%c")).
看几个输出样例:
local ONE_DAY = 60*60*24 print(os.date()) print(os.date("%m/%d/%y, %H:%M:%S", os.time())) print(os.date("%m/%d/%y, %H:%M:%S", os.time()-ONE_DAY)) -- 结果分别为: 05/26/14 20:30:42 05/26/14, 20:30:42 05/25/14, 20:30:42 |
如果 os.date() 的格式字符是 "*t", 则会返回一个table,其域包含 year,month,day,hour,min,sec,wday,yday,isdst。(见英文部分)
一开始不太明白返回这个 Table 来作何用,今天在使用 os.time() 时,就明白了。
os.time():
os.time (
print(os.time()) print(os.time({day=26, month=5, year=2014})) print(os.date("%m/%d/%y, %H:%M:%S", os.time({day=26, month=5, year=2014}))) -- 输出结果为 1401108284 1401076800 05/26/14, 12:00:00 |
os.time 源码:
static int os_time (lua_State *L) { time_t t; if (lua_isnoneornil(L, 1)) /* called without args? */ t = time(NULL); /* get current time */ else { struct tm ts; luaL_checktype(L, 1, LUA_TTABLE); lua_settop(L, 1); /* make sure table is at the top */ ts.tm_sec = getfield(L, "sec", 0); ts.tm_min = getfield(L, "min", 0); ts.tm_hour = getfield(L, "hour", 12); ts.tm_mday = getfield(L, "day", -1); ts.tm_mon = getfield(L, "month", -1) - 1; ts.tm_year = getfield(L, "year", -1) - 1900; ts.tm_isdst = getboolfield(L, "isdst"); t = mktime(&ts); } if (t == (time_t)(-1)) lua_pushnil(L); else lua_pushnumber(L, (lua_Number)t); return 1; } |
我的做法是:
local tbl = loadstring(os.date("return {day=%d, month=%m, year=%Y, sec=0, min=0, hour=0}"))() os.time(tbl) |
相关文章推荐
- lua时间函数操作和对比代码,os.date() os.time()
- cocos-Lua中的os.time()与os.date()
- Lua学习与交流——Lua string库经验分享
- cocos-Lua中的os.time()与os.date()
- lua os.time与os.date的简单用法
- lua os.time() 和os.date()
- lua时间函数操作和对比代码,os.date() os.time()
- Lua中os库的使用(execute,date,clock,time)
- Lua - 获取系统时间(os.date()/os.time())
- boost之date/time学习
- 跟燕十八学习PHP-第二十三天-mysql日期时间列详解date-time-year-timesta
- marsAndroid学习——常见控件DatePicker、DatePickerDialog和TimePicker、TimePickerDialog
- 【分享】程序员职业规划公开课,进来一起交流学习
- uC/OS-II 学习笔记之:在VC++6.0环境下调试uC/OS-II的源代码分享!
- Lua学习与交流——元表的基础
- Lua使用os.date函数也要小心 推荐
- Lua学习与交流——Lua的栈(转载)
- Lua学习与交流——Lua虚拟机指令(转载)
- lua Date和Time
- ruby -- 基础学习(四)TimeDate处理