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 |