Lua中os库的使用(execute,date,clock,time)

--更多详细参考 http://luaer.diandian.com/post/2011-10-09/5660047 --
--更多详细参考 http://blog.csdn.net/goodai007/article/details/8077285--
local function main()
	print(os.clock()) --
	print(os.time())
	dateTable = os.date("*t") --返回一张时间table表{year,month,day,hour,min,sec}
	print(dateTable.year.."~"..dateTable.month.."~"..dateTable.day.."~"..
		  dateTable.hour.."~"..dateTable.min.."~"..dateTable.sec)
	print("rename and remove~~")
	os.rename("./src/Demo.txt","./Demo.bbb") --os.rename()操作可以移动文件的位置只需要在rename时,重新配置路径就可以了
	os.remove("./Demo.bbb")  --删除文件
	--执行命令
	os.execute("mkdir vokie123") --创建文件夹
	os.execute("ls")
	os.execute("dir")
	os.execute("pause")
	os.exit() --停止Lua的主线程
end

main()

输出结果:

0.046
1385609104
2013~11~28~11~25~4
rename and remove~~
src
vokie123
 驱动器 D 中的卷是 SOFTWARE
 卷的序列号是 8236-DBD6


 D:luaDevTest2 的目录


2013-11-28  10:26    <DIR>          .
2013-11-28  10:26    <DIR>          ..
2013-11-28  10:26               382 .project
2013-11-28  10:26    <DIR>          src
2013-11-28  10:26               213 .buildpath
2013-11-28  11:25    <DIR>          vokie123
               2 个文件            595 字节
               4 个目录 67,707,273,216 可用字节
请按任意键继续. . . 


date格式format小记:

例如:
t = os.date("*t", os.time());
for i, v in pairs(t) do
      print(i, v);
end

输出:
hour  14
min   58
wday  2
day   10
month  8
year  2009
sec   18
yday  222
isdst  false

对于其它的格式字符串,os.date会将日期格式化为一个字符串

例如:
print(os.date("today is %A, in %B"))      -->today is Tuesday, in May
print(os.date("%x", 906000490))           -->09/16/1998

所有格式化字符串如下:
  
%a      一星期中天数的简写                      (Wed)
%A      一星期中天数的全称                      (Wednesday)
%b      月份的简写                                  (Sep)
%B      月份的全称                                  (September)
%c      日期和时间                                   (09/16/98 23:48:10)
%d      一个月中的第几天                          (16)[0 ~ 31]
%H      24小时制中的小时数                      (23)[00 ~ 23]
%I      12小时制中的小时数                       (11)[01 ~ 12]
%j      一年中的第几天                             (259)[01 ~ 366]
%M      分钟数                                       (48)[00 ~ 59]
%m      月份数                                       (09)[01 ~ 12]     
%P      "上午(am)" 或 "下午(pm)"               (pm)
%S      秒数                                          (10)[00 ~ 59]
%w      一星期中的第几天                         (3)[0 ~ 6 = 星期天 ~ 星期六]

 %W 一年中的第几个星期 0 ~ 52
%x      日期                                          (09/16/98)
%X      时间                                          (23:48:10)
%y      两位数的年份                               (90)[00 ~ 99]
%Y      完整的年份                                 (2009)
%%      字符串'%'

时间戳转日期,日期转时间戳的几个小函数:

 1 function daysDifference(min_timestamp, max_timestamp)
 2     local temp = nil
 3     if max_timestamp < min_timestamp then
 4         temp = min_timestamp
 5         min_timestamp = max_timestamp
 6         max_timestamp = temp
 7     end
 8 
 9     local min_days = checkint(min_timestamp / 86400)
10     local max_days = checkint(max_timestamp / 86400)
11     return (max_days - min_days)
12 end
13 
14 function timeStampConvertToDate(timestamp)
15     date_string = string.format(os.date("%x",timestamp))
16     local date_table = {}
17     local t = string.split(date_string, "/")
18     date_table.YEAR = checkint(t[3])
19     date_table.MONTH = checkint(t[1])
20     date_table.DAY = checkint(t[2])
21     print("YEAR,MONTH,DAY = "..date_table.YEAR..","..date_table.MONTH..","..date_table.DAY)
22     return date_table
23 end
24 
25 
26 --date_t 格式要求:
27 --local date_t = {year=2005, month=11, day=6, hour=22,min=18,sec=30,isdst=false}
28 --isdst表示是否夏令时 
29 function DateConvertToTimeStamp(date_t)
30     return os.time(date_t)
31 end





原文地址:https://www.cnblogs.com/vokie/p/3602084.html