bash 将 HH:MM:SS (hours:minutes:seconds.split seconds) 转换为秒的简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2181712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:54:25  来源:igfitidea点击:

Simple way to convert HH:MM:SS (hours:minutes:seconds.split seconds) to seconds

linuxbashshelltime

提问by Mint

What's an easy way to convert 00:20:40.28(HH:MM:SS) to seconds with a Bash script?

00:20:40.28使用 Bash 脚本将(HH:MM:SS)转换为秒的简单方法是什么?

Split seconds can be cut out, it's not essential.

分秒可以剪掉,这不是必需的。

回答by dreamlax

Try awk. As a bonus, you can keep the split seconds.

试试awk。作为奖励,您可以保留分秒。

echo "00:20:40.25" | awk -F: '{ print ( * 3600) + ( * 60) +  }'

回答by Lri

This would work even if you don't specify hours or minutes: echo "04:20:40" | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc

即使您不指定小时或分钟,这也会起作用: echo "04:20:40" | sed -E 's/(.*):(.+):(.+)/\1*3600+\2*60+\3/;s/(.+):(.+)/\1*60+\2/' | bc

回答by NawaMan

Try this:

尝试这个:

T='00:20:40.28'
SavedIFS="$IFS"
IFS=":."
Time=($T)
Seconds=$((${Time[0]}*3600 + ${Time[1]}*60 + ${Time[2]})).${Time[3]}
IFS="$SavedIFS"

echo $Seconds

($<string>) splits <string>based on the splitter (IFS).

($ <string>)基于拆分器 ( )拆分<string>IFS

${<array>[<index>]} returns the element of the <array>at the <index>.

${ <array>[ <index>]} 返回<index><array>的元素。

$((<arithmetic expression>)) performs the arithmetic expression.

$(( <算术表达式>)) 执行算术表达式。

Hope this helps.

希望这可以帮助。

回答by kvz

If you are processing a time from ps, mind you that the format 2-18:01is also possible for 2 days, 19 hours, 1 minute. In that case you'll want to checkout: Parse ps' "etime" output and convert it into seconds

如果您正在处理从 开始的时间ps,请注意格式2-18:01也可以是 2 天 19 小时 1 分钟。在这种情况下,您需要结帐:解析 ps 的“etime”输出并将其转换为秒

回答by wgzhao

echo "40.25" | awk -F: '{ if (NF == 1) {print $NF} else if (NF == 2) {print  * 60 + } else if (NF==3) {print  * 3600 +  * 60 + } }'
40.25
echo "10:40.25" | awk -F: '{ if (NF == 1) {print $NF} else if (NF == 2) {print  * 60 + } else if (NF==3) {print  * 3600 +  * 60 + } }'
640.25
echo "20:10:40.25" | awk -F: '{ if (NF == 1) {print $NF} else if (NF == 2) {print  * 60 + } else if (NF==3) {print  * 3600 +  * 60 + } }'
72640.25

回答by mikevmk

If you don't know what exactly do you have - SS, MM:SS or HH:MM:SS, like after youtube-dl --get-duration, then awk magic could be useful:

如果你不知道你到底有什么 - SS、MM:SS 或 HH:MM:SS,比如 after youtube-dl --get-duration,那么 awk 魔法可能很有用:

echo 12 | awk -F\: '{ for(k=NF;k>0;k--) sum+=($k*(60^(NF-k))); print sum }'
12
echo 35:12 | awk -F\: '{ for(k=NF;k>0;k--) sum+=($k*(60^(NF-k))); print sum }'
2112
echo 1:35:12 | awk -F\: '{ for(k=NF;k>0;k--) sum+=($k*(60^(NF-k))); print sum }'
5712

回答by Aymeric Do

You can convert minutes to hour, seconds, or minutes with bccommand.

您可以使用bc命令将分钟转换为小时、秒或分钟。

By example:

举例:

How many minutes for 1000 sec ?

1000秒需要多少分钟?

$ echo 'obase=60;1000' | bc
02 00

then -> 2 min

然后 -> 2 分钟

How much hour for 375 min ?

375 分钟需要多少小时?

$ echo 'obase=60;375'| bc
06 15

then -> 06h15

然后 -> 06h15

How days for 56 hours?

56 小时是几天?

$ echo 'obase=24;56' | bc
02 08

then 02 days and 08 hours

然后是 02 天 08 小时

bc with obase is extra!

带 obase 的 bc 是额外的!

回答by Toby Speight

With GNU date, you can perform the conversion if the duration is less than 24 hours, by treating it as a time of day on the epoch:

使用 GNU 日期,如果持续时间少于 24 小时,您可以执行转换,方法是将其视为时代的一天中的时间:

to_seconds() {
    local epoch=$(date --utc -d @0 +%F)
    date --utc -d "$epoch " +%s.%09N
}

Running it with the example from the question:

使用问题中的示例运行它:

$ to_seconds 00:20:40.29
1240.290000000

Note that --utc, @, %sand %Nare all GNU extensions not necessarily supported by other implementations.

请注意--utc@%s%N都是其他实现不一定支持的 GNU 扩展。

回答by tzot

I have this old shell function (/bin/sh compatible in the sense of POSIX shell, not bash) which does this conversion in integer math (no fractions in the seconds):

我有这个旧的 shell 函数(/bin/sh 在 POSIX shell 的意义上兼容,而不是 bash)它在整数数学中进行这种转换(秒内没有分数):

tim2sec() {
    mult=1
    arg=""
    res=0
    while [ ${#arg} -gt 0 ]; do
        prev="${arg%:*}"
        if [ "$prev" = "$arg" ]; then
            curr="${arg#0}"  # avoid interpreting as octal
            prev=""
        else
            curr="${arg##*:}"
            curr="${curr#0}"  # avoid interpreting as octal
        fi
        curr="${curr%%.*}"  # remove any fractional parts
        res=$((res+curr*mult))
        mult=$((mult*60))
        arg="$prev"
    done
    echo "$res"
}

Outputs:

输出:

$ tim2sec 1:23:45.243
5025

It works with SS, MM:SS and HH:MM:SS only :)

它仅适用于 SS、MM:SS 和 HH:MM:SS :)

回答by netricate

I haven't tested this but, I think this is how you'd split the string. Followed by multiplying by the appropriate amounts for hours and minutes.

我还没有测试过这个,但是我认为这就是你分割字符串的方式。然后乘以适当的小时和分钟数。

mytime='00:20:40.28′
part1=${mytime%%:*}; rest=${mytime#*:}
part2=${rest%%:*}; rest=${rest#*:}
part3=${rest%%:*};