os¶
Functions¶
number os.clock()¶
1 2 3 4 5 6 7 8 |
|
In this example, we use os.clock() to measure the time taken to execute a loop that counts from 1 to 1000000. We store the starting time in the start variable using os.clock(), and then execute the loop. After the loop has completed, we calculate the elapsed time by subtracting the starting time from the current time using os.clock() - start, and store the result in the elapsed variable. Finally, we print the elapsed time using print("Elapsed time:", elapsed, "seconds"). Note that os.clock() returns CPU time, which may not correspond directly to wall-clock time. The actual elapsed time may be longer or shorter than the CPU time depending on factors such as system load and CPU speed.
string os.date(string format, number time)¶
format
The date format.time
The time.
1 2 3 4 5 |
|
In this example, we use os.time() to get the current time as a Unix timestamp, and store it in the currentTime variable. We then use os.date() to format the timestamp as a string in the format "%Y-%m-%d %H:%M:%S", which represents the year, month, day, hour, minute, and second of the timestamp. We store the formatted string in the formattedTime variable.
Finally, we print the formatted time using print("Current time:", formattedTime).
The output of this program will be a string representing the current date and time in the specified format, like this:
1 |
|
Note that the os.date() function supports a wide range of format codes for representing different parts of the date and time, such as %a for the abbreviated weekday name and %Y for the year with century as a decimal number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
number os.difftime(number t2, number t1)¶
t2
The later time.t1
The earlier time.
1 2 3 4 5 6 |
|
In this example, we use os.time() to get the current time as a Unix timestamp, and store it in the currentTime variable. We also use os.time() to create another timestamp, earlierTime, which represents February 1, 2023 at midnight. We store this timestamp in the earlierTime variable.
We then use os.difftime() to calculate the difference in seconds between the two timestamps, currentTime and earlierTime, and store the result in the timeDifference variable.
Finally, we print the time difference in seconds using print("Time difference:", timeDifference, "seconds").
The output of this program will be the time difference between the two timestamps in seconds, which will depend on the current time when the program is run. For example, if the current time is February 16, 2023 at 19:28 PM, the output might look like this:
1 |
|
number os.time()¶
1 2 3 4 5 6 7 |
|
In this example, we use os.time() to get the current time as a Unix timestamp, and store it in the currentTime variable. We then create a new table that specifies a date and time of February 16, 2023 at 8:00 PM, and pass it to os.time() to convert it to a Unix timestamp. We store the resulting timestamp in the timestamp variable.
Finally, we print the two timestamps using print("Current time:", currentTime) and print("Timestamp for Feb 16, 2023 at 8:00 PM:", timestamp).
The output of this program will be two Unix timestamps representing the current time and the specified date and time, like this:
1 2 |
|
Note that the table passed to os.time() must have the required fields for the date and time. The fields can include year, month, day, hour, min, sec, and isdst. If the isdst field is not provided, Lua assumes that daylight saving time is not in effect. If the table contains only a subset of the required fields, os.time() will use default values for the missing fields.