LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-11-2005, 03:02 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
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
 
Old 01-11-2005, 03:29 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
in what language ?
 
Old 01-11-2005, 03:30 AM   #3
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
in awk shell scripting
 
Old 01-11-2005, 03:46 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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);
}
 
Old 01-11-2005, 04:49 AM   #5
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
is there any built in functuon
 
Old 01-11-2005, 05:03 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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
 
Old 01-11-2005, 06:11 AM   #7
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
strftime converts but from since jan 1970..
how shall i
 
Old 01-11-2005, 06:24 AM   #8
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 46
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

Last edited by heema; 01-11-2005 at 06:26 AM.
 
Old 01-11-2005, 02:39 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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.
 
Old 01-11-2005, 07:38 PM   #10
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 46
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
 
Old 01-11-2005, 08:33 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Quote:
strftime converts but from since jan 1970..
how shall i
try this:
Code:
print strftime("%H:%M:%S", 3620);
 
Old 11-20-2010, 02:01 PM   #12
kitgerrits
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Rep: Reputation: 1
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
 
Old 11-20-2010, 02:36 PM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.

Last edited by theNbomr; 11-20-2010 at 02:39 PM.
 
Old 11-20-2010, 06:07 PM   #14
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 627

Rep: Reputation: 137Reputation: 137
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);
}
 
Old 11-21-2010, 02:55 AM   #15
kitgerrits
LQ Newbie
 
Registered: Dec 2008
Posts: 23

Rep: Reputation: 1
Quote:
Originally Posted by theNbomr View Post
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
get logged out every 5 seconds Avatar LQ Suggestions & Feedback 4 12-20-2005 10:13 AM
Logged in for 10 seconds or less Yawgmoth7 Linux - Software 1 11-01-2005 07:43 AM
10 seconds in google? davidmelv Linux - Software 1 10-09-2004 09:39 AM
SECONDS per FRAME opensource Linux - General 4 02-01-2004 04:04 AM
strftime - 61 seconds?!?!?!? esnagel Linux - General 2 09-12-2002 07:06 PM


All times are GMT -5. The time now is 08:35 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration