#/…/u2;/sex2hms==将秒格式化为
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#/…/u2;/sex2hms==将秒格式化为相关的知识,希望对你有一定的参考价值。
Bourne Shell Script (created on a FreeBSD_7.1-RELEASE system :-)
#!/bin/sh ############################################################################## # /.../_/sex2hms == Format seconds into "xx week(s) xx day(s) hh:mm:ss" # #----------------------------------------------------------------------------# #.........william.o.yates...spamware.at.tru2life.net...tru2life.info.........# #----------------------------------------------------------------------------# # and YES, sex2hms coulda, maybe shoulda be called sex2wdhms, but oh well... # #----------------------------------------------------------------------------# # original (for bash): http://codesnippets.joyent.com/tag/bash/3#post1936 # # # # #!/bin/sh # # #Convert seconds to h:m:s format # # # # [ -z ${1} ] && echo "Usage: $(basename $0) <seconds>" && exit||secs=${1} # # _hms() # # { # # local S=${1} # # ((h=S/3600)) # # ((m=S%3600/60)) # # ((s=S%60)) # # printf "%dh:%dm:%ds " $h $m $s # # } # # # # _hms ${secs} # # # #----------------------------------------------------------------------------# # # # 1 second = 1 second = "S=$(expr ( ${SEX} % 60 ))" # # 60 seconds = 1 minute = "M=$(expr ( ${SEX} % 3600 / 60 ))" # # 3,600 seconds = 1 hour = "H=$(expr ( ${SEX} % 86400 / 3600 ))" # # 86,400 seconds = 1 day = "D=$(expr ( ${SEX} % 604800 / 86400 ))" # # 604,800 seconds = 1 week = "W=$(expr ( ${SEX} / 604800 ))" # # # # Following are beyond scope of this script, but jus4giggles... # # # # 2,419,200 seconds = 1 month28 = "[ ${SEX} -gt 2419200 ] && calc_month"... # # 2,505,600 seconds = 1 month29 = "[ ${SEX} -gt 2505600 ] && calc_month"... # # 2,592,000 seconds = 1 month30 = "[ ${SEX} -gt 2592000 ] && calc_month"... # # 2,678,400 seconds = 1 month31 = "[ ${SEX} -gt 2678400 ] && calc_month"... # # 31,536,000 seconds = 1 year365 = "[ ${SEX} -gt 31536000 ] && calc_year"... # # 31,622,400 seconds = 1 year366 = "[ ${SEX} -gt 31622400 ] && calc_year"... # # # #----------------------------------------------------------------------------# # # # a heavy duty hand calculation for what correct answers should be: # # # # 999,999 / 604,800 = 1.6534375 == ( W=1 ) # # # # 1 * 604,800 = 604,800 == ( 1 week in seconds )(doh... :-) # # # # 999,999 - 604,800 = 395,199 == ( SEX - 1W ) # # # # 395,199 / 86,400 = 4.5740625 == ( D=4 ) # # # # 4 * 86,400 = 345,600 == ( 4 days in seconds ) # # # # 395,199 - 345,600 = 49,599 == ( SEX - 1W - 4D ) # # # # 49,599 / 3,600 = 13.7775 == ( H=13 ) # # # # 13 * 3,600 = 46,800 == ( 13 hours in seconds ) # # # # 49,599 - 46,800 = 2,799 == ( SEX - 1W - 4D - 13H ) # # # # 2,799 / 60 = 46.65 == ( M=46 ) # # # # 46 * 60 = 2,760 == ( SEX - 1W - 4D - 13H - 46M ) # # # # 2,799 - 2,760 = 39 == ( S=39 ) # # # # So, (finally!) with the following sex2hms runs, the answers are: # # (with your directory prefixed from where you run it from) # # # # /.../_/sex2hms: 9 seconds = 00 week 00 day 00:00:09 # # /.../_/sex2hms: 99 seconds = 00 week 00 day 00:01:39 # # /.../_/sex2hms: 999 seconds = 00 week 00 day 00:16:39 # # /.../_/sex2hms: 9999 seconds = 00 week 00 day 02:46:39 # # /.../_/sex2hms: 99999 seconds = 00 week 01 day 03:46:39 # # /.../_/sex2hms: 999999 seconds = 01 week 04 days 13:46:39 # # /.../_/sex2hms: 9999999 seconds = 16 weeks 03 days 17:46:39 # # /.../_/sex2hms: 99999999 seconds = 165 weeks 02 days 09:46:39 # # /.../_/sex2hms: 999999999 seconds = 1653 weeks 03 days 01:46:39 # #----------------------------------------------------------------------------# #.........william.o.yates...spamware.at.tru2life.net...tru2life.info.........# #============================================================================# #----------------------------------------------------------------------------- # hms() == convert seconds into hours:minutes:seconds (hh:mm:ss) format #----------------------------------------------------------------------------- hms() { local SEX=${1}; local HOUR=3600; local DAY=86400; local WEEK=604800; local DAZE="day"; local WEKZ="week"; W=$(expr ( ${SEX} / ${WEEK} )) D=$(expr ( ${SEX} % ${WEEK} / ${DAY} )) H=$(expr ( ${SEX} % ${DAY} / ${HOUR} )) M=$(expr ( ${SEX} % ${HOUR} / 60 )) S=$(expr ( ${SEX} % 60 )) [ ${D} -gt 1 ] && DAZE="days" [ ${W} -gt 1 ] && WEKZ="weeks" #=========================================================================== # printf format [arguments ...] # '%s'=string, '%d'=decimal_number, '%02d'=0_padded_2_digit_decimal_number # character_escape_sequences: 'v'=vertical_tab, ' '=tab, ' '=newline #--------------------------------------------------------------------------- #-------------------SEX-----------W--WEKZ--D--DAZE--H----M----S------------- printf "v ${0}: %d seconds = %02d %s %02d %s %02d:%02d:%02dv " ${SEX} ${W} ${WEKZ} ${D} ${DAZE} ${H} ${M} ${S} echo -e "v SEX=${SEX}, W=${W}, WEKZ=${WEKZ}, D=${D}, DAZE=${DAZE}, H=${H}, M=${M}, S=${S} v" } #============================================================================= # [ -z STRING ] == True if the length of STRING is zero. #----------------------------------------------------------------------------- [ -z ${1} ]&&echo -e " Usage: $(basename ${0}) <seconds> "&&exit||hms ${1} ##############################################################################
以上是关于#/…/u2;/sex2hms==将秒格式化为的主要内容,如果未能解决你的问题,请参考以下文章