作業系統函式庫教程

lua-users home
wiki

你可以在參考手冊的第 5.8 章中找到關於操作系統函式庫的詳細資訊 [1]

os.clock()

傳回自 Lua 啟動以來,CPU 已執行的秒數。

> = os.clock()
11056.989

os.date([格式 [, 時間]])

傳回已格式化的日期字串,或時間資訊表格。格式字串與 C 的 strftime() 函式具有相同的格式。

待辦事項:插入資訊、範例和連結至 strftime()

http://www.cplusplus.com/reference/clibrary/ctime/strftime/ 列出所有參數

簡單範例

> = os.date("%d.%m.%Y")
06.10.2012

如果格式字串為 "*t",會傳回包含時間資訊的表格,例如:

> table.foreach(os.date('*t'), print)
hour    14
min     36
wday    1
year    2003
yday    124
month   5
sec     33
day     4
isdst   true

使用 pairs() 方法的上述範例

> for k, v in pairs(os.date("*t")) do print(k, v) end
year    2012
day     1
min     54
wday    4
month   8
isdst   true
yday    214
sec     39
hour    14

如果格式在前面加上「!」符號,時間會轉換為協調世界時,例如:

> table.foreach(os.date('!*t'), print)
hour    21
min     36
wday    1
year    2003
yday    124
month   5
sec     42
day     4
isdst   false

使用 pairs() 方法的上述範例

> for k, v in pairs(os.date("!*t")) do print(k, v) end
year    2012
day     1
min     58
wday    4
month   8
isdst   false
yday    214
sec     39
hour    12

os.date() 會以 MM/DD/YY HH:MM:SS 格式傳回字串。

> print(os.date())
08/16/05 10:22:32

如今,os.date() 會以不同的格式傳回字串。

> = os.date()
Wed Aug  1 15:00:47 2012

os.difftime(t2, t1)

計算從時間 t1 到時間 t2 之間的秒數。

> t1 = os.time()
> -- wait a little while then type....
> = os.difftime(os.time(), t1)
31
> = os.difftime(os.time(), t1)
38

os.execute([命令])

執行操作系統殼層命令。就像 C 的 system() 函式。會傳回系統依賴的狀態碼。

> = os.execute("echo hello")
hello
0
> = os.execute("mmmmm")  -- generate an error
'mmmmm' is not recognized as an internal or external command,
operable program or batch file.
1

如果沒有參數,這個命令會在存在 OS shell 時傳回非零值,在不存在 OS shell 時傳回零值。

> = os.execute()   -- no argument
1

os.exit([代碼])

呼叫 C 函式 exit,使用選用的代碼終止主機程式。代碼的預設值是成功代碼。

> os.exit(0)   -- kill the Lua shell we are in and pass 0 back to parent shell

os.getenv(變數名稱)

傳回程序環境變數 varname 的值,如果變數未定義,則傳回 nil。

> = os.getenv("BANANA")
nil
> = os.getenv("USERNAME")
Nick

os.remove(檔名)

刪除具有特定檔名的檔案。如果此函式失敗,將傳回 nil,以及說明錯誤的字串。

> os.execute("echo hello > banana.txt")
> = os.remove("banana.txt")
true
> = os.remove("banana.txt")
nil     banana.txt: No such file or directory   2

os.rename(舊檔名, 新檔名)

將名稱為舊檔名的檔案重新命名為新檔名。如果此函式失敗,將傳回 nil,以及說明錯誤的字串。

> os.execute("echo hello > banana.txt")
> = os.rename("banana.txt", "apple.txt")
true
> = os.rename("banana.txt", "apple.txt")
nil     banana.txt: No such file or directory   2

os.setlocale(地區設定 [, 類別])

設定程式目前的語言環境。地區設定是一個指定語言環境的字串;類別是一個描述要變更哪個類別的選用字串:「all」、「collate」、「ctype」、「monetary」、「numeric」或「time」;預設類別是「all」。函式會傳回新語言環境的名稱,或在要求無法被接受時傳回 nil。

os.time([表格])

提供由 os.date() 使用的格式化日期表格,傳回系統秒數中的時間。

> t = os.date('*t')  -- time now
> table.foreach(os.date('*t'), print)
hour    15
min     1
wday    1
year    2003
yday    124
month   5
sec     2
day     4
isdst   true
> = os.time(t)       -- time in system seconds
1052085659
> t.year = 2001      -- 2001, a Lua odyssey
> = os.time(t)       -- time then
989013659

os.tmpname ()

產生可用於暫存檔的名稱。這只會產生名稱,不會開啟檔案。

> = os.tmpname()  -- on windows
\s2js.
> = os.tmpname()  -- on debian
/tmp/lua_5xPi18

最新變更 · 喜好設定
編輯 · 歷史紀錄
最後編輯於 2022 年 6 月 26 日,星期日 上午 6:57 (GMT) (diff)