Lua学习与交流——Lua os.date 和 os.time 分享

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 time argument is present, this is the time to be formatted (see the os.time function for a description of this value). Otherwise, date formats the current time.

If format starts with '!', then the date is formatted in Coordinated Universal Time. After this optional character, if format is the string "*t", then date returns 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 format is not "*t", then date returns the date as a string, formatted according to the same rules as the C function strftime.

When called without arguments, date returns 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
     Format格式注解:

     如果 os.date() 的格式字符是 "*t", 则会返回一个table,其域包含 year,month,day,hour,min,sec,wday,yday,isdst。(见英文部分)
     一开始不太明白返回这个 Table 来作何用,今天在使用 os.time() 时,就明白了。

os.time():

os.time ([table])

Returns the current time when called without arguments, or a time representing the date and time specified by the given table. This table must have fields yearmonth, and day, and may have fields hourminsec, and isdst (for a description of these fields, see the os.date function).

The returned value is a number, whose meaning depends on your system. In POSIX, Windows, and some other systems, this number counts the number of seconds since some given start time (the "epoch"). In other systems, the meaning is not specified, and the number returned by time can be used only as an argument to date and difftime

    如果没有任何参数,就会返回当前时间;
    如果参数一个table,并且table的域必须有 year, month, day, 可有也可以没有 hour, min, sec, isdst,则会返回table所代表日期的时间。
    返回值是一个 number ,其值取决于你的系统。返回值通常被用于 os.date 和 os.difftime。

    看几个输出样例:
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() 的参数可以是一个 table,如果不指定 sec,hour,min这三个域,其默认值会是当天中午 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;
}

    其实,楼主工作时,是想要返回今天凌晨,也就是00:00:00时的时间值。
    我的做法是:
local tbl = loadstring(os.date("return {day=%d, month=%m, year=%Y, sec=0, min=0, hour=0}"))()
os.time(tbl)
    注意的就是一定要指定 sec,min,hour 这三个值,否则默认是12:00:00

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值