Converting seconds to time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting seconds to time
# 1  
Old 02-23-2018
Converting seconds to time

I have a list of time spans in seconds, and want to compute the time span
as hh:mm:nn

I am coding in bash and have coded the following. However, the results are
wrong as "%.0f" rounds the values.

Example:

Code:
ftm: 25793.5
tmspan(hrs,min,sec): 7.16  429.89  25793.50

hh: 7
mm: 10
ss: -0
tmspan 7:10:1193.50


Code:
  tmspan_sec=`echo "($npt - 1) * $dlt" | bc -l`
  tmspan_min=`awk -v tm=$tmspan_sec 'BEGIN {printf "%.2f", tm/60.0}'`
  tmspan_hrs=`awk -v tm=$tmspan_sec 'BEGIN {printf "%.2f", tm/(60.0*60.0)}'`
  echo "tmspan(hrs,min,sec): $tmspan_hrs  $tmspan_min  $tmspan_sec"

  hh=`awk -v tm=$tmspan_sec 'BEGIN {printf "%.0f", tm / (60.0 * 60.0)}'`
  echo "hh: $hh" 
  mm=`awk -v tm=$tmspan_sec -v thh=$hh \
    'BEGIN {printf "%.0f", (tm-(thh*60.0*60.0))/60.0}'`
  echo "mm: $mm" 
  ss=`awk -v tm=$tmspan_sec -v thh=$hh -v tmm=$mm  \
    'BEGIN {printf "%.0f", (tm-(thh*60.0*60.0)-(tmm*60.0))/60.0}'`
  echo "ss: $ss"

# 2  
Old 02-23-2018
Why not
Code:
printf "%02d:%02d:%02d\n" $((25793 / 3600)) $((25793 % 3600 / 60 )) $((25793 % 3600 % 60))
07:09:53

Please be aware that bashcan do integer calculations only! If you need fractions of seconds, try
Code:
printf "%02d:%02d:%02d%s\n" $((${ftm%.*} / 3600)) $((${ftm%.*} % 3600 / 60 )) $((${ftm%.*} % 3600 % 60)) ${ftm#${ftm%.*}}
07:09:53.5


Last edited by RudiC; 02-23-2018 at 08:58 AM..
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 02-23-2018
I have now stored the initial and final time as 24-hour hours, minutes and seconds in
variables `itm` and `ftm` respictively.

My files look as follows and want to introduce `itm` and `ftm` in the file name. I want to
introduce `itm` and `ftm` instead of the number before `.sac` to get something like

./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.itm.ftm.sac

Is there any good way to do this?


Code:
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132909.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132909.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132934.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.132934.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.13290945.sac
./iv-hhz-sac/hpac/hhz.d/iv.hpac..hhz.d.2016.215.1329095.sac

---------- Post updated at 10:15 AM ---------- Previous update was at 10:06 AM ----------

The following does not work well

Code:
echo "$stfl" | awk -F '.' '{for(i=1;i<=(NF-2);i++) print $i}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting seconds to minutes

Hello everyone, I calculate in my bash script the different between two timestamps in seconds. The next step would be to get the difference in minutes, and there is my Problem:AnzahlUeberstunden=$(( $(date -d "$JAHR-$MONAT-$TAG $ArbeitEnde" +%s) - $(date -d "$JAHR-$MONAT-$TAG... (6 Replies)
Discussion started by: Chaos_Lord
6 Replies

2. Shell Programming and Scripting

Converting time format as Integer to seconds

Hello, How can we convert date like format 20181004171050 in seconds ? I can able to convert till date but failing for HHMMSS. date -d "20181004" "+%s" output as 1538596800 . But when i add hhmmss it is failing date -d "20181004172000" "+%s" result Invalid date Kindly guide. Regards (16 Replies)
Discussion started by: sadique.manzar
16 Replies

3. Shell Programming and Scripting

Time in seconds on AIX 4.3.2.0

Hi to everybody again i Need your help, i wasting hours but can't find a solutuin for my Problem. I am not an expert with AIX script programming. I have a csh script and i need the time in seconds but since i have an old AIX the Option -%s doesnot exist with the date command. I seach in Google... (13 Replies)
Discussion started by: Nadielosabra
13 Replies

4. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

5. Shell Programming and Scripting

Elapsed time in seconds in awk

I am trying to get the ellapsed time in seconds in the body of the awk script. I use unix date to get the time. It works in BEGIN {} but not in the body {} of awk. Any ideas? $ cat a BEGIN { "date +%s" | getline x print x } { "date +%s" | getline y print y } $ echo "one line" |... (3 Replies)
Discussion started by: arturas123
3 Replies

6. Shell Programming and Scripting

Time difference in seconds

date1=$(date +"%H:%M:%S") date2=$(date +"01:00:54") diff=$date2-$date1 echo $diff How to get the time difference in seconds. (4 Replies)
Discussion started by: sandy1028
4 Replies

7. Shell Programming and Scripting

Converting past date to seconds

Need a little help developing a ksh script. Have read through Perderabo's datecalc routine and it does not seem to fit the function I am looking for. Basically what I am trying to do is find any file (in a specific directory) that was created within the last five minutes. I am not a programming... (3 Replies)
Discussion started by: synergy_texas
3 Replies

8. UNIX for Advanced & Expert Users

Time Difference in seconds

It is required to calculate time difference in seconds between epoch time (19700101 00:00:00) and any given date time (e.g. 20010214 14:30:30). Is there any command in unix to get it? Thanks in adv. (1 Reply)
Discussion started by: k_bijitesh
1 Replies

9. Shell Programming and Scripting

how to display time in minutes n seconds...

Hi all, may i know how to display time in minutes and seconds(may be milliseconds and even smaller that ) in shell scripts.... (1 Reply)
Discussion started by: santy
1 Replies

10. UNIX for Dummies Questions & Answers

getting time in mili seconds

hi all UNIX Gurus, this is my first post...so i posting this with great expectations:o...hoping to get the similar replies... my question is.... need to get timestamp with millisecond in UNIX. Date command gives Year,month day, hour,minute and second but it does not give millisecond. Any... (5 Replies)
Discussion started by: Bhups
5 Replies
Login or Register to Ask a Question