|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Getting formatted (UNIX) system time and date
Hello!
I need to get the system's date and time in the dd-mm-yyy and hh:mm:ss format and write it on to a string (so it can be written onto a structure's member and later on, to a file). I figured something out like this: char sRelTempo[30], pts=':', space=' ', hif='-'; time_t agora; struct tm *relTempo; time(&agora); relTempo = localtime(&agora); sprintf(sRelTempo,"%d",relTempo->tm_mday); sprintf(sRelTempo,"%c",hif); sprintf(sRelTempo,"%d",relTempo->tm_mon); etc... but it's quite a huge chunk of code and it won't give me days and seconds in the format specified. So, in "The C Programming Language" I saw the strftime function that, I guess, will do this for me. I just can't figure out how to use it (and what parameters to provide and what will it return); can someone post an example, please? So, how can I solve my problem, if this function doesn't do what I need? Thanx in advance! ![]() |
|
#2
|
||||
|
||||
|
Yes, it looks like strftime() should do the job for you. Remember, you can combine the format specifiers to customize your own string.
For that matter, your example can combined into one statement: Code:
sprintf(sRelTempo,"%d%c%d",relTempo->tm_mday,hif,relTempo->tm_mon); or better: Code:
sprintf(sRelTempo,"%d-%d",relTempo->tm_mday,relTempo->tm_mon); Look up the format specifiers in your help/manpage system. If none of them exactly matches what you want, then create it; eg, for your "dd-mm-yyyy hh:mm:ss" string: Code:
strftime(sRelTempo,29,"%d-%m-%Y %T",relTempo); Comparing a few different listings of the format specifiers, I found that not all the help-file/man-page listings were complete. So do a Google search on strftime and play with it until you get what you want. |
|
#3
|
|||
|
|||
|
I'm pretty sure that
strftime(sRelTempo,29,"%d-%m-%Y %T",relTempo); will do the job, I just didn't know how to use it properly, it kept giving errors I couldn't solve. By the way, what (or how) will the function return? I still have to put the return into a string->structure->file And thanx a lot so far! ![]() |
|
#4
|
||||
|
||||
|
How were you using it and what errors did it give you?
As for what strftime returns, it's the number of characters in the string that was generated. Says Visual C++6 Help: Quote:
Says the man page on Red Hat Linux 7: Quote:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > Getting formatted (UNIX) system time and date |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|