#/…/u2;/sex2hms==将秒格式化为

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#/…/u2;/sex2hms==将秒格式化为相关的知识,希望对你有一定的参考价值。

Bourne Shell Script (created on a FreeBSD_7.1-RELEASE system :-)
  1. #!/bin/sh
  2. ##############################################################################
  3. # /.../_/sex2hms == Format seconds into "xx week(s) xx day(s) hh:mm:ss" #
  4. #----------------------------------------------------------------------------#
  5. #.........william.o.yates...spamware.at.tru2life.net...tru2life.info.........#
  6. #----------------------------------------------------------------------------#
  7. # and YES, sex2hms coulda, maybe shoulda be called sex2wdhms, but oh well... #
  8. #----------------------------------------------------------------------------#
  9. # original (for bash): http://codesnippets.joyent.com/tag/bash/3#post1936 #
  10. # #
  11. # #!/bin/sh #
  12. # #Convert seconds to h:m:s format #
  13. # #
  14. # [ -z ${1} ] && echo "Usage: $(basename $0) <seconds>" && exit||secs=${1} #
  15. # _hms() #
  16. # { #
  17. # local S=${1} #
  18. # ((h=S/3600)) #
  19. # ((m=S%3600/60)) #
  20. # ((s=S%60)) #
  21. # printf "%dh:%dm:%ds " $h $m $s #
  22. # } #
  23. # #
  24. # _hms ${secs} #
  25. # #
  26. #----------------------------------------------------------------------------#
  27. # #
  28. # 1 second = 1 second = "S=$(expr ( ${SEX} % 60 ))" #
  29. # 60 seconds = 1 minute = "M=$(expr ( ${SEX} % 3600 / 60 ))" #
  30. # 3,600 seconds = 1 hour = "H=$(expr ( ${SEX} % 86400 / 3600 ))" #
  31. # 86,400 seconds = 1 day = "D=$(expr ( ${SEX} % 604800 / 86400 ))" #
  32. # 604,800 seconds = 1 week = "W=$(expr ( ${SEX} / 604800 ))" #
  33. # #
  34. # Following are beyond scope of this script, but jus4giggles... #
  35. # #
  36. # 2,419,200 seconds = 1 month28 = "[ ${SEX} -gt 2419200 ] && calc_month"... #
  37. # 2,505,600 seconds = 1 month29 = "[ ${SEX} -gt 2505600 ] && calc_month"... #
  38. # 2,592,000 seconds = 1 month30 = "[ ${SEX} -gt 2592000 ] && calc_month"... #
  39. # 2,678,400 seconds = 1 month31 = "[ ${SEX} -gt 2678400 ] && calc_month"... #
  40. # 31,536,000 seconds = 1 year365 = "[ ${SEX} -gt 31536000 ] && calc_year"... #
  41. # 31,622,400 seconds = 1 year366 = "[ ${SEX} -gt 31622400 ] && calc_year"... #
  42. # #
  43. #----------------------------------------------------------------------------#
  44. # #
  45. # a heavy duty hand calculation for what correct answers should be: #
  46. # #
  47. # 999,999 / 604,800 = 1.6534375 == ( W=1 ) #
  48. # #
  49. # 1 * 604,800 = 604,800 == ( 1 week in seconds )(doh... :-) #
  50. # #
  51. # 999,999 - 604,800 = 395,199 == ( SEX - 1W ) #
  52. # #
  53. # 395,199 / 86,400 = 4.5740625 == ( D=4 ) #
  54. # #
  55. # 4 * 86,400 = 345,600 == ( 4 days in seconds ) #
  56. # #
  57. # 395,199 - 345,600 = 49,599 == ( SEX - 1W - 4D ) #
  58. # #
  59. # 49,599 / 3,600 = 13.7775 == ( H=13 ) #
  60. # #
  61. # 13 * 3,600 = 46,800 == ( 13 hours in seconds ) #
  62. # #
  63. # 49,599 - 46,800 = 2,799 == ( SEX - 1W - 4D - 13H ) #
  64. # #
  65. # 2,799 / 60 = 46.65 == ( M=46 ) #
  66. # #
  67. # 46 * 60 = 2,760 == ( SEX - 1W - 4D - 13H - 46M ) #
  68. # #
  69. # 2,799 - 2,760 = 39 == ( S=39 ) #
  70. # #
  71. # So, (finally!) with the following sex2hms runs, the answers are: #
  72. # (with your directory prefixed from where you run it from) #
  73. # #
  74. # /.../_/sex2hms: 9 seconds = 00 week 00 day 00:00:09 #
  75. # /.../_/sex2hms: 99 seconds = 00 week 00 day 00:01:39 #
  76. # /.../_/sex2hms: 999 seconds = 00 week 00 day 00:16:39 #
  77. # /.../_/sex2hms: 9999 seconds = 00 week 00 day 02:46:39 #
  78. # /.../_/sex2hms: 99999 seconds = 00 week 01 day 03:46:39 #
  79. # /.../_/sex2hms: 999999 seconds = 01 week 04 days 13:46:39 #
  80. # /.../_/sex2hms: 9999999 seconds = 16 weeks 03 days 17:46:39 #
  81. # /.../_/sex2hms: 99999999 seconds = 165 weeks 02 days 09:46:39 #
  82. # /.../_/sex2hms: 999999999 seconds = 1653 weeks 03 days 01:46:39 #
  83. #----------------------------------------------------------------------------#
  84. #.........william.o.yates...spamware.at.tru2life.net...tru2life.info.........#
  85. #============================================================================#
  86.  
  87. #-----------------------------------------------------------------------------
  88. # hms() == convert seconds into hours:minutes:seconds (hh:mm:ss) format
  89. #-----------------------------------------------------------------------------
  90. hms()
  91. {
  92. local SEX=${1};
  93.  
  94. local HOUR=3600; local DAY=86400; local WEEK=604800;
  95.  
  96. local DAZE="day"; local WEKZ="week";
  97.  
  98. W=$(expr ( ${SEX} / ${WEEK} ))
  99.  
  100. D=$(expr ( ${SEX} % ${WEEK} / ${DAY} ))
  101.  
  102. H=$(expr ( ${SEX} % ${DAY} / ${HOUR} ))
  103.  
  104. M=$(expr ( ${SEX} % ${HOUR} / 60 ))
  105.  
  106. S=$(expr ( ${SEX} % 60 ))
  107.  
  108. [ ${D} -gt 1 ] && DAZE="days"
  109.  
  110. [ ${W} -gt 1 ] && WEKZ="weeks"
  111.  
  112. #===========================================================================
  113. # printf format [arguments ...]
  114. # '%s'=string, '%d'=decimal_number, '%02d'=0_padded_2_digit_decimal_number
  115. # character_escape_sequences: 'v'=vertical_tab, ' '=tab, ' '=newline
  116. #---------------------------------------------------------------------------
  117. #-------------------SEX-----------W--WEKZ--D--DAZE--H----M----S-------------
  118. printf "v ${0}: %d seconds = %02d %s %02d %s %02d:%02d:%02dv "
  119. ${SEX} ${W} ${WEKZ} ${D} ${DAZE} ${H} ${M} ${S}
  120.  
  121. echo -e "v SEX=${SEX}, W=${W}, WEKZ=${WEKZ}, D=${D}, DAZE=${DAZE},
  122. H=${H}, M=${M}, S=${S} v"
  123. }
  124.  
  125. #=============================================================================
  126. # [ -z STRING ] == True if the length of STRING is zero.
  127. #-----------------------------------------------------------------------------
  128. [ -z ${1} ]&&echo -e " Usage: $(basename ${0}) <seconds> "&&exit||hms ${1}
  129.  
  130. ##############################################################################

以上是关于#/…/u2;/sex2hms==将秒格式化为的主要内容,如果未能解决你的问题,请参考以下文章

js实现将秒数格式化为HH:MM:SS的形式

如何将秒转换为日期时间格式?

如何将秒转换为时间格式? [复制]

JAVA将秒的总和转换成时分秒的格式

将秒数转为HH:MM:SS格式的时间

js 将秒数转换为时分秒的格式