Calculate the Elapsed Time in Sensible Units

Share:

Description

Function to calculate the elapsed time between 2 time periods (in seconds), or to calculate a number of seconds into a time measurement in more sensible units.

Usage

1
timestring(time1, time2=NA, units=NA, show.units=TRUE)

Arguments

time1

either the time index (from Sys.time()) at the start of the time period, a length of time in seconds, or an object of class 'difftime'.

time2

either the time index (from Sys.time()) at the end of the time period, or missing data if converting a single length of time. Default NA.

units

either missing, in which case a sensible time unit is chosen automatically, or one of 's', 'm', 'h', 'd', 'w', 'y' to force a specific unit. Default NA.

show.units

if TRUE, then the time is returned with units, if FALSE then just an integer is returned. Default TRUE.

Value

A time measurement, with or without units.

See Also

Sys.time

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# time how long it takes to complete a task:

pre.time <- Sys.time()
Sys.sleep(2)  # PROCESS TO TIME
post.time <- Sys.time()
timestring(pre.time, post.time)

# Convert 4687 seconds into hours:

timestring(4687, units='hours', show.units=FALSE)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.