Skip to main content

lua_std/
os.rs

1//! The operating system library.
2//!
3//! Operating system facilities: the clock, dates and times, environment
4//! variables, file management and process control.
5
6use crate::{any, boolean, number, option, string, table};
7
8/// Returns the CPU time used by the program.
9///
10/// # Return value
11///
12/// An approximation of the amount in seconds of CPU time used by the
13/// program, as returned by the underlying ISO C function `clock`. Useful
14/// for benchmarking.
15///
16/// # Examples
17///
18/// ```lua
19/// local t0 = os.clock()
20/// for i = 1, 1e7 do end
21/// print(os.clock() - t0)    --> 0.031  (elapsed CPU seconds)
22/// ```
23///
24/// # See also
25///
26/// [`time`], [`difftime`]
27pub fn clock() -> number {
28    todo!()
29}
30
31/// Formats a date and time.
32///
33/// # Parameters
34///
35/// - **`format`**: a format string following the ISO C function
36///   `strftime` (default `"%c"`, the full date and time). A leading
37///   `"!"` formats in UTC instead of local time. The special format
38///   `"*t"` (or `"!*t"`) requests a table instead of a string.
39/// - **`time`**: the time to format, as returned by [`time`] (default:
40///   the current time).
41///
42/// # Return value
43///
44/// A string with the formatted date, or, for the `"*t"` formats, a table
45/// with fields `year`, `month` (1–12), `day` (1–31), `hour` (0–23),
46/// `min` (0–59), `sec` (0–61 due to leap seconds), `wday` (weekday,
47/// 1–7, Sunday is 1), `yday` (day of the year, 1–366), and `isdst`
48/// (daylight saving flag, a boolean).
49///
50/// # Examples
51///
52/// ```lua
53/// print(os.date("%Y-%m-%d"))          --> 2026-09-03
54/// print(os.date("!%H:%M UTC"))        --> 18:45 UTC
55/// local t = os.date("*t")
56/// print(t.year, t.month, t.day)       --> 2026   9   3
57/// ```
58///
59/// # See also
60///
61/// [`time`], [`difftime`]
62pub fn date(format: option<string>, time: option<number>) -> any {
63    todo!()
64}
65
66/// Returns the difference between two times.
67///
68/// # Parameters
69///
70/// - **`t2`**: the later time, as returned by [`time`].
71/// - **`t1`**: the earlier time.
72///
73/// # Return value
74///
75/// The difference, in seconds, from time `t1` to time `t2`, as a float.
76/// On POSIX, Windows and most other systems this is exactly `t2 - t1`.
77///
78/// # Examples
79///
80/// ```lua
81/// local before = os.time()
82/// -- ... some slow work ...
83/// print(os.difftime(os.time(), before))    --> 2.0
84/// ```
85///
86/// # See also
87///
88/// [`time`], [`clock`]
89pub fn difftime(t2: number, t1: number) -> number {
90    todo!()
91}
92
93/// Runs a shell command.
94///
95/// # Parameters
96///
97/// - **`command`**: the command passed to the operating system shell.
98///   When absent, the function instead reports whether a shell is
99///   available.
100///
101/// # Return value
102///
103/// A boolean that is true when the command terminated successfully,
104/// followed by the string `"exit"` (the command ended normally) or
105/// `"signal"` (it was terminated by a signal) and the return status.
106/// When called without a command, returns only a boolean indicating
107/// whether a shell is available.
108///
109/// # Examples
110///
111/// ```lua
112/// print(os.execute("exit 0"))    --> true   exit   0
113/// print(os.execute("exit 3"))    --> nil    exit   3
114/// print(os.execute())            --> true   (a shell exists)
115/// ```
116///
117/// # See also
118///
119/// [`io::popen`](crate::io::popen)
120pub fn execute(command: option<string>) -> (boolean, string, number) {
121    todo!()
122}
123
124/// Terminates the host program.
125///
126/// # Parameters
127///
128/// - **`code`**: true for a successful exit (the default), false for a
129///   failure exit, or a numeric return code.
130/// - **`close`**: when true, closes the Lua state before exiting,
131///   running finalizers and to-be-closed variables.
132///
133/// # Description
134///
135/// Calls the ISO C function `exit` to terminate the host program. This
136/// function never returns.
137///
138/// # Examples
139///
140/// ```lua
141/// if not config_ok then
142///   io.write("bad configuration\n")
143///   os.exit(1)
144/// end
145/// ```
146pub fn exit(code: option<any>, close: option<boolean>) {
147    todo!()
148}
149
150/// Reads an environment variable.
151///
152/// # Parameters
153///
154/// - **`varname`**: the name of the environment variable.
155///
156/// # Return value
157///
158/// The value of the process environment variable `varname`, or `nil` if
159/// the variable is not defined.
160///
161/// # Examples
162///
163/// ```lua
164/// print(os.getenv("HOME"))          --> /home/ana
165/// print(os.getenv("NO_SUCH_VAR"))   --> nil
166/// ```
167pub fn getenv(varname: string) -> option<string> {
168    todo!()
169}
170
171/// Deletes a file.
172///
173/// # Parameters
174///
175/// - **`filename`**: the name of the file (or empty directory, on POSIX
176///   systems) to delete.
177///
178/// # Return value
179///
180/// True on success; in case of errors, `nil` plus an error message and
181/// an error code.
182///
183/// # Examples
184///
185/// ```lua
186/// local ok, err = os.remove("old.log")
187/// if not ok then print("could not remove:", err) end
188/// ```
189///
190/// # See also
191///
192/// [`rename`]
193pub fn remove(filename: string) -> option<boolean> {
194    todo!()
195}
196
197/// Renames a file or directory.
198///
199/// # Parameters
200///
201/// - **`oldname`**: the current name.
202/// - **`newname`**: the new name.
203///
204/// # Return value
205///
206/// True on success; in case of errors, `nil` plus an error message and
207/// an error code.
208///
209/// # Examples
210///
211/// ```lua
212/// assert(os.rename("draft.txt", "final.txt"))
213/// ```
214///
215/// # See also
216///
217/// [`remove`]
218pub fn rename(oldname: string, newname: string) -> option<boolean> {
219    todo!()
220}
221
222/// Sets the current locale of the program.
223///
224/// # Parameters
225///
226/// - **`locale`**: a system-dependent locale name. `""` selects the
227///   native environment locale, `"C"` the standard C locale, and `nil`
228///   only queries the current locale.
229/// - **`category`**: which category to change: `"all"` (the default),
230///   `"collate"`, `"ctype"`, `"monetary"`, `"numeric"` or `"time"`.
231///
232/// # Return value
233///
234/// The name of the new locale, or `nil` if the request cannot be
235/// honoured.
236///
237/// # Examples
238///
239/// ```lua
240/// print(os.setlocale("C"))    --> C
241/// ```
242pub fn setlocale(locale: option<string>, category: option<string>) -> option<string> {
243    todo!()
244}
245
246/// Returns the current time, or converts a date table to a time.
247///
248/// # Parameters
249///
250/// - **`table`**: a table describing a local date, with mandatory fields
251///   `year`, `month` and `day`, and optional fields `hour` (default 12),
252///   `min`, `sec` (default 0) and `isdst`.
253///
254/// # Return value
255///
256/// A time value, whose meaning depends on the system — on POSIX and
257/// Windows, the number of seconds since the epoch. When called without
258/// arguments, the current time. The returned value can be used with
259/// [`date`] and [`difftime`].
260///
261/// # Examples
262///
263/// ```lua
264/// print(os.time())    --> 1788806700
265/// print(os.time({ year = 2026, month = 9, day = 3 }))
266/// --> 1788782400
267/// ```
268///
269/// # See also
270///
271/// [`date`], [`difftime`], [`clock`]
272pub fn time(t: option<table>) -> number {
273    todo!()
274}
275
276/// Returns a name usable for a temporary file.
277///
278/// # Return value
279///
280/// A string with a file name that can be used for a temporary file. The
281/// file must be explicitly opened before use and explicitly removed when
282/// no longer needed. When possible, prefer
283/// [`io::tmpfile`](crate::io::tmpfile), which removes the file
284/// automatically when the program ends.
285///
286/// # Examples
287///
288/// ```lua
289/// local name = os.tmpname()
290/// print(name)    --> /tmp/lua_Fx3kzM
291/// ```
292///
293/// # See also
294///
295/// [`io::tmpfile`](crate::io::tmpfile)
296pub fn tmpname() -> string {
297    todo!()
298}