36

我有一个 $i 变量,它在 shell 脚本中是秒,我正在尝试将其转换为 24 HOUR HH:MM:SS。这可能在shell中吗?

4

7 回答 7

87

这是一个有趣的hacky方式来做你正在寻找的东西=)

date -u -d @${i} +"%T"

解释:

  • date实用程序允许您从字符串指定时间,以 1970-01-01 00:00:00 UTC 以来的秒数为单位,并以您指定的任何格式输出。
  • 选项是显示-uUTC 时间,因此它不考虑时区偏移(因为 1970 年的开始时间是 UTC)
  • 以下部分是 GNU特定的date(Linux):
    • -d部分告诉date接受来自字符串的时间信息,而不是使用now
    • @${i}部分是你如何告诉date$i是在几秒钟内
  • +"%T"用于格式化输出。从man date页面:%T time; same as %H:%M:%S。因为我们只关心HH:MM:SS零件,所以这很合适!
于 2012-11-16T19:17:51.123 回答
18

另一种方法:算术

i=6789
((sec=i%60, i/=60, min=i%60, hrs=i/60))
timestamp=$(printf "%d:%02d:%02d" $hrs $min $sec)
echo $timestamp

生产1:53:09

于 2012-11-16T23:16:08.967 回答
15

-d论点仅适用于coreutils(Linux) 的日期。

在 BSD/OS X 中,使用

date -u -r $i +%T
于 2014-02-17T06:48:21.073 回答
1

如果$i表示自纪元以来第二个日期,您可以使用

  date -u -d @$i +%H:%M:%S

但是您似乎认为这$i是一个间隔(例如某个持续时间)而不是日期,然后我不明白您想要什么。

于 2012-11-16T19:17:47.090 回答
1

这是我网站上的算法/脚本助手: http: //ram.kossboss.com/seconds-to-split-time-convert/ 我从这里使用了这个优雅的算法:将秒转换为小时、分钟、秒

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)

我的秒到日、时、分、秒转换器的示例:

# 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
于 2015-01-07T13:43:36.123 回答
0

我使用 C shell,如下所示:

#! /bin/csh -f

set begDate_r = `date +%s`
set endDate_r = `date +%s`

set secs = `echo "$endDate_r - $begDate_r" | bc`
set h = `echo $secs/3600 | bc`
set m = `echo "$secs/60 - 60*$h" | bc`
set s = `echo $secs%60 | bc`

echo "Formatted Time: $h HOUR(s) - $m MIN(s) - $s SEC(s)"
于 2016-11-24T09:18:57.663 回答
0

继续@Daren 的回答,只是为了清楚一点:如果您想使用转换为您的时区请不要使用“u”开关,如:date -d @$i +%T或在某些情况下date -d @"$i" +%T

于 2018-02-13T15:02:25.187 回答