Monday, July 27, 2009

How do I use the getdate() function to get the time and date of a machine usng C code?

I have to use the getdate() function and this is about all I know about it:





#include %26lt;time.h%26gt;


struct tm *getdate (const char *string);





the function getdate() converts a string pointed to by string into


the tm structure that it returns. Note the tm structure below this


table, it contains fields that record time too, not just date)





In short, my question really is, how do I use the getdate function, sample code would help. Even an online source would be helpful.

How do I use the getdate() function to get the time and date of a machine usng C code?
Please see if the following URL could be of some help for you.





http://www.opengroup.org/


onlinepubs/


000095399/


functions/getdate.html





Please note:


I had to insert line breaks as the complete URL was not visible when previewing my answer.





Added later:


getdate() is used to convert a string (which is user understandable broken down time) into the internal format in which it is stored (for the purpose of calculations).





if you need to convert the system time into a user understandable broken down format, then you need to use:





localtime() followed by asctime()





locatime converts the clock time into the tm structure format and this is converted into printable format by asctime.





Look at the code below. You can compile and run the same:





#include%26lt;stdio.h%26gt;


#include %26lt;time.h%26gt;





int main(void)


{


time_t timer;


// the following code gets the clock time


timer=time(NULL);


// the following line converts the clock time


// into the struct tm format (done by localtime())


// and prints it in the readable format (done by asctime())


printf("The current time is %s.\n",asctime(localtime(%26amp;timer)));


return 0;


}





Hope this helps.

survey questions

No comments:

Post a Comment