Getting Current time and adding some time interval

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

Getting Current time and adding some time interval

 
0
  #1
Dec 17th, 2007
The function time() retrieves the current calendar time from the system's clock. It has the following prototype:

time_t time(time_t * tp);

In systems in which no clock is available, the function returns -1.
Part 1:
I found this on the internet. I have a question about the above quote where they mention there could be situation in which "no clock is available".

What do they mean by no clock availabe? For example I am running linux, doesn't all OS have some kind of clock?

Part 2:
I need to add some time interval to the current time, how I do this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Getting Current time and adding some time interval

 
0
  #2
Dec 17th, 2007
>>doesn't all OS have some kind of clock
Most do, but some embedded systems do not, for example the computers in your car do not have a clock (except, of course, the clock you see). A few years ago when we were all busy making Y2K adjustments to our programs auto manufacturers didn't have to change anything because their computers didn't have clocks. I know of a couple other embedded systems that don't have them either.

Part 2:
Depends. If you know the number of seconds then just add the seconds to the return value of time(). Otherwise, call localtime() to get a pointer to struct tm, change the number of hours, minutes or seconds you want to increment, then call mktime() to normalize the tm structure (make corrections). For example if you want to increment the current time by 9 hours then add 9 to the tm_hours structure member and don't worry if the result is greater than 24 because mktime() will make all necessary adjustments to the date and time.
Last edited by Ancient Dragon; Dec 17th, 2007 at 10:17 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC