Converting seconds to time

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting seconds to time
# 1  
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.


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

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

  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  
Why not
printf "%02d:%02d:%02d\n" $((25793 / 3600)) $((25793 % 3600 / 60 )) $((25793 % 3600 % 60))

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

Last edited by RudiC; 02-23-2018 at 09:58 AM..
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
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


Is there any good way to do this?


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

The following does not work well

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

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #62
Difficulty: Easy
DNS in Internet terms stands for the 'Domain Name System'.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Time in seconds on AIX

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

3. Shell Programming and Scripting

Get how much time process has been running in seconds

I use this command to get the time elapsed for a process ps -eo pid,pcpu,pmem,user,args,etime,cmd --sort=start_time | grep perl It gives in format 19990 0.0 0.0 user /usr/bin/php 5-09:58:51 /usr/bin/php I need in seconds. Please use CODE tags for sample input and output as well... (2 Replies)
Discussion started by: anil510
2 Replies

4. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

Id 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( 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 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

Featured Tech Videos