Learn R Programming

SimDesign (version 2.23)

timeFormater: Format time string to suitable numeric output

Description

Format time input string into suitable numeric output metric (e.g., seconds). Input follows the SBATCH utility specifications. Accepted time formats include "minutes", "minutes:seconds", "hours:minutes:seconds", "days-hours", "days-hours:minutes" and "days-hours:minutes:seconds". Alternatively, function can be used to convert numeric input to SBATCH format.

Usage

timeFormater(time, output = "sec", input = "min", sround = floor)

Arguments

time

a character string to be formatted. If a numeric vector is supplied then this will be interpreted as minutes due to character coercion.

output

type of numeric output to convert time into. Currently supported are 'sec' for seconds (default), 'min' for minutes, 'hour', and 'day'.

Alternatively, if time were numeric then setting output to 'SBATCH' will return a suitable SBATCH format.

input

if supplied time is a numeric, indicates what the value represents. Default assumes the input is in minutes (see output for supported values)

sround

function used to round last seconds computation

Details

For example, time = "60" indicates a maximum time of 60 minutes, time = "03:00:00" a maximum time of 3 hours, time = "4-12" a maximum of 4 days and 12 hours, and time = "2-02:30:00" a maximum of 2 days, 2 hours and 30 minutes.

Examples

Run this code

# Test cases (outputs in seconds)
timeFormater("4-12")        # day-hours
timeFormater("4-12:15")     # day-hours:minutes
timeFormater("4-12:15:30")  # day-hours:minutes:seconds

timeFormater("30")          # minutes
timeFormater("30:30")       # minutes:seconds
timeFormater("4:30:30")     # hours:minutes:seconds

# output in hours
timeFormater("4-12", output = 'hour')
timeFormater("4-12:15", output = 'hour')
timeFormater("4-12:15:30", output = 'hour')

timeFormater("30", output = 'hour')
timeFormater("30:30", output = 'hour')
timeFormater("4:30:30", output = 'hour')

# numeric input is understood as minutes
timeFormater(42)               # seconds
timeFormater(42, output='min') # minutes

# convert numeric inputs to SBATCH format
timeFormater(60, output='SBATCH')
timeFormater(3, output='SBATCH', input='day')
timeFormater(7000, output='SBATCH', input='sec')
timeFormater(100000, output='SBATCH', input='sec')

# rounding seconds
timeFormater(1.55555, output='SBATCH', input='sec') # floor default
timeFormater(1.55555, output='SBATCH', input='sec', sround=ceiling)
timeFormater(1.55555, output='SBATCH', input='sec', sround=\(x) round(x, 3))


Run the code above in your browser using DataLab