Skip to content

Commit

Permalink
Add QOL improvements to the timestr functions
Browse files Browse the repository at this point in the history
  • Loading branch information
drivera-armedia committed Sep 13, 2024
1 parent 9505cbb commit d51b41a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,13 @@ secs_to_timestr()
local __NUM="(0|[1-9][0-9]*)"

[ -n "${__STR}" ] || return 1

# If it's already a timestring,
# then output it in lowercase
[[ "${__STR^^}" =~ ^(${__NUM}D)?(${__NUM}H)?(${__NUM}M)?(${__NUM}S)?$ ]] && \
echo -n "${__STR,,}" && return 0

# It's not a timestring, so parse it
[[ "${__STR}" =~ ^${__NUM}$ ]] || return 1

local __RET=""
Expand Down Expand Up @@ -895,6 +902,12 @@ timestr_to_secs()

# It could be a time string, so parse it out
[ -n "${__STR}" ] || return 1

# If it's already a number, then it's
# a seconds count so output it verbatim
[[ "${__STR}" =~ ^${__NUM}$ ]] && echo -n "${__STR}" && return 0

# It's not a seconds count, so parse it!
[[ "${__STR^^}" =~ ^(${__NUM}D)?(${__NUM}H)?(${__NUM}M)?(${__NUM}S)?$ ]] || return 1

local __PARTS=( "${BASH_REMATCH[@]:1}" )
Expand Down

0 comments on commit d51b41a

Please sign in to comment.