Hey everyone, I've got a question about something called struct tm. I'm not really even sure if it is a string, array, or what, otherwise I would've said that instead of 'something'. I need to know how to use it. I've been searching the internet for the past hour, and haven't found a single website saying something like 'To find the current date and time using struct tm...' And that's what I need to know. So if I have a simple C file, what do I need to include and use in order to find the current date and time? Preferable in a format of mm/dd//yyyy.


6 Years
Discussion Span
Last Post by Ancient Dragon

Check this out

Broken-down  time  is  stored  in  the structure tm which is defined in
       <time.h> as follows:

           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 */

       The members of the tm structure are:

       tm_sec    The number of seconds after the minute, normally in the range
                 0 to 59, but can be up to 60 to allow for leap seconds.

       tm_min    The number of minutes after the hour, in the range 0 to 59.

       tm_hour   The number of hours past midnight, in the range 0 to 23.

       tm_mday   The day of the month, in the range 1 to 31.

       tm_mon    The number of months since January, in the range 0 to 11.

       tm_year   The number of years since 1900.

       tm_wday   The number of days since Sunday, in the range 0 to 6.

       tm_yday   The number of days since January 1, in the range 0 to 365.

       tm_isdst  A  flag  that  indicates  whether  daylight saving time is in
                 effect at the time described.  The value is positive if  day‐
                 light  saving time is in effect, zero if it is not, and nega‐
                 tive if the information is not available.

Or this web site which took me 30 secs to find



how to use:

1. call time() to get current date/time as number of seconds since 1 Jan 1970.

2. call localtime() to get struct tm pointer. If you want GMT them call gmtime() instead of localtime()

3. Use sprintf() or strftime() to convert the struct tm to a string in any format you want.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.