Is there any function in std. C to convert seconds into hour, min and sec. ?
Lets say the following code takes 129 seconds, how do i get components of
time in "hour min and second" format? Is there any std. library function to
do this?
it should give "0 hrs 2 min 9 sec"
/* The following code only prints elapsed time in seconds */
#include<stdio.h>
#include<limits.h>
#include<time.h>
int main(void)
{
time_t t1, t2;
unsigned int i,j, k;
t1 = time(NULL);
/* some code to timed here.... */
/* ..... */
for(i = 0; i < ~0; i++);
/* ... end of processing ... */
t2 = time(NULL);
k = difftime(t2, t1);
printf("Time Elapsed = %d seconds\n", k);
return 0;
}
-Neo
|
|
0
|
|
|
|
Reply
|
timeless_illusion (71)
|
3/28/2005 11:07:16 AM |
|
Neo wrote:
> Is there any function in std. C to convert seconds into hour, min and sec.
> ? Lets say the following code takes 129 seconds, how do i get components
> of time in "hour min and second" format? Is there any std. library
> function to do this?
> it should give "0 hrs 2 min 9 sec"
>
% is the infix modulus operator.
If you cant now get the answer, give up programming.
gtoomey
|
|
0
|
|
|
|
Reply
|
nospam258 (216)
|
3/28/2005 11:38:47 AM
|
|
Neo wrote:
>
> Is there any function in std. C to convert seconds into hour, min
> and sec. ? Lets say the following code takes 129 seconds, how do i
> get components of time in "hour min and second" format? Is there
> any std. library function to do this?
> it should give "0 hrs 2 min 9 sec"
>
> /* The following code only prints elapsed time in seconds */
> #include<stdio.h>
> #include<limits.h>
> #include<time.h>
>
> int main(void)
> {
> time_t t1, t2;
> unsigned int i,j, k;
>
> t1 = time(NULL);
>
> /* some code to timed here.... */
>
> t2 = time(NULL);
> k = difftime(t2, t1);
> printf("Time Elapsed = %d seconds\n", k);
> return 0;
> }
try (untested):
#include <stdlib.h>
#include <time.h>
long int hrs, mins, secs;
ldiv_t result;
time_t t1, t2;
.....
t1 = time(NULL);
....
t2 = time(NULL);
secs = difftime(t2, t1);
result = ldiv(secs, 60);
secs = result.rem;
mins = result.quot;
mins = ldiv(mins, 60);
mins = result.rem;
hrs = result.quot;
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
3/28/2005 1:33:00 PM
|
|
> Is there any function in std. C to convert seconds into hour,
> min and sec. ?
No, but it sounds like a perfectly wonderful routine to add to your
personal library of routines.
Then you'd have the added advantage of learning how to maintain an
object library in C.
Or you could take the cheap way out and make it a standalone source
file and use a makefile to include it for the program. Assuming
'make' is supported on your platform, of course.
_________________
Steven K. Mariner
marinersk@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/
|
|
0
|
|
|
|
Reply
|
marinersk (16)
|
3/28/2005 7:30:04 PM
|
|
> "If you want to post a followup via groups.google.com, don't
> use the broken "Reply" link at the bottom of the article.
> Click on "show options" at the top of the article, then click
> on the "Reply" at the bottom of the article headers." - Keith
> Thompson
Thanks for the tip.
I was wondering why the formatting was sometimes quite stupid. Hadn't
looked into it enough to hit critical mass for intuitive resolution.
Figured it was just another one of those things you live with in an
imperfect Internet world.
_________________
Steven K. Mariner
marinersk@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/
|
|
0
|
|
|
|
Reply
|
marinersk (16)
|
3/28/2005 7:32:20 PM
|
|
|
4 Replies
45 Views
(page loaded in 0.071 seconds)
Similiar Articles: Time elapsed record - comp.databases.filemaker... script to update the times every couple of minutes. Any ... The first > layout is for our entry of time that something ... data structure, which I ... seconds elapsed since ... How to get the time difference in a script - comp.unix.solaris ...... time1 - time2 I want to get hours and minutes from time ... so start=$SECONDS process_logic end=$SECONDS time ... Hi, How can I calculate "elapsed time" since the ... Converting milliseconds to Days, Hours, Minutes, Seconds - comp ...... min % 60 > t = two(min) + ":" + t > > var day = Math.floor(hr/60 ... comp.soft-sys.matlab Converting milliseconds to Days, Hours, Minutes, Seconds - comp ... get utc time to ... Finding difference in minutes between 2 DateTimes - comp.databases ...I'm trying to find a status time in minutes and I can't seem ... in the way that > there are always 60 seconds in a ... ... Comparison - comp.unix.shell... print $((elapsed_mins ... Assura terminates on Segmentation violation - comp.cad.cadence ...> > elapsed time = 2.57 minutes, cpu time = 2.55 minutes > > Executing: widthMOS ... butting > diffLayer) 0.5) > elapsed time = 28.00 seconds, cpu time = 26 ... Stabilizing the drift file? - comp.protocols.time.ntp... is unknown) up after 5 minutes in a cluster, which also means, that its time ... drift with my script for 2 seconds b) Set the time ... time.ntp How to calculate "elapsed time ... finding the closest number in an array - comp.soft-sys.matlab ...tic tmp = abs(f-val); [idx idx] = min(tmp); %index of closest value closest = f(idx); %closest value toc %Elapsed time is 0.100856 seconds. Convert UTC seconds to actual time. - comp.lang.cThe amount of time legally elapsed differs from one country to another. ... translates to, and add 40 years 185 days 20 hours 0 minutes 0 seconds to > that time. find the column index of the first occurance of a value in each ...... find(A); [r c] = ind2sub(size(A),idx); cmin = accumarray(r,c,[],@min) ... end toc isequal(cmin1,cmin2,cmin3) %{ Elapsed time is 2.553398 seconds. Elapsed time ... How can i take my system time in milliseconds in matlab - comp ...... here ms = round(toc * 1000); If you mean "elapsed time ... Converting milliseconds to Days, Hours, Minutes, Seconds - comp ... How can i take my system time in ... Elasped Time - Microsoft Access / VBA - Bytes - Tech Commmunity ...Elasped Time - in hr, min and seconds; Race Time Part II; Sum overall elasped time from query then report; time.clock() problem under linux (precision=0.01s) 8/1/2012 7:03:50 AM
|