473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Elasped Time - in hr, min and seconds

Neo
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
Nov 14 '05 #1
4 2803
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
Nov 14 '05 #2
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

Nov 14 '05 #3
> 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
ma*******@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

Nov 14 '05 #4
> "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
ma*******@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: kpp9c | last post by:
I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing...
77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
1
by: davb | last post by:
Trying to sum the overall elasped time in hours using "start" and "end" times in separate fields from multiple rows. I have tried datediff, timediff and datepart functions on this attemp. When...
5
by: Tom | last post by:
A field in a data set I want to import into Access is in Unix time (seconds from a certain time on a certain date). Does anyone know the precise date and the precise time on that date that Unix is...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
7
by: nono909 | last post by:
I wrote the following time class for the following assignment.i need help in completing this program pleasee. Write a class to hold time. Time is the hour, minute and seconds. Write a constructor...
2
by: WJM | last post by:
I'm looking for some help with a error in the code below. When 119.45 seconds are entered the code returns 01:59.45, but when you enter 119.51 the code returns 02:59.51. But, if I enter 120.00 the...
8
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+...
0
by: Hendrik van Rooyen | last post by:
Lawrence D'Oliveiro wrote: The above is an endless loop, doing something every five seconds, and something else in the in between times. I don't think you are dense - its a good point,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.