Lua os functions
Lua os functions

These are the functions in the "os" table. Some of these functions are disabled by default in the MUSHclient "Lua sandbox". Check that out if you plan to use the "os" functions.




os.clock ()

Returns the approximate amount of CPU time used by the current process in seconds.


print (os.clock () / 60 / 60) --> 34.381006944444 (hours)



os.date (format, time)

Returns a string with the date and time formatted according to the format given. If no time is supplied defaults to the current time.

The format string can consist of literal strings, mixed with the following directives:


*t - produces a date table rather than a string

%a - Abbreviated weekday name
%A - Full weekday name
%b - Abbreviated month name
%B - Full month name
%c - Date and time representation appropriate for locale
%d - Day of month as decimal number (01 - 31)
%H - Hour in 24-hour format (00 - 23)

%I - Hour in 12-hour format (01 - 12)
%j - Day of year as decimal number (001 - 366)
%m - Month as decimal number (01 - 12)
%M - Minute as decimal number (00 - 59)
%p - Current locale’s A.M./P.M. indicator for 12-hour clock
%S - Second as decimal number (00 - 59)
%U - Week of year as decimal number, with Sunday as first day of week (00 - 53)
%w - Weekday as decimal number (0 - 6; Sunday is 0)
%W - Week of year as decimal number, with Monday as first day of week (00 - 53)
%x - Date representation for current locale
%X - Time representation for current locale
%y - Year without century, as decimal number (00 - 99)
%Y - Year with century, as decimal number
%z, %Z  - Time-zone name or abbreviation; no characters if time zone is unknown
%% - Percent sign


The # flag may prefix any formatting code. In that case, the meaning of the format code is changed as follows.


%#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% - # flag is ignored.

%#c - Long date and time representation, appropriate for current locale. For example: "Tuesday, March 14, 1995, 12:41:29".

%#x - Long date representation, appropriate to current locale. For example: "Tuesday, March 4, 1995".

%#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y - Remove leading zeros (if any).


Examples:


print (os.date ("%x")) --> 12/11/05
print (os.date ("%#x")) --> Sunday, December 11, 2005

t = os.date ("*t") --> produces table as below:

t = {
  hour=19,  min=49,  wday=1,  year=2005, 
  yday=345,  month=12,  sec=11,  day=11,  isdst=true
  }




os.difftime (t1, t2)

Returns the number of seconds from time t1 to time t2.


t1 = os.time ()
-- do something lengthy here ..
t2 = os.time ()
print (os.difftime (t1, t2)) --> -3



os.execute (command)

Passes 'command' to the operating system shell for execution. Returns a status code.


status = os.execute ("dir") --> (directory listing whizzes by)


Here is a method of capturing the output to a file, and reading it in:


-- get a temporary file name
n = os.tmpname ()

-- execute a command
os.execute ("dir > " .. n)

-- display output
for line in io.lines (n) do
  print (line)
end

-- remove temporary file
os.remove (n)



os.exit (n)

Terminates the host program with the return code n.

However, os.exit is doest not do that in MUSHclient. It will raise an error if called. This is because doing an immediate exit bypasses all checks to save open documents, save plugin states, and so on.


os.getenv (v)

Returns the environment variable v, or nil if undefined.



print (os.getenv ("PATH")) --> (my PATH variable)



os.remove (filename)

Deletes the file. On success, returns true.
If it cannot, it returns nil followed by an error message.


os.remove ("test.txt") --> true
ok, err = os.remove ("test.txt") --> nil, test.txt: No such file or directory



os.rename (oldname, newname)

Renames a file. On success, returns true.
If it cannot, it returns nil followed by an error message.


os.rename("test.txt", "test2.txt") --> true
os.rename("test.txt", "test2.txt") --> nil, test.txt: File exists 



os.setlocale (locale, category)

Sets the current locale to the supplied locale. Category, which is optional, is a string which is one of:


  • all (the default)
  • collate
  • ctype
  • monetory
  • numeric
  • time


Returns the name of the new locale, or nil if invalid.


print (os.setlocale ("en", "collate")) --> English_United States.1252


Hint: I found that in Australia, the default in Lua was to use the USA time format (MM/DD/YY) rather than the local format (DD/MM/YY) when displaying dates with os.date. This line seemed to fix that:


print (os.setlocale ("", "time")) --> English_Australia.1252


According to the help file "If locale points to an empty string, the locale is the implementation-defined native environment.". Thus the empty locale string seemed take the country default.


os.time (t)

Returns the current time if no argument supplied. The time is in seconds from the start of the current epoch.

On Windows, the function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock.

If an argument is supplied it must be a table with at least the entries:


  • year
  • month
  • day


The table can also have the entries:



  • hour
  • min
  • sec
  • isdst


This table can be produced by calling: os.date ("*t")


print (os.time { year = 2004, month = 8, 
                 day = 30, hour = 6, min = 32, sec = 1, dst = true }) --> 1093811521
print (os.date ("%#x %#X", 1093811521))  --> Monday, August 30, 2004 06:32:01                 



os.tmpname ()

Returns a string with a name for a temporary file. Considered unsafe in an untrusted environment because someone may substitute a different file for the one whose name is returned to you, in the small window of time between obtaining the file name, and opening the file.


print (os.tmpname ()) --> /temp/s56.2



See Also ...

Topics

DOC_lua_base Lua base functions
DOC_lua_coroutines Lua coroutine functions
DOC_lua_debug Lua debug functions
DOC_lua_io Lua io functions
DOC_lua_math Lua math functions
DOC_lua_package Lua package functions
DOC_lua Lua script extensions
DOC_lua_string Lua string functions
DOC_lua_tables Lua table functions

(Help topic: general=lua_os)

DOC_contents Documentation contents page