LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   convert total no of seconds in the format hour minutes and seconds (https://www.linuxquestions.org/questions/programming-9/convert-total-no-of-seconds-in-the-format-hour-minutes-and-seconds-276427/)

suchi_s 01-11-2005 03:02 AM

convert total no of seconds in the format hour minutes and seconds
 
how to convert the integer no which represents the total no of seconds
int hour minute and seconds format..
is there any function..to do

jlliagre 01-11-2005 03:29 AM

in what language ?

suchi_s 01-11-2005 03:30 AM

in awk shell scripting

jlliagre 01-11-2005 03:46 AM

They are no function I'm aware, but it is easy to implement:
Code:

function hms(s)
{
  h=int(s/3600);
  s=s-(h*3600);
  m=int(s/60);
  s=s-(m*60);
  printf("%d:%02d:%02d\n", h, m, s);
}


suchi_s 01-11-2005 04:49 AM

is there any built in functuon

jlliagre 01-11-2005 05:03 AM

There are no time builtin functions in the standard awk / nawk, but gnu awk does have these extensions:
mktime
strftime
systime

They are documented in the gawk manual page

suchi_s 01-11-2005 06:11 AM

strftime converts but from since jan 1970..
how shall i

heema 01-11-2005 06:24 AM

you could make a script :

Code:

#!/bin/bash

seconds=0

echo -n "Enter number of seconds : "
read seconds

hours=$((seconds / 3600))
seconds=$((seconds % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))

echo "$hours hour(s) $minutes minute(s) $seconds second(s)"


save this to a file and name it seconds.sh

then type :
su
<your root password>
chmod a+x seconds.sh

then to execute it type : ./seconds.sh


P.S: i didnt write this script , i found it before and sorry i cant remember where i got it from

jlliagre 01-11-2005 02:39 PM

heema,
- the OP asked for awk and you sent a shell script.
- there's absolutely no need to become root to chmod a file to be executable.

heema 01-11-2005 07:38 PM

Quote:

- the OP asked for awk and you sent a shell script.
sorry i read only the question , i didnt notice he wanted it in awk

jlliagre 01-11-2005 08:33 PM

Quote:

strftime converts but from since jan 1970..
how shall i
try this:
Code:

print strftime("%H:%M:%S", 3620);

kitgerrits 11-20-2010 02:01 PM

Ugly, but works...
 
You seem correct, but AWK seems to make some strange assumptions.
apparently, 433 seconds translates into 01:07:11:
Code:

echo |awk '{print strftime("%H:%M:%S", 433)}'
01:07:13

Everything looks OK once you drop off the hour, but that means losing any minutes over 60
Code:

echo |awk '{print strftime("%M:%S", 433)}'
07:13

I have regressed into division and casing to get the type I was looking for:
Code:

echo |awk '{print int(433/60)":"int(433%60)}'
7:13

This will scale over 60 minutes:
Code:

echo |awk '{print int(4000/60)":"int(4000%60)}'
66:40


theNbomr 11-20-2010 02:36 PM

It looks like strftime() is sensitive to timezones. I played around with setting $TZ, and it affected the result of strftime(). I would post an example, but I'm not conversant enough in matters of timezones to give an example I know isn't nonsensical.

--- rod.

AnanthaP 11-20-2010 06:07 PM

I like this best from an earlier post in the same thread. It's simple, understandable and generic.

Quote:

Code:
function hms(s)
{
h=int(s/3600);
s=s-(h*3600);
m=int(s/60);
s=s-(m*60);
printf("%d:%02d:%02d\n", h, m, s);
}

kitgerrits 11-21-2010 02:55 AM

Quote:

Originally Posted by theNbomr (Post 4165586)
It looks like strftime() is sensitive to timezones. I played around with setting $TZ, and it affected the result of strftime(). I would post an example, but I'm not conversant enough in matters of timezones to give an example I know isn't nonsensical.

--- rod.

You're entirely correct.
I forgot this part when reading up on strftome():

strftime([format [, timestamp [, utc-flag]]])
This function returns a string. It is similar to the function of the same name in ISO C. The time specified by timestamp is used to produce a string, based on the contents of the format string. If utc-flag is present and is either non-zero or non-null, the value is formatted as UTC.

It works like a charm:
Code:

echo |awk '{print strftime("%H:%M:%S", 433,1)}'
00:07:13

Thanks for the tip!

doctorAL 03-15-2011 11:34 AM

echo "3661" | awk '{print int($0/3600),int($0%3600/60),$0%3600%60}'


All times are GMT -5. The time now is 12:11 AM.