lua os 库提供了简单的跟操作系统有关的功能
1.os.clock()
返回程序所运行使用的时间
- local nowTime = os.clock()
- print("now time is",nowTime)
- local s = 0
- for i = 1,100000000 do
- s =s+i
- end
- spendTime = os.clock() - nowTime
- print(string.format("Spend time is : %.2f\n", spendTime))
- -- now time is 0.002841
- -- Spend time is : 0.66
- 2.os.date ([format [, time]])
用来格式化时间戳为可读时间, time 为可选时间戳, 省略时取当下
- local now = os.date()
- print("current time is",now)
- --current time is Mon Nov 4 09:00:49 2019
- local now = os.date("%Y-%m-%d %H:%M:%S")
- print("now time is",now)
- -- now time is 2019-11-04 09:00:49
- -- %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 一个月里的几号 [01-31]
-- %H 24 小时 [00-23]
-- %I 12 小时 [01-12]
-- %j 一年中的第几天 [001-366]
-- %M 分 [00-59]
-- %m 月份 [01-12]
-- %p am(上午),pm(下午)
-- %S 秒 [00-60]
-- %w 星期几 [0-6 = 星期天 - 星期六]
- -- %x date (e.g., 09/16/98)
- -- %X time (e.g., 23:48:10)
- -- %y two-digit year (98) [00-99]
-- %Y 年 (1998)
- -- %% the character '%'
- local now = os.date("%Y-%m-%d %H:%M:%S")
- print("now time is",now)
- --now time is 2019-11-04 09:13:47
- local year = string.sub(now,1,4)
- local month = string.sub(now,6,7)
- local day = string.sub(now,9,10)
- local hour = string.sub(now,12,13)
- local minute = string.sub(now,15,16)
- local second = string.sub(now,18,19)
- print("now year is",year)
- print("now month is",month)
- print("now day is",day)
- print("now hour is",hour)
- print("now minute is",minute)
- print("now second is",second)
- --now year is 2019
- --now month is 11
- --now day is 04
- --now hour is 09
- --now minute is 13
- --now second is 47
其中 "*t":
将返一个带 year(4 位),month(1-12), day (1--31), hour (0-23), min (0-59), sec (0-61), wday (星期几, 星期天为 1), yday (年内天数), and isdst (是否为日光节约时间 true/false) 的带键名的表
- local now = os.date("*t")
- for k ,v in pairs(now) do
- print (k ,v )
- end
-- 输出如下
- wday 2
- sec 3
- hour 9
- month 11
- year 2019
- yday 308
- min 6
- day 4
- isdst false
- 3.os.time ([table])
默认获取当前时间戳 (1970 到现在的秒数), 也可以指定 table 参数
- local now = os.date("%Y-%m-%d %H:%M:%S")
- print("now time is",now)
- --now time is 2019-11-04 09:27:13
- local year = string.sub(now,1,4)
- local month = string.sub(now,6,7)
- local day = string.sub(now,9,10)
- local hour = string.sub(now,12,13)
- local minute = string.sub(now,15,16)
- local second = string.sub(now,18,19)
- print("now year is",year)
- print("now month is",month)
- print("now day is",day)
- print("now hour is",hour)
- print("now minute is",minute)
- print("now second is",second)
- --now year is 2019
- --now month is 11
- --now day is 04
- --now hour is 09
- --now minute is 27
- --now second is 13
-- 获取 7 天前的时间
- ta = {
- year=year,
- month=month,
- day=day-7,
- hour=hour,
- min=minute,
- sec=second
- }
- --2018-05-02 09:50:57
- local t = os.date("%Y-%m-%d %H:%M:%S", os.time(ta))
- print ("the day before 7days time",t)
- --the day before 7days time 2019-10-28 09:27:13
-- 获取本年 2 月份的天数
--3 月份开头减去 1 天就是 2 月份的最后一天
- ta = {
- year=year,
- month=3,
- day=0,
- }
- --28
- local days = os.date("%d", os.time(ta))
- print ("二月份的天数",days)
-- 二月份的天数 28
4.os.difftime (t2, t1)**
返回 t1 到 t2 相差的秒数
- t1 = os.time();
- print("t1 time is",t1)
- --t1 time is 1572830412
- for i = 0, 10000000 do
- os.time();
- end
- t2 = os.time();
- print(t2 - t1);
- --1
- print(os.difftime(t2, t1));
- --1.0
- 5.os.execute ([command])
相当于 system 函数, 返回执行结果, 可以执行 shell 语句
- os.execute("lua -v")
- --Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
- 6.os.exit
相当于 C 的 exit 函数, 终止主程序, code 为返回值
- ```lua
- function func_testexit()
- print("lua --> func_testexit start");
- io.read();
os.exit(0); --==>os.exit() 用法
- print("lua --> func_testexit end");
- io.read();
- end
- ```
- ```objective-c
- lua_State *L = lua_open();
- luaL_openlibs(L);
- printf("c++ --> before call func_testexit\n");
- luaL_dofile(L,"exittest.lua"); // 加载执行 lua 文件
- lua_getglobal(L, "func_testexit"); // 函数入栈
- lua_pcall(L, 0, 0, 0); // 打印信息
- printf("c++ --> after call func_testexit\n");
- lua_close(L); // 关闭 lua 环境
- ```
- ```lua
-- 输出结果为:
- -- c++ --> before call func_testexit
- -- lua --> func_testexit start
- ```
总结:
- 需要注意的是,`os.exit()` 不仅仅会退出 lua 运行环境, 连宿主程序也会退出, 代码中的 `io.read();` 仅仅为了可以看到打印信息, 否则宿主程序直接退出就什么也看不到了.
- 我们可以发现代码 `print("lua --> func_testexit end");` 和 `printf("c++ --> after call func_testexit\n");` 是无论如何也执行不到的.
- 利用 `os.exit()` 可以退出宿主程序, 我们可以在 lua 解释器中使用这个函数已退出解释器
其中这个函数的实现源码, 其实就是调用了 c 语言的 exit():
- static int os_exit (lua_State *L) {
- exit(luaL_optint(L, 1, EXIT_SUCCESS));
- }
- // 其中 c 语言中有如下定义:
- /* Definition of the argument values for the exit() function */
- #define EXIT_SUCCESS 0
- #define EXIT_FAILURE 1
- 7.os.getenv (varname)
功能: 返回当前进程的环境变量 varname 的值, 若变量没有定义时返回 nil
- print(os.getenv("HOME"))
- print(os.getenv("TEMP"))
- --/Users/jason
- --nil
- 8.os.remove (filename)
删除文件或目录, 若函数调用失败则返回 nil 和错误信息
9.os.rename (oldname, newname)
重命名文件或目录, 失败返回 nil 以及错误信息
10.os.tmpname()
返回具有可用于临时文件的文件名的字符串. 该文件必须在使用前打开, 并且在不需要时删除.
io.tmpfile() 产生的临时文件程序结束后会自动删除
11.os.setlocale (locale [, category])
解释: 设置程序的当前区域, 函数返回设置以后该项最新的值, 如果失败则返回 `nil`.
主要用于设定不同区域, 不同语言习惯
参数: 两个参数均可省略, 但具体含义不同.
locale: 表示一个指定当前设置区域的字串, 有几种特殊形式如下
- "": 一个空字串, 当前设置被视为本地设置
- "C": 当前设置被视为标准 C 设置
- nil: 返回 category 指示设置名的当前值
category: 一个描述要更改的设置名, 实际上就是制定一个分类的名字, 分类如下
- all: 默认选项, 包含下述所用分类.
- collate : 影响 C 语言函数 strcoll 和 strxfrm
- ctype: 影响字符处理函数和多行字符处理函数
- monetary: 影响 C 语言函数 localeconv 返回的货币格式化信息
- numeric: 影响格式化输入输出字符中的小数点符号
- time: 影响 C 语言函数 strftime
常用语言区域:
| 语言缩写 | 语言种类 | 语言代码 |
| -------- | ------------ | -------- |
| chs | 简体中文 | 0804 |
| cht | 繁体中文 | 0404 |
| jpn | 日文 | 0011 |
| kor | 韩文 | 0012 |
| dan | 丹麦文 | 0006 |
| deu | 德文 | 0007 |
| eng | 国际英文 | 0809 |
| enu | 英文 | 0409 |
| esp | 西班牙文 | 000A |
| fin | 芬兰文 | 000B |
| fra | 法文 (标准) | 040C |
| frc | 加拿大法文 | 0C0C |
| ita | 意大利文 | 0010 |
| nld | 荷兰文 | 0013 |
| nor | 挪威文 | 0014 |
| plk | 波兰文 | 0015 |
| ptb | 巴西葡萄牙文 | 0416 |
| ptg | 葡萄牙文 | 0816 |
| rus | 俄文 | 0019 |
| sve | 瑞典文 | 001D |
| tha | 泰文 | 001E |
来源: http://www.bubuko.com/infodetail-3345737.html