1,105,594 Community Members

struct tm

Member Avatar
zachman1094
Light Poster
36 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Thanks!

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Sure. Try this

Member Avatar
gerard4143
Nearly a Posting Maven
2,385 posts since Jan 2008
Reputation Points: 371 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 0 [?]
 
0
 

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

http://www.cplusplus.com/reference/clibrary/ctime/tm/

Member Avatar
Ancient Dragon
Achieved Level 70
27,656 posts since Aug 2005
Reputation Points: 5,234 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: