Lua system library

Source: Internet
Author: User

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 provides the OS. Rename function and OS. Remove function.

1. Date and Time:
In Lua, the time and date functions provide all the date and time functions.
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 UTC, So If hour is equal to UTC, it indicates UTC 0.
Print (OS. Time () -- the number of seconds that the current time passes through at 00:00:00, January 1. 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 the week, and the day of the week.
6 print ("yearday ="... dd. yday) -- the day of the year, and the first day of the year.
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 function OS. Clock () returns the CPU time description, which is usually used to calculate the execution efficiency of a piece of code. 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 -- the output result is:
9 -- elapsed time: 0.21


2. Other system calls:
Function OS. Exit () can abort the execution of the current program. Function OS. getenv () can be used to obtain the value of an environment variable. For example:
Print (OS. getenv ("path") -- if the environment variable does not exist, Nil is returned.
The OS .exe cute function is used to execute operating system-related commands, such:
OS .exe cute ("mkdir" .. "hello ")

Lua system library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.