We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,430 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

struct tm

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!

4
Contributors
3
Replies
1 Hour
Discussion Span
2 Years Ago
Last Updated
4
Views
zachman1094
Light Poster
36 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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/

gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,157 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0637 seconds using 2.69MB