Re: Logging with date troubles..

From: Mike Stilson (
Date: 08/04/02

On Sun, Aug 04, 2002 at 02:09:08PM +0200, Christian Ejlertsen wrote:
>I have been working on a reconfiguring of the logging system towards beeing a daily log.
>I'd like it to automaticly append this format to the logname MMDDYYYY.mudname.log.
>what i have been playing is some of the time formats.. I ve read the man asctime, and it
>mentions that the time is stored in this struct.
>              struct tm {
>                      int     tm_sec;         /* seconds */
>                      int     tm_min;         /* minutes */
>                      int     tm_hour;        /* hours */
>                      int     tm_mday;        /* day of the month */
>                      int     tm_mon;         /* month */
>                      int     tm_year;        /* year */
>                      int     tm_wday;        /* day of the week */
>                      int     tm_yday;        /* day in the year */
>                      int     tm_isdst;       /* daylight saving time */
(and from <time.h> it also has:
                       long int tm_gmtoff;
                       __const char *tm_zone;
(names vary depending on settin of __USE_BSD)

>              };
>How is this struct accessible for me i've tried a few options no luck so far.
>Or is there an even easier way to do it.

Sure there's an easier way to do it, but here's one with that struct:

    char buf[40];
    time_t now = time(0);
    strftime(buf, sizeof(buf), "%m%d%Y.mudname.log", localtime(&now));

buf will now contain "MMDDYYY.mudname.log".

If you want to do more with the tm struct, just change the last two
lines to something like:

    struct tm *t = localtime(&now);
    strftime(buf, sizeof(buf, "%m%d%Y.mudname.log", t);

Note that since %s is used as a conversion specifier by strftime, if
you're planning to make this a more general use routine for different
muds, you'll need to actually build the string piece by piece. (ie, you
can't do something like:

    const char *fmt = "%m%d%Y.%s.log";
    strftime(buf, sizeof(buf), fmt, t);
    sprintf(name_buf, buf, mud_name_variable);

You'll have to strcat the "mudname.log" part to the output of the
strftime buffer somehow or another.


   | FAQ: |
   | Archives: |
   | Newbie List:   |

This archive was generated by hypermail 2b30 : 06/25/03 PDT