This will convert a second into a more feasible number split up into parts of hrs/min/sec (or if you want also days and weeks and years)

Provided 5 configurations of this converter for your use:

* hms (hour minute second)
* dhms (day hour minute second)
* wdhms (week day hour minute second)
* ywdhms (year week day hour minute second)
* ydhms (year day hour minute second)

Note that the value on the left most can be anything from 0 to infinity (and in the negative range), but the numbers to the right will be rotated/moded accordingly (seconds will be rotated every 60, minutes every 60, hours every 24, weeks every 7, or days every 365 if using the last config there)

Each function provided below will be demonstrated via test variables
Notice there are many correct ways to output the results.

Citation/used this article as source for algorithm: http://stackoverflow.com/questions/12199631/convert-seconds-to-hours-minutes-seconds-in-bash

Here is the original method (from the above link):

# using this elegant provided method:
convertsecs() {
 ((h=${1}/3600))
 ((m=(${1}%3600)/60))
 ((s=${1}%60))
 printf "%02d:%02d:%02d\n" $h $m $s
}
TIME1="36"
TIME2="1036"
TIME3="91925"
echo $(convertsecs $TIME1)
echo $(convertsecs $TIME2)
echo $(convertsecs $TIME3)

Here are my methods:

hour:min:sec

# convert seconds to hour:min:sec
convertsecs2hms() {
 ((h=${1}/3600))
 ((m=(${1}%3600)/60))
 ((s=${1}%60))
 printf "%02d:%02d:%02d\n" $h $m $s
 # PRETTY OUTPUT: uncomment below printf and comment out above printf if you want prettier output
 # printf "%02dh %02dm %02ds\n" $h $m $s
}
# setting test variables: testing some constant variables & evaluated variables
TIME1="36"
TIME2="1036"
TIME3="91925"
# outputting results: just one way to show results (via echo & command substitution with parenthesis)
echo $(convertsecs2hms $TIME1)
echo $(convertsecs2hms $TIME2)
echo $(convertsecs2hms $TIME3)

# OUTPUT WOULD BE LIKE THIS (If none pretty printf used): 
# 00:00:36
# 00:17:16
# 25:32:05
# OUTPUT WOULD BE LIKE THIS (If pretty printf used): 
# 00h 00m 36s
# 00h 17m 16s
# 25h 32m 05s

day-hour:min:sec

# convert seconds to day-hour:min:sec
convertsecs2dhms() {
 ((d=${1}/(60*60*24)))
 ((h=(${1}%(60*60*24))/(60*60)))
 ((m=(${1}%(60*60))/60))
 ((s=${1}%60))
 printf "%02d-%02d:%02d:%02d\n" $d $h $m $s
 # PRETTY OUTPUT: uncomment below printf and comment out above printf if you want prettier output
 # printf "%02dd %02dh %02dm %02ds\n" $d $h $m $s
}
# setting test variables: testing some constant variables & evaluated variables
TIME1="36"
TIME2="1036"
TIME3="91925"
# one way to output results
((TIME4=$TIME3*2)) # 183850
((TIME5=$TIME3*$TIME1)) # 3309300
((TIME6=100*86400+3*3600+40*60+31)) # 8653231 s = 100 days + 3 hours + 40 min + 31 sec
# outputting results: another way to show results (via echo & command substitution with backticks)
echo $TIME1 - `convertsecs2dhms $TIME1`
echo $TIME2 - `convertsecs2dhms $TIME2`
echo $TIME3 - `convertsecs2dhms $TIME3`
echo $TIME4 - `convertsecs2dhms $TIME4`
echo $TIME5 - `convertsecs2dhms $TIME5`
echo $TIME6 - `convertsecs2dhms $TIME6`

# OUTPUT WOULD BE LIKE THIS (If none pretty printf used): 
# 36 - 00-00:00:36
# 1036 - 00-00:17:16
# 91925 - 01-01:32:05
# 183850 - 02-03:04:10
# 3309300 - 38-07:15:00
# 8653231 - 100-03:40:31
# OUTPUT WOULD BE LIKE THIS (If pretty printf used): 
# 36 - 00d 00h 00m 36s
# 1036 - 00d 00h 17m 16s
# 91925 - 01d 01h 32m 05s
# 183850 - 02d 03h 04m 10s
# 3309300 - 38d 07h 15m 00s
# 1000000000 - 11574d 01h 46m 40s

week:day-hour:min:sec

# convert seconds to week:day-hour:min:sec
convertsecs2wdhms() {
 ((w=${1}/(60*60*24*7)))
 ((d=(${1}%(60*60*24*7))/(60*60*24)))
 ((h=(${1}%(60*60*24))/(60*60)))
 ((m=(${1}%(60*60))/60))
 ((s=${1}%60))
 printf "%02d:%02d-%02d:%02d:%02d\n" $w $d $h $m $s
 # PRETTY OUTPUT: uncomment below printf and comment out above printf if you want prettier output
 # printf "%02dw %02dd %02dh %02dm %02ds\n" $w $d $h $m $s
}
# setting test variables: testing some constant variables & evaluated variables
TIME1="36"
TIME2="1036"
TIME3="91925"
((TIME4=$TIME3*2)) # 183850 s
((TIME5=$TIME3*$TIME1)) # 3309300 s
((TIME6=7*86400*10+7*86400+24*3600+40*60+31)) # 6741631 s = 11wks 1 day 0 hrs 40 min 31 sec
# outputting results: just one way to show results (via echo & command substitution with parenthesis)
echo "$TIME1 - $(convertsecs2wdhms $TIME1)"
echo "$TIME2 - $(convertsecs2wdhms $TIME2)"
echo "$TIME3 - $(convertsecs2wdhms $TIME3)"
echo "$TIME4 - $(convertsecs2wdhms $TIME4)"
echo "$TIME5 - $(convertsecs2wdhms $TIME5)"
echo "$TIME6 - $(convertsecs2wdhms $TIME6)"

# OUTPUT WOULD BE LIKE THIS (If none pretty printf used): 
# 36 - 00:00-00:00:36
# 1036 - 00:00-00:17:16
# 91925 - 00:01-01:32:05
# 183850 - 00:02-03:04:10
# 3309300 - 05:03-07:15:00
# 6741631 - 11:01-00:40:31
# OUTPUT WOULD BE LIKE THIS (If pretty printf used): 
# 36 - 00w 00d 00h 00m 36s
# 1036 - 00w 00d 00h 17m 16s
# 91925 - 00w 01d 01h 32m 05s
# 183850 - 00w 02d 03h 04m 10s
# 3309300 - 05w 03d 07h 15m 00s
# 8653231 - 14w 02d 03h 40m 31s

year:week:day-hour:min:sec

# convert seconds to year:week:day-hour:min:sec
convertsecs2ywdhms() {
 ((y=${1}/(60*60*24*7*52)))
 ((w=(${1}%(60*60*24*7*52))/(60*60*24*7)))
 ((d=(${1}%(60*60*24*7))/(60*60*24)))
 ((h=(${1}%(60*60*24))/(60*60)))
 ((m=(${1}%(60*60))/60))
 ((s=${1}%60))
 printf "%02d:%02d:%02d-%02d:%02d:%02d\n" $y $w $d $h $m $s
 # PRETTY OUTPUT: uncomment below printf and comment out above printf if you want prettier output
 # printf "%02dy %02dw %02dd %02dh %02dm %02ds\n" $y $w $d $h $m $s
}
# setting test variables: testing some constant variables & evaluated variables
TIME1="36"
TIME2="1036"
TIME3="91925"
((TIME4=$TIME3*2)) # 183850 s
((TIME5=$TIME3*$TIME1)) # 3309300 s
((TIME6=1000000000)) # 1,000,000,000 s (billion)
# outputting results: just one way to show results (via echo & command substitution with parenthesis)
echo $TIME1 - $(convertsecs2ywdhms $TIME1)
echo $TIME2 - $(convertsecs2ywdhms $TIME2)
echo $TIME3 - $(convertsecs2ywdhms $TIME3)
echo $TIME4 - $(convertsecs2ywdhms $TIME4)
echo $TIME5 - $(convertsecs2ywdhms $TIME5)
echo $TIME6 - $(convertsecs2ywdhms $TIME6)

# OUTPUT WOULD BE LIKE THIS (If none pretty printf used): 
# 36 - 00:00:00-00:00:36
# 1036 - 00:00:00-00:17:16
# 91925 - 00:00:01-01:32:05
# 183850 - 00:00:02-03:04:10
# 3309300 - 00:05:03-07:15:00
# 1000000000 - 31:41:03-01:46:40
# OUTPUT WOULD BE LIKE THIS (If pretty printf used): 
# 36 - 00y 00w 00d 00h 00m 36s
# 1036 - 00y 00w 00d 00h 17m 16s
# 91925 - 00y 00w 01d 01h 32m 05s
# 183850 - 00y 00w 02d 03h 04m 10s
# 3309300 - 00y 05w 03d 07h 15m 00s
# 6741631 - 00y 11w 01d 00h 40m 31s

year:day-hour:min:sec

# convert seconds to year:day-hour:min:sec
convertsecs2ydhms() {
 ((y=${1}/(60*60*24*365)))
 ((d=(${1}%(60*60*24*365))/(60*60*24)))
 ((h=(${1}%(60*60*24))/(60*60)))
 ((m=(${1}%(60*60))/60))
 ((s=${1}%60))
 printf "%02d:%02d-%02d:%02d:%02d\n" $y $d $h $m $s
 # PRETTY OUTPUT: uncomment below printf and comment out above printf if you want prettier output
 # printf "%02dy %02dd %02dh %02dm %02ds\n" $y $d $h $m $s
}
# setting test variables: testing some constant variables & evaluated variables
TIME1="36"
TIME2="1036"
TIME3="91925"
((TIME4=$TIME3*2)) # 183850 s
((TIME5=$TIME3*$TIME1)) # 3309300 s
((TIME6=1000000000)) # 1,000,000,000 s (billion)
# outputting results: just one way to show results (via echo & command substitution with parenthesis)
echo $TIME1 - $(convertsecs2ydhms $TIME1)
echo $TIME2 - $(convertsecs2ydhms $TIME2)
echo $TIME3 - $(convertsecs2ydhms $TIME3)
echo $TIME4 - $(convertsecs2ydhms $TIME4)
echo $TIME5 - $(convertsecs2ydhms $TIME5)
echo $TIME6 - $(convertsecs2ydhms $TIME6)

# OUTPUT WOULD BE LIKE THIS (If none pretty printf used): 
# 36 - 00:00-00:00:36
# 1036 - 00:00-00:17:16
# 91925 - 00:01-01:32:05
# 183850 - 00:02-03:04:10
# 3309300 - 00:38-07:15:00
# 1000000000 - 31:259-01:46:40
# OUTPUT WOULD BE LIKE THIS (If pretty printf used): 
# 36 - 00y 00d 00h 00m 36s
# 1036 - 00y 00d 00h 17m 16s
# 91925 - 00y 01d 01h 32m 05s
# 183850 - 00y 02d 03h 04m 10s
# 3309300 - 00y 38d 07h 15m 00s
# 1000000000 - 31y 259d 01h 46m 40s

AWK VERSION OF THE SAME SCRIPTS

#### AWK VERSION OF THE SAME TRICK ###

# there is definitely a better way to do this with a single awk command per function, but i like the way this looks better although it probably costs alot more resources then if say 1 awk was per function.

# TIP (can completely ignore this if you want) - This will go where you see "TIP CAN GO HERE"
# Since awk benefits from having decimals in its numbers, you can get the decimal part like so:
# decimalpart=`echo ${1} | awk '{x=$1; print x-int(x);}'`
# secwithdecimal=`echo ${s} ${decimalpart}| awk '{print ($1+$2);}'`
# now use $secwithdecimal instead of $s in the printf, and change the last %02d (on the very right to something different that shows 2 places to the left of the period for the integer value of the second, and the full decimal to the right of the period.)

convertsecs2hms() {
 # do the conversion
 h=`echo ${1} | awk '{x=$1; print int(x/3600);}'`
 m=`echo ${1} | awk '{x=$1; print int((x%3600)/60);}'`
 s=`echo ${1} | awk '{x=$1; print int(x%60);}'`
 ### TIP CAN GO HERE ###
 # output
 printf "%02d:%02d:%02d\n" $h $m $s
}

convertsecs2dhms() {
 # do the conversion
 d=`echo ${1} | awk '{x=$1; print int(x/(60*60*24));}}'`
 h=`echo ${1} | awk '{x=$1; print int((x%(60*60*24))/(60*60));}'`
 m=`echo ${1} | awk '{x=$1; print int((x%(60*60))/(60));}'`
 s=`echo ${1} | awk '{x=$1; print int(x%60);}'`
 ### TIP CAN GO HERE ###
 # output
 printf "%02d:%02d-%02d:%02d:%02d\n" $w $d $h $m $s
}

convertsecs2wdhms() {
 # do the conversion
 w=`echo ${1} | awk '{x=$1; print int(x/(60*60*24*7));}}'`
 d=`echo ${1} | awk '{x=$1; print int((x%(60*60*24*7))/(60*60*24));}'`
 h=`echo ${1} | awk '{x=$1; print int((x%(60*60*24))/(60*60));}'`
 m=`echo ${1} | awk '{x=$1; print int((x%(60*60))/(60));}'`
 s=`echo ${1} | awk '{x=$1; print int(x%60);}'`
 ### TIP CAN GO HERE ###
 # output
 printf "%02d:%02d-%02d:%02d:%02d\n" $w $d $h $m $s
}

convertsecs2wdhms() {
 # do the conversion
 w=`echo ${1} | awk '{x=$1; print int(x/(60*60*24*7));}}'`
 d=`echo ${1} | awk '{x=$1; print int((x%(60*60*24*7))/(60*60*24));}'`
 h=`echo ${1} | awk '{x=$1; print int((x%(60*60*24))/(60*60));}'`
 m=`echo ${1} | awk '{x=$1; print int((x%(60*60))/(60));}'`
 s=`echo ${1} | awk '{x=$1; print int(x%60);}'`
 ### TIP CAN GO HERE ###
 # output
 printf "%02d:%02d-%02d:%02d:%02d\n" $w $d $h $m $s
}

convertsecs2ywdhms() {
 # do the conversion
 y=`echo ${1} | awk '{x=$1; print int(x/(60*60*24*7*52));}'`
 w=`echo ${1} | awk '{x=$1; print int((x%(60*60*24*7*52))/(60*60*24*7));}'`
 d=`echo ${1} | awk '{x=$1; print int((x%(60*60*24*7))/(60*60*24));}'`
 h=`echo ${1} | awk '{x=$1; print int((x%(60*60*24))/(60*60));}'`
 m=`echo ${1} | awk '{x=$1; print int((x%(60*60))/(60));}'`
 s=`echo ${1} | awk '{x=$1; print int(x%60);}'`
 ### TIP CAN GO HERE ###
 # output
 printf "%02d:%02d:%02d-%02d:%02d:%02d\n" $y $w $d $h $m $s
}

convertsecs2ydhms() {
 # do the conversion
 y=`echo ${1} | awk '{x=$1; print int(x/(60*60*24*365));}'`
 d=`echo ${1} | awk '{x=$1; print int((x%(60*60*24*365))/(60*60*24)); }'`
 h=`echo ${1} | awk '{x=$1; print int((x%(60*60*24))/(60*60));}'`
 m=`echo ${1} | awk '{x=$1; print int((x%(60*60))/(60));}'`
 s=`echo ${1} | awk '{x=$1; print int(x%60);}'`
 ### TIP CAN GO HERE ###
 # output
 printf "%02d:%02d-%02d:%02d:%02d\n" $y $d $h $m $s
}

Leave a Reply

Your email address will not be published. Required fields are marked *