Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to print something out every 50 seconds. I figured I would calculate that 50 seconds had passed by subtracting my current time from the previous time.

Let's say I know the hour, minute and seconds of the previous and current time, how do I calculate how much time had passed in seconds?

I'm using C
Posted
Updated 19-Nov-11 19:47pm
v2
Comments
RaisKazi 20-Nov-11 1:19am    
Which Technology?
Sergey Alexandrovich Kryukov 20-Nov-11 20:43pm    
Language is not enough. Please, platform, libraries you want to use, other relevant detail.
--SA

Calculations do not help. You really need either a timer of a sleep state of a current thread, this way you can avoid spin-wait and wasting CPU time. You can do it the way the OS switches off your thread and don't schedule it back to execution until it is awaken by a timeout to continue execution. This is just a basic idea, and the API depends on your platform and/or libraries you might use.

Too bad you did not provide essential details.

—SA
 
Share this answer
 
Thanks for the answers. Here's what I did.

I used a modified version of an answer someone else gave me:

( (Cur_hour - Prev_hour)*3600) + ((Cur_min - Prev_min)*60) + (cur_sec - Prev_sec).

I was using C, so I calculated the difference by:
(cur_hour *3600)+(cur_min*60)+(cur_sec) - (prev_hour * 3600) + (prev_min*60)+(prev_sec).
 
Share this answer
 
Dear CSLexy u are using C then use delay(t) function of header file dos.h and there t in delay function for50 second is 50000.
 
Share this answer
 
If you are using .Net, use the TimeSpan class to calculate the difference in seconds.
This[^] is a good example that should give you enough ideas to proceed.
 
Share this answer
 
Comments
CSLexy 20-Nov-11 1:47am    
I'm using C

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900