SoFunction
Updated on 2025-04-09

Detailed introduction to the Lua os library

This library is implemented through table os

()

Function: Return an approximate value of CPU time used by a program

For example:

Copy the codeThe code is as follows:

local x = ();
print(())
local s = 0;
for i = 1, 100000000 do
      s = s + i;
end
print(("elapsed time : %.2f\n", () - x));

Output:
Copy the codeThe code is as follows:

0
elapsed time : 2.55

([format [, time]])

Function: Return a string or table that formats the date, time or formats
If the time parameter is set, format it according to the time specified by the time, otherwise format it according to the current time.
parameter:
  format:
"!": Format according to Greenwich time.
"*t": will return a table with key names with year(4 bits), month(1-12), day (1--31), hour (0-23), min (0-59), sec (0-61), wday (day, Sunday is 1), yday (days in the year), and isdst (whether it is sun saving time true/false); if there is no "*t", it will return a string formatted by the strftime function pressing C
If there are no parameters, return the formatted string according to the current system settings () <=> ("%c")
 
For example:

Copy the codeThe code is as follows:

t = ("*t", ());
for i, v in pairs(t) do
      print(i, v);
end

Output:
Copy the codeThe code is as follows:

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

For other format strings, the date is formatted as a string

For example:

Copy the codeThe code is as follows:

print(("today is %A, in %B"))      -->today is Tuesday, in May
print(("%x", 906000490))           -->09/16/1998

All formatted strings are as follows:
Copy the codeThe code is as follows:
 
%a      Abbreviation of the number of days in a week                                                                                                                  �
%A     The full name of the number of days in a week                                                    �
%b
%B
%c
%d     What day of the month                                                       �
%H
%I
%j     What day of the year                                                                                                                       �
%M
%m
%P     "Am (am)" or "Am (pm)"                                                  �
%S      Number of seconds                                          (10)[00 ~ 59]
%w     What day of the week                                                       �

%W What week of the year 0 ~ 52
%x
%X
%y
%Y
%%      String '%'

(t2, t1)

Function: Return the number of seconds between t1 and t2

For example:

Copy the codeThe code is as follows:

t1 = ();
for i = 0, 10000000 do
 ();
end
t2 = ();
print((t2, t1));

Output:

2

([command])

Function: equivalent to C system function, return the system status code

For example:

Copy the codeThe code is as follows:

("pause")

Output:

Press any key to continue... .

(

Copy the codeThe code is as follows:
)

Function: Exit function equivalent to C, terminates the main program, code is the return value

For example:
[code]
(1)


(varname)-

Function: Return the value of the varname environment variable of the current process. If the variable is not defined, return nil

For example:

Copy the codeThe code is as follows:

print(("USERDOMAIN"))
print(("SystemRoot"))
print(("Os2LibPath"))
print(("ProgramFiles" ))
print(("APPDATA" ))
print(("ALLUSERSPROFILE" ))
print(("CommonProgramFiles" ))
print(("COMPUTERNAME" ))
print(("USERNAME"))
print(("USERPROFILE" ))
print(("ComSpec"))
print(("LOGONSERVER" ))
print(("NUMBER_OF_PROCESSORS" ))
print(("OS"))
print(("PATHEXT" ))
print(("PROCESSOR_ARCHITECTURE" ))
print(("PROCESSOR_IDENTIFIER" ))
print(("PROCESSOR_LEVEL" ))
print(("PROCESSOR_REVISION" ))
print(("USERDOMAIN"))
print(("SystemRoot" ))
print(("TEMP"))

Output:
Copy the codeThe code is as follows:

RDEV
C:\WINDOWS
nil
C:\Program Files
C:\Documents and Settings\baiyun\Application Data
C:\Documents and Settings\All Users
C:\Program Files\Common Files
BAIYUN
baiyun
C:\Documents and Settings\baiyun
C:\WINDOWS\system32\
2
Windows_NT
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.py;.pyw;.wlua
x86
x86 Family 15 Model 6 Stepping 5, GenuineIntel
15
0605
RDEV
C:\WINDOWS
C:\DOCUME~1\baiyun\LOCALS~1\Temp

(filename)

Function: Delete a file or an empty directory. If the function call fails, add nil and error message.

(oldname, newname)

Function: Change a file or directory name, if the function call fails, add nil and error message

(locale [, category])

Function: Set the current settings of the program, the function returns the latest value, and if it fails, it returns nil
parameter:
locale: a string that specifies the current setting
"": An empty string, the current setting is considered a local setting
"c": The current setting is considered as the standard c setting
nil: Returns the current value of the category indicating the setting name
category: A setting name that describes the change
"all"[default], "collate", "ctype", "monetary", "numeric", "time"

([table])

Function: Return a time value (number) according to the content of the table. If there is no parameter, it returns the field of the current time table:

Copy the codeThe code is as follows:

  year, month, day, hour, min, sec, isdst

For example:
Copy the codeThe code is as follows:

print(());
-->1249887340
print(({year=1970, month=1, day=1, hour=0}));
-->10500

()

Function: Return a temporary file name