Operating System Library in Lua

Source: Internet
Author: User

This library is implemented through table OS

OS. Clock ()
Function: returns an approximate value of CPU time used by a program.

For example:
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:
0
Elapsed time: 2.55

--------------------------------------------------------------------------------

OS. Date ([format [, time])
Function: returns a string or table formatted by format.
If the time parameter is set, it is formatted according to the time specified. Otherwise, it is formatted according to the current time.
Parameters:
Format:
"! ": Format by GMT time.
"* T": returns one with year (4 digits), month (1-12), Day (1--31), hour (0-23 ), min (0-59), SEC (0-61), wday (day of the week, 1 on Sunday), yday (days of the year ), and isdst (whether it is daylight saving time true/false) of the table with a key name; if there is no "* t", return a string formatted by the strftime function of C
If no parameter is specified, the formatted string OS. Date () <=> OS. date ("% C") is returned based on the current system settings ")

For example:
T = OS. date ("* t", OS. Time ());
For I, V in pairs (t) Do
Print (I, V );
End

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

For other format strings, OS. date will format the date as a string

For example:
Print (OS. date ("Today is % A, in % B") --> today is Tuesday, in May
Print (OS. date ("% x", 906000490) --> 09/16/1998

All formatted strings are as follows:

% A short for the number of days in one week (WED)
% A full name of days in one week (Wednesday)
Abbreviated as % B month (SEP)
% B FULL NAME (September)
% C date and time (09/16/98 23:48:10)
% D the day of the month (16) [0 ~ 31]
Hour (23) in the % H 24-hour system [00 ~ 23]
Hours In % I 12-hour system (11) [01 ~ 12]
% J the day of the year (259) [01 ~ 366]
% M minutes (48) [00 ~ 59]
% M months (09) [01 ~ 12]
% P "am" or "PM" (pm)
% S (10) [00 ~ 59]
% W the day of the first week (3) [0 ~ 6 = Sunday ~ Saturday]

% W the week of the year 0 ~ 52
% X date (09/16/98)
% X time (23:48:10)
% Y double-digit year (90) [00 ~ 99]
% Y full year (2009)
% String '%'

--------------------------------------------------------------------------------

OS. difftime (t2, T1)
Function: returns the number of seconds between t1 and t2.

For example:
T1 = OS. Time ();
For I = 0, 10000000 do
OS. Time ();
End
T2 = OS. Time ();
Print (OS. difftime (t2, T1 ));

Output:
2

--------------------------------------------------------------------------------

OS .exe cute ([command])
Function: returns the system status code equivalent to the system function of C.

For example:
OS .exe cute ("pause ")

Output:
Press any key to continue...

--------------------------------------------------------------------------------

OS. Exit ([Code])
Function: equivalent to the exit function of C. It terminates the main program and the return value is code.

For example:
OS. Exit (1)

--------------------------------------------------------------------------------

OS. getenv (varname )-
Function: returns the varname value of the environment variable of the current process. If the variable is not defined, Nil is returned.

For example:
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:
Rdev
C: \ WINDOWS
Nil
C: \ Program Files
C: \ Documents ents and Settings \ policyun \ Application Data
C: \ Documents ents and Settings \ All Users
C: \ Program Files \ common files
Baiyun
Baiyun
C: \ Documents ents and Settings \ policyun
C: \ windows \ system32 \ cmd.exe
Http://www.cnblogs.com/whiteyun/admin/file://rdev1/
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: \ release E ~ 1 \ policyun \ locals ~ 1 \ Temp

--------------------------------------------------------------------------------

OS. Remove (filename)
Function: delete a file or an empty directory. If the function call fails, add the NIL error message.

--------------------------------------------------------------------------------

OS. Rename (oldname, newname)
Function: Change the name of a file or directory. If the function call fails, add the NIL error message.

--------------------------------------------------------------------------------

OS. setlocale (Locale [, category])
Function: sets the current settings of the program. The function returns the latest value. If the function fails, Nil is returned.
Parameters:
Locale: a specified string
"": An empty string. The current setting is considered as a local setting.
"C": the current setting is regarded as the standard C setting.
Nil: returns the current value of the category indication set name.
Category: the name of the configuration to be modified.
"All" [Default], "collate", "ctype", "monetary", "numeric", "Time"

--------------------------------------------------------------------------------

OS. Time ([Table])
Function: returns a time value (number) based on the table content. If no parameter is specified, the current time is returned.
Table field:
Year, month, day, hour, Min, SEC, isdst

For example:
Print (OS. Time ());
--> 1249887340
Print (OS. Time ({year = 1970, month = 1, Day = 1, hour = 0 }));
--> 10500

--------------------------------------------------------------------------------

OS. tmpname ()
Function: returns a temporary file name.

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.