2012-11-16 73 views

回答

60

這裏是做你正在尋找什麼=)一個有趣的哈克的方式

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

說明:

  • 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部件,所以適合!
+2

快速而令人印象深刻! –

+1

請看下面的Alan Tam的答案,瞭解它如何在Mac/Unix中工作:http://stackoverflow.com/a/21822510/243709 –

+5

我認爲值得一提的是這隻適用於低於86400s的'i':我= 86400; date -u -d @ $ {i} +「%T」'會給你'00:00:00' – galaux

0

如果$i表示,因爲大紀元在第二某個日期,你可以用

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

顯示,但你似乎假設$i是時間間隔(例如,一些持續時間)不是一個日期,然後我不不明白你想要什麼。

+0

與@ sampson-chen相同(重複)+%T。 –

14

的另一種方法:算術

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

+1

可能不是OP之後的內容,但正是我所需要的。謝謝。 –

10

-d觀點也適用於從日期僅coreutils(Linux)的。

在BSD/OS X,使用

date -u -r $i +%T 
+0

不適用於AIX :( – zack

1

這是我在我的網站算法中/腳本助手: http://ram.kossboss.com/seconds-to-split-time-convert/ 我曾經從這裏這個elogant算法中:Convert seconds to hours, minutes, seconds

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 
0

我用C殼,就像這樣:

#! /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)" 
0

繼續@ Daren`s答案,僅僅是明確的: 如果你想使用轉換爲時區不使用「u」開關,如:date -d @$i +%T或在某些情況下date -d @"$i" +%T

相關問題