如何将秒转换为小时、分钟和秒?
show_time() {
?????
}
show_time 36 # 00:00:36
show_time 1036 # 00:17:26
show_time 91925 # 25:32:05
如何将秒转换为小时、分钟和秒?
show_time() {
?????
}
show_time 36 # 00:00:36
show_time 1036 # 00:17:26
show_time 91925 # 25:32:05
使用日期,转换为 UTC:
$ date -d@36 -u +%H:%M:%S
00:00:36
$ date -d@1036 -u +%H:%M:%S
00:17:16
$ date -d@12345 -u +%H:%M:%S
03:25:45
限制是小时将在 23 处循环,但这对于您想要单线的大多数用例来说并不重要。
在 macOS 上,运行brew install coreutils
并替换date
为gdate
#!/bin/sh
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)
对于浮点秒:
convertsecs() {
h=$(bc <<< "${1}/3600")
m=$(bc <<< "(${1}%3600)/60")
s=$(bc <<< "${1}%60")
printf "%02d:%02d:%05.2f\n" $h $m $s
}
我知道的最简单的方法:
secs=100000
printf '%dh:%dm:%ds\n' $((secs/3600)) $((secs%3600/60)) $((secs%60))
注意 - 如果您想要天数,只需添加其他单位并除以 86400。
$ secs=236521
$ printf '%dh:%dm:%ds\n' $((secs/3600)) $((secs%3600/60)) $((secs%60))
65h:42m:1s
$ secs=236521
$ printf '%02dh:%02dm:%02ds\n' $((secs/3600)) $((secs%3600/60)) $((secs%60))
65h:42m:01s
$ secs=236521
$ printf '%dd:%dh:%dm:%ds\n' $((secs/86400)) $((secs%86400/3600)) $((secs%3600/60)) \
$((secs%60))
2d:17h:42m:1s
$ secs=21218.6474912
$ printf '%02dh:%02dm:%02fs\n' $(echo -e "$secs/3600\n$secs%3600/60\n$secs%60"| bc)
05h:53m:38.647491s
我自己使用以下功能:
function show_time () {
num=$1
min=0
hour=0
day=0
if((num>59));then
((sec=num%60))
((num=num/60))
if((num>59));then
((min=num%60))
((num=num/60))
if((num>23));then
((hour=num%24))
((day=num/24))
else
((hour=num))
fi
else
((min=num))
fi
else
((sec=num))
fi
echo "$day"d "$hour"h "$min"m "$sec"s
}
请注意,它也计算天数。此外,它会为您的最后一个号码显示不同的结果。
对于我们懒惰的人:现成的脚本可在https://github.com/k0smik0/FaCRI/blob/master/fbcmd/bin/displaytime获得:
#!/bin/bash
function displaytime {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
[[ $D > 0 ]] && printf '%d days ' $D
[[ $H > 0 ]] && printf '%d hours ' $H
[[ $M > 0 ]] && printf '%d minutes ' $M
[[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and '
printf '%d seconds\n' $S
}
displaytime $1
基本上只是其他解决方案的另一种旋转,但具有抑制空时间单位(fe10 seconds
而不是0 hours 0 minutes 10 seconds
)的额外好处。无法完全追踪函数的原始来源,发生在多个 git repos 中。
使用dc
:
$ echo '12345.678' | dc -e '?1~r60~r60~r[[0]P]szn[:]ndZ2>zn[:]ndZ2>zn[[.]n]sad0=ap'
3:25:45.678
该表达式?1~r60~r60~rn[:]nn[:]nn[[.]n]sad0=ap
执行以下操作:
? read a line from stdin
1 push one
~ pop two values, divide, push the quotient followed by the remainder
r reverse the top two values on the stack
60 push sixty
~ pop two values, divide, push the quotient followed by the remainder
r reverse the top two values on the stack
60 push sixty
~ pop two values, divide, push the quotient followed by the remainder
r reverse the top two values on the stack
[ interpret everything until the closing ] as a string
[0] push the literal string '0' to the stack
n pop the top value from the stack and print it with no newline
] end of string, push the whole thing to the stack
sz pop the top value (the string above) and store it in register z
n pop the top value from the stack and print it with no newline
[:] push the literal string ':' to the stack
n pop the top value from the stack and print it with no newline
d duplicate the top value on the stack
Z pop the top value from the stack and push the number of digits it has
2 push two
>z pop the top two values and executes register z if the original top-of-stack is greater
n pop the top value from the stack and print it with no newline
[:] push the literal string ':' to the stack
n pop the top value from the stack and print it with no newline
d duplicate the top value on the stack
Z pop the top value from the stack and push the number of digits it has
2 push two
>z pop the top two values and executes register z if the original top-of-stack is greater
n pop the top value from the stack and print it with no newline
[ interpret everything until the closing ] as a string
[.] push the literal string '.' to the stack
n pop the top value from the stack and print it with no newline
] end of string, push the whole thing to the stack
sa pop the top value (the string above) and store it in register a
d duplicate the top value on the stack
0 push zero
=a pop two values and execute register a if they are equal
p pop the top value and print it with a newline
每次操作后使用堆栈状态执行的示例:
: <empty stack>
? : 12345.678
1 : 1, 12345.678
~ : .678, 12345
r : 12345, .678 # stack is now seconds, fractional seconds
60 : 60, 12345, .678
~ : 45, 205, .678
r : 205, 45, .678 # stack is now minutes, seconds, fractional seconds
60 : 60, 205, 45, .678
~ : 25, 3, 45, .678
r : 3, 25, 45, .678 # stack is now hours, minutes, seconds, fractional seconds
[[0]n] : [0]n, 3, 25, 45, .678
sz : 3, 25, 45, .678 # '[0]n' stored in register z
n : 25, 45, .678 # accumulated stdout: '3'
[:] : :, 25, 45, .678
n : 25, 45, .678 # accumulated stdout: '3:'
d : 25, 25, 45, .678
Z : 2, 25, 45, .678
2 : 2, 2, 25, 45, .678
>z : 25, 45, .678 # not greater, so register z is not executed
n : 45, .678 # accumulated stdout: '3:25'
[:] : :, 45, .678
n : 45, .678 # accumulated stdout: '3:25:'
d : 45, 45, .678
Z : 2, 45, 45, .678
2 : 2, 2, 45, .678
>z : 45, .678 # not greater, so register z is not executed
n : .678 # accumulated stdout: '3:25:45'
[[.]n] : [.]n, .678
sa : .678 # '[.]n' stored to register a
d : .678, .678
0 : 0, .678, .678
=a : .678 # not equal, so register a not executed
p : <empty stack> # accumulated stdout: '3:25:45.678\n'
在 0 小数秒的情况下:
: 3, 25, 45, 0 # starting just before we begin to print
n : 25, 45, .678 # accumulated stdout: '3'
[:] : :, 25, 45, .678
n : 25, 45, .678 # accumulated stdout: '3:'
d : 25, 25, 45, .678
Z : 2, 25, 45, .678
2 : 2, 2, 25, 45, .678
>z : 25, 45, .678 # not greater, so register z is not executed
n : 45, .678 # accumulated stdout: '3:25'
[:] : :, 45, .678
n : 45, .678 # accumulated stdout: '3:25:'
d : 45, 45, .678
Z : 2, 45, 45, .678
2 : 2, 2, 45, .678
>z : 45, .678 # not greater, so register z is not executed
n : .678 # accumulated stdout: '3:25:45'
[[.]n] : [.]n, 0
sa : 0 # '[.]n' stored to register a
d : 0, 0
0 : 0, 0, 0
=a : 0 # equal, so register a executed
[.] : ., 0
n : 0 # accumulated stdout: '3:35:45.'
p : <empty stack> # accumulated stdout: '3:25:45.0\n'
如果分钟值小于 10:
: 3, 9, 45, 0 # starting just before we begin to print
n : 9, 45, .678 # accumulated stdout: '3'
[:] : :, 9, 45, .678
n : 9, 45, .678 # accumulated stdout: '3:'
d : 9, 9, 45, .678
Z : 1, 9, 45, .678
2 : 2, 1, 9, 45, .678
>z : 9, 45, .678 # greater, so register z is executed
[0] : 0, 9, 45, .678
n : 9, 45, .678 # accumulated stdout: '3:0'
n : 9, .678 # accumulated stdout: '3:09'
# ...and continues as above
编辑:这有一个错误,可以打印像 7:7:34.123 这样的字符串。如有必要,我已对其进行了修改以打印前导零。
编辑 2:我意识到小数秒的处理是多余的,这个简化dc
的表达式执行几乎相同的工作:
$ echo '12345.678' | dc -e '?60~r60~r[[0]P]szn[:]ndZ2>zn[:]ndZ2>zp'
3:25:45.678
$ echo '12345.0' | dc -e '?60~r60~r[[0]P]szn[:]ndZ2>zn[:]ndZ2>zp'
3:25:45.0
$ echo '12345' | dc -e '?60~r60~r[[0]P]szn[:]ndZ2>zn[:]ndZ2>zp'
3:25:45
这样做的缺点是不同但相同的输入会产生不同的输出;由于 的定点性质,小数点后的任意数量的零都将在输出中逐字回显dc
。
以上所有内容都是针对 bash 的,忽略没有任何 bashism 的 "#!/bin/sh" 将是:
convertsecs() {
h=`expr $1 / 3600`
m=`expr $1 % 3600 / 60`
s=`expr $1 % 60`
printf "%02d:%02d:%02d\n" $h $m $s
}
t=12345;printf %02d:%02d:%02d\\n $((t/3600)) $((t%3600/60)) $((t%60)) # POSIX
echo 12345|awk '{printf "%02d:%02d:%02d",$0/3600,$0%3600/60,$0%60}' # POSIX awk
date -d @12345 +%T # GNU date
date -r 12345 +%T # OS X's date
如果其他人正在寻找如何做相反的事情:
IFS=: read h m s<<<03:25:45;echo $((h*3600+m*60+s)) # POSIX
echo 03:25:45|awk -F: '{print 3600*$1+60*$2+$3}' # POSIX awk
使用 OOTB/bin/date
且不需要GNU 版本的MacOS 特定答案date
:
# convert 195 seconds to MM:SS format, i.e. 03:15
/bin/date -ju -f "%s" 195 "+%M:%S"
## OUTPUT: 03:15
如果你也想有几个小时:
/bin/date -ju -f "%s" 3600 "+%H:%M:%S"
# OUTPUT: 01:00:00
注意:如果您想处理小时数,则
-u
需要它,因为它会强制 UTC 时间,如果没有它,除非您居住在 UTC 时区,否则您将得到错误的输出:
-u Display or set the date in UTC (Coordinated Universal) time.
有关为什么-u
需要的解释,请参见this。
在一行中:
show_time () {
if [ $1 -lt 86400 ]; then
date -d@${1} -u '+%Hh:%Mmn:%Ss';
else
echo "$(($1/86400)) days $(date -d@$(($1%86400)) -u '+%Hh:%Mmn:%Ss')" ;
fi
}
如果存在,请添加天数。
我无法让 Vaulter/chepner 的代码正常工作。我认为正确的代码是:
convertsecs() {
h=$(($1/3600))
m=$((($1/60)%60))
s=$(($1%60))
printf "02d:%02d:%02d\n $h $m $s
}
这是旧帖子 ovbioius - 但是,对于那些可能正在寻找实际经过的时间但以军事格式(00:05:15:22 - 而不是 0:5:15:22 )的人
!#/bin/bash
num=$1
min=0
hour=0
day=0
if((num>59));then
((sec=num%60))
((num=num/60))
if((num>59));then
((min=num%60))
((num=num/60))
if((num>23));then
((hour=num%24))
((day=num/24))
else
((hour=num))
fi
else
((min=num))
fi
else
((sec=num))
fi
day=`seq -w 00 $day | tail -n 1`
hour=`seq -w 00 $hour | tail -n 1`
min=`seq -w 00 $min | tail -n 1`
sec=`seq -w 00 $sec | tail -n 1`
printf "$day:$hour:$min:$sec"
exit 0
在MacOSX 10.13上对 @eMPee584 的代码进行轻微编辑,以便一次完成所有操作(将函数放在类似 .bashrc 的文件中并将其作为 myuptime 使用。对于非 Mac OS,将 T 公式替换为给出自上次启动以来的秒数。
myuptime ()
{
local T=$(($(date +%s)-$(sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g')));
local D=$((T/60/60/24));
local H=$((T/60/60%24));
local M=$((T/60%60));
local S=$((T%60));
printf '%s' "UpTime: ";
[[ $D > 0 ]] && printf '%d days ' $D;
[[ $H > 0 ]] && printf '%d hours ' $H;
[[ $M > 0 ]] && printf '%d minutes ' $M;
[[ $D > 0 || $H > 0 || $M > 0 ]] && printf '%d seconds\n' $S
}
又一个版本。仅处理完整整数,不填充0
:
format_seconds() {
local sec tot r
sec="$1"
r="$((sec%60))s"
tot=$((sec%60))
if [[ "$sec" -gt "$tot" ]]; then
r="$((sec%3600/60))m:$r"
let tot+=$((sec%3600))
fi
if [[ "$sec" -gt "$tot" ]]; then
r="$((sec%86400/3600))h:$r"
let tot+=$((sec%86400))
fi
if [[ "$sec" -gt "$tot" ]]; then
r="$((sec/86400))d:$r"
fi
echo "$r"
}
$ format_seconds 59
59s
$ format_seconds 60
1m:0s
$ format_seconds 61
1m:1s
$ format_seconds 3600
1h:0m:0s
$ format_seconds 236521
2d:17h:42m:1s
直接通过awk
:
echo $(seconds) | awk '{printf "%d:%02d:%02d", $1/3600, ($1/60)%60, $1%60}'