function::local_clock_s - Linux
Overview
function::local_clock_s
: Get a local clock in seconds.
Produces a monotonically increasing 64-bit integer, which corresponds to the number of seconds that have elapsed since the start of the epoch. Note that this is the number of wall seconds elapsed, not seconds as measured by a CPU clock cycle.
This function does not adjust for non-monotonic clock changes caused by leap seconds. It’s recommended that you use function::local_clock
instead, if possible.
Syntax
function::local_clock_s(start_date_time)
Parameters
start_date_time
: The date and time from which to measure the elapsed time. If not specified, the start of the epoch (1970-01-01 00:00:00 UTC) is used.
Options/Flags
None.
Examples
Simple Use
function::local_clock_s()
Prints the number of seconds that have elapsed since the start of the epoch.
Measuring Elapsed Time
start_time=$(function::local_clock_s)
# Do something that takes time
end_time=$(function::local_clock_s)
elapsed_time=$((end_time - start_time))
echo "Elapsed time: $elapsed_time seconds"
Prints the number of seconds that have elapsed between the two function calls.
Common Issues
Non-Monotonic Clock Changes
This function does not adjust for non-monotonic clock changes caused by leap seconds. If this is a concern, use function::local_clock
instead.
Integration
This command can be combined with other Linux commands or tools for advanced tasks. For example, it can be used to create a simple stopwatch:
while true; do
clear
current_time=$(function::local_clock_s)
elapsed_time=$((current_time - start_time))
echo "Elapsed time: $elapsed_time seconds"
sleep 1
done
Related Commands
function::local_clock
: Get a local clock in nanoseconds.date
: Print the current date and time.