#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep 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! :)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    16
    Rep 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! :) :)
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    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:
    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:
    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.

IMN logo majestic logo threadwatch logo seochat tools logo