Linux 将时间跨度(以秒为单位)转换为 shell 中的格式化时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13422743/
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
Convert a time span in seconds to formatted time in shell
提问by Darren
I have a variable of $i which is seconds in a shell script, and I am trying to convert it to 24 HOUR HH:MM:SS. Is this possible in shell?
我有一个 $i 变量,它是 shell 脚本中的秒数,我试图将它转换为 24 小时 HH:MM:SS。这在shell中可能吗?
采纳答案by sampson-chen
Here's a fun hacky way to do exactly what you are looking for =)
这是一个有趣的 hacky 方式来做你正在寻找的东西 =)
date -u -d @${i} +"%T"
Explanation:
解释:
- The
date
utility allows you to specify a time, from string, in seconds since 1970-01-01 00:00:00 UTC, and output it in whatever format you specify. - The
-u
option is to display UTC time, so it doesn't factor in timezone offsets (since start time from 1970 is in UTC) - The following parts are GNU
date
-specific (Linux):- The
-d
part tellsdate
to accept the time information from string instead of usingnow
- The
@${i}
part is how you telldate
that$i
is in seconds
- The
- The
+"%T"
is for formatting your output. From theman date
page:%T time; same as %H:%M:%S
. Since we only care about theHH:MM:SS
part, this fits!
- 该
date
实用程序允许您从字符串中指定自 1970-01-01 00:00:00 UTC 以来的秒数,并以您指定的任何格式输出。 - 该
-u
选项是显示 UTC 时间,因此它不考虑时区偏移(因为 1970 年的开始时间是 UTC) - 以下部分是 GNU
date
特定的 (Linux):- 该
-d
部分告诉date
接受来自字符串的时间信息而不是使用now
- 该
@${i}
部分是你如何告诉date
那$i
是在几秒钟内
- 该
- 该
+"%T"
是格式化你的输出。从man date
页面:%T time; same as %H:%M:%S
。由于我们只关心HH:MM:SS
零件,所以这很合适!
回答by Basile Starynkevitch
If $i
represents some date in second since the Epoch, you could display it with
如果$i
代表自纪元以来的某个日期,您可以使用
date -u -d @$i +%H:%M:%S
but you seems to suppose that $i
is an interval (e.g. some duration) not a date, and then I don't understand what you want.
但您似乎认为这$i
是一个间隔(例如某个持续时间)而不是日期,然后我不明白您想要什么。
回答by glenn Hymanman
Another approach: arithmetic
另一种方法:算术
i=6789
((sec=i%60, i/=60, min=i%60, hrs=i/60))
timestamp=$(printf "%d:%02d:%02d" $hrs $min $sec)
echo $timestamp
produces 1:53:09
产生 1:53:09
回答by Alan Tam
The -d
argument applies to date from coreutils
(Linux) only.
该-d
参数仅适用于coreutils
(Linux) 的日期。
In BSD/OS X, use
在 BSD/OS X 中,使用
date -u -r $i +%T
回答by kossboss
Here is my algo/script helpers on my site: http://ram.kossboss.com/seconds-to-split-time-convert/I used this elogant algo from here: Convert seconds to hours, minutes, seconds
这是我网站上的算法/脚本助手: http://ram.kossboss.com/seconds-to-split-time-convert/ 我从这里使用了这个优雅的算法:将秒转换为小时、分钟、秒
convertsecs() {
((h=/3600))
((m=(%3600)/60))
((s=%60))
printf "%02d:%02d:%02d\n" $h $m $s
}
TIME1="36"
TIME2="1036"
TIME3="91925"
echo $(convertsecs $TIME1)
echo $(convertsecs $TIME2)
echo $(convertsecs $TIME3)
Example of my second to day, hour, minute, second converter:
我的秒到天、小时、分钟、秒转换器的示例:
# convert seconds to day-hour:min:sec
convertsecs2dhms() {
((d=/(60*60*24)))
((h=(%(60*60*24))/(60*60)))
((m=(%(60*60))/60))
((s=%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
回答by Shilv
I use C shell, like this:
我使用 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)"
回答by Shai Alon
Continuing @Daren`s answer, just to be clear:
If you want to use the conversion to your time zone, don't use the "u" switch, as in: date -d @$i +%T
or in some cases date -d @"$i" +%T
.
继续@Daren 的回答,只是要清楚:如果您想使用时区的转换,请不要使用“u”开关,例如:date -d @$i +%T
或在某些情况下date -d @"$i" +%T
。