To ensure high portability, Lua provides only a small number of features, especially OS-related libraries. However, Lua also provides some extension libraries, such as POSIX libraries. For file operations, this library only providesOS. RenameFunctions andOS. RemoveFunction.
1. Date and Time:
In Lua, the functionTimeAndDateAll date and time functions are provided.
If the time function is called without any parameters, it returns the current date and time in number format. If a table is used as a parameter, it returns a number indicating the date and time described in the table. The valid fields of the table are as follows:
| Field name |
Description |
| Year |
A complete year |
| Month |
01-12 |
| Day |
01-31 |
| Hour |
00-23 |
| Min |
00-59 |
| Sec |
00-59 |
| Isdst |
Boolean value. True indicates timeout. |
Print (OS. Time {year = 1970, month = 1, Day = 1, hour = 8, min = 0 })-- Beijing is the UTC + 8 zone, so if hour is equal to UTC, it indicates 0 of UTC.
Print (OS. Time ())-- The number of seconds that the current time passes through from 00:00:00. The output value is 1333594721
The date function is the inverse function of time, that is, you can convert the numeric value returned by time to a more advanced readable format. The first parameter is a formatted string, indicating the expected date return format, the second parameter is the number of the date and time. The default value is the current date and time. For example:
1 Dd = OS. Date ( " * T " ,OS. Time ()) -- If the formatted string is "* t", the function returns a date object in table format. If it is "! * T "indicates the UTC time format.
2 Print ( " Year = " .. Dd. Year)
3 Print ( " Month = " .. Dd. month)
4 Print ( " Day = " .. Dd. Day)
5 Print ( " Weekday = " .. Dd. wday) -- The day of a week, and Sunday is the first day.
6 Print (" Yearday = " .. Dd. yday) -- The day of the year, and the first day of the month
7 Print ( " Hour = " .. Dd. hour)
8 Print ( " Min = " .. Dd. min)
9 Print ( " SEC = " .. Dd. Sec)
10
11 --[[
12 Year = 2012
13 Month = 4
14 Day = 5
15 Weekday = 5
16 Yearday = 96
17 Hour = 11
18 Min = 13
19 SEC = 44
20 -- ]
The format ID of the date function is exactly the same as that of the strftime function in the C Runtime Library. See the following table:
| Keywords |
Description |
| % |
Abbreviation of the number of days in a week, such as wed |
| % |
The full name of days in one week, such as Friday |
| % B |
Abbreviation of month, such as SEP |
| % B |
The full name of a month, such as September. |
| % C |
Date and Time |
| % D |
The day of the month (01-31) |
| % H |
Hours in the 24-hour system (00-23) |
| % I |
Hours in the 12-hour system (01-12) |
| % J |
Day of the year (001-366) |
| % M |
Minute (00-59) |
| % M |
Month (01-12) |
| % P |
"Am" or "PM )" |
| % S |
Seconds (00-59) |
| % W |
The day of the week (0-6 is equivalent to Sunday-Saturday) |
| % X |
Date, for example |
| % X |
Time, such as 23:48:20 |
| % Y |
Two-digit year (00-99) |
| % Y |
Complete year (2012) |
| % |
Character '%' |
Print (OS. date ("% Y-% m-% d "))-- Output
The description of the CPU time returned by the function OS. Clock (), which is usually used to calculate a period of time.CodeExecution efficiency. For example:
1 Local X = OS. Clock ()
2 Local S = 0
3 For I = 1 , 10000000 Do
4 S = S + I
5 End
6 Print ( String. Format ( " Elapsed time: %. 2f \ n " , OS. Clock ()-X ))
7
8 -- Output result:
9 -- Elapsed time: 0.21
2. Other system calls:
FunctionOS. Exit ()Can abort the currentProgram. FunctionOS. getenv ()Obtains the value of an environment variable. For example:
Print (OS. getenv ("path "))-- If the environment variable does not exist, Nil is returned.
OS .exe cuteThe function is used to execute commands related to the operating system, such:
OS .exe cute ("mkdir" .. "hello ")