| | |
Getting Current time and adding some time interval
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Solved Threads: 0
•
•
•
•
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.
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?
>>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.
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.
![]() |
Similar Threads
- Slide In, Slide Out, ala Digg (JavaScript / DHTML / AJAX)
Other Threads in the C Forum
- Previous Thread: makefile question
- Next Thread: newbie: C-Program write to /var/log/mylog via syslog
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file floatingpointvalidation fork frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer lazy license linked linkedlist linux linuxsegmentationfault list logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat openwebfoundation pattern pause pdf pointer pointers posix power problem program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures student suggestions system test testautomation unix urboc user voidmain() win32api windows.h






