Introduction to the OS Library in Lua detail _lua

Source: Internet
Author: User
Tags current time locale lua

This library is implemented through the table OS

Os.clock ()

Function: Returns an approximate value for the CPU time used by a program

For example:

Copy Code code as follows:

Local x = Os.clock ();
Print (Os.clock ())
Local s = 0;
For i = 1, 100000000 do
s = s + i;
End
Print (String.Format ("Elapsed time:%.2f\n", Os.clock ()-X));

Output:
Copy Code code as follows:

0
Elapsed time:2.55

Os.date ([format [, Time]])

Function: Returns a string or table formatted with a date or time by format
If the time parameter is set, it is formatted at the specified times, otherwise the current time is formatted
Parameters:
Format
"!": formatted according to GMT.
"*t": will be returned to a band year (4-bit), month (1-12), Day (1--31), hour (0-23), Min (0-59), SEC (0-61), wday (Days of the week, Sunday to 1), Yday (number of years), and ISDS A table with a key name for T (whether daylight saves time true/false); Returns a string formatted with the Strftime function of C if there is no "*t"
Returns the formatted string os.date () <=> os.date ("%c") with the current system settings, without parameters

For example:

Copy Code code as follows:

t = os.date ("*t", Os.time ());
For I, V-pairs (t) do
Print (I, v);
End

Output:
Copy Code code as follows:

Hour 14
Min 58
Wday 2
Day 10
Month 8
Year 2009
SEC 18
Yday 222
ISDST false

For other format strings, os.date formats the date as a string

For example:

Copy Code code as follows:

Print (Os.date (' is%A, in%B '))-->today is Tuesday
Print (Os.date ("%x", 906000490)-->09/16/1998

All formatted strings are as follows:
Copy Code code as follows:

%a the number of days in a week (Wed)
%A the full name of the number of days in a week (Wednesday)
Abbreviation for%b month (SEP)
Full name of%B month (September)
%c Date and time (09/16/98 23:48:10)
Days of%d one months (16) [0 ~ 31]
%H 24 Hours (23) [00 ~ 23]
%I Hours in 12 hours (11) [01 ~ 12]
%j Day of the Year (259) [01 ~ 366]
%m minutes (48) [00 ~ 59]
%m months (09) [01 ~ 12]
%P "Morning (AM)" or "PM" (PM)
%s seconds (10) [00 ~ 59]
%w Day of the Week (3) [0 ~ 6 = Sunday ~ Saturday]

%w the first few weeks of the year 0 ~ 52
%x Date (09/16/98)
%x Time (23:48:10)
%y Two-digit year (90) [00 ~ 99]
%Y full Year (2009)
Percent% of string '% '

Os.difftime (T2, T1)

Function: Returns the number of seconds T1 to T2

For example:

Copy Code code as follows:

T1 = Os.time ();
For i = 0, 10000000 do
Os.time ();
End
T2 = Os.time ();
Print (Os.difftime (t2, T1));

Output:

2

Os.execute ([command])

Function: equivalent to the system function of C, return the state code

For example:

Copy Code code as follows:

Os.execute ("Pause")

Output:

Press any key to continue ...

Os.exit (

Copy Code code as follows:
)

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

For example:
[Code]
Os.exit (1)


Os.getenv (varname)-

Function: Returns the value of the environment variable varname the current process, and returns nil if the variable is undefined

For example:

Copy Code code as follows:

Print (Os.getenv ("UserDomain"))
Print (Os.getenv ("SystemRoot"))
Print (Os.getenv ("Os2LibPath"))
Print (Os.getenv ("ProgramFiles"))
Print (Os.getenv ("AppData"))
Print (Os.getenv ("AllUsersProfile"))
Print (Os.getenv ("CommonProgramFiles"))
Print (Os.getenv ("COMPUTERNAME"))
Print (Os.getenv ("USERNAME"))
Print (Os.getenv ("UserProfile"))
Print (Os.getenv ("ComSpec"))
Print (Os.getenv ("LogonServer"))
Print (Os.getenv ("Number_of_processors"))
Print (Os.getenv ("OS"))
Print (Os.getenv ("Pathext"))
Print (Os.getenv ("Processor_architecture"))
Print (Os.getenv ("Processor_identifier"))
Print (Os.getenv ("Processor_level"))
Print (Os.getenv ("Processor_revision"))
Print (Os.getenv ("UserDomain"))
Print (Os.getenv ("SystemRoot"))
Print (Os.getenv ("TEMP"))

Output:
Copy Code code 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\cmd.exe
2
Windows_NT
. COM;. EXE;. BAT;. CMD;. VBS;. VBE;. JS;. JSE;. WSF;. WSH;. py;. Pyw;. Wlua
x86
x86 Family Model 6 stepping 5, Genuineintel
15
0605
Rdev
C:\WINDOWS
C:\docume~1\baiyun\locals~1\temp

Os.remove (filename)

Function: Delete the file or an empty directory, if the function call fails to add nil plus error message

Os.rename (Oldname, NewName)

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

Os.setlocale (locale [, category])

Function: Set the current setting of the program, the function returns the most recent value, and the failure returns nil
Parameters:
Locale: A string that specifies the current setting
' ': An empty string, the current setting is treated as a local setting
"C": Current settings are considered standard C settings
Nil: Returns the current value of the category indicating the setting name
Category: A name that describes the settings to be changed
"All" [default], "collate", "CType", "monetary", "numeric", "Time"

Os.time ([table])

Function: Returns a time value (number) by the contents of the table, and returns the field of the current time table without parameters:

Copy Code code as follows:

Year, month, day, hour, Min, sec, isdst

For example:
Copy Code code as follows:

Print (Os.time ());
-->1249887340
Print (Os.time ({year=1970, month=1, Day=1, hour=0}));
-->10500

Os.tmpname ()

Function: Returns a temporary filename

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.