The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Getting formatted (UNIX) system time and date
Discuss Getting formatted (UNIX) system time and date in the C Programming forum on Dev Shed. Getting formatted (UNIX) system time and date C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 7th, 2003, 08:09 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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! 
|

May 7th, 2003, 10:34 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

May 7th, 2003, 10:41 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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! 
|

May 7th, 2003, 10:49 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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:
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );
Return Value
strftime returns the number of characters placed in strDest if the total number of resulting characters, including the terminating null, is not more than maxsize. wcsftime returns the corresponding number of wide characters. Otherwise, the functions return 0, and the contents of strDest is indeterminate. |
Says the man page on Red Hat Linux 7:
Quote:
RETURN VALUE
The strftime() function returns the number of characters
placed in the array s, not including the terminating NUL
character, provided the string, including the terminating
NUL, fits. Otherwise, it returns 0, and the contents of
the array is undefined. (Thus at least since libc 4.4.4;
very old versions of libc, such as libc 4.4.1, would
return max if the array was too small.)
Note that the return value 0 does not necessarily indicate
an error; for example, in many locales %p yields an empty
string. |
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|