Thread: Is there a way to get the time?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Is there a way to get the time?

    Is there a way for my program to take the time from the user's computer clock and store it into some sort of variable? I'd like to put a bonus in my game program for somebody using it in the wee hours of morning for example.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Some simple function calls and an include will get you on your way:

    Code:
     #include <time.h>
    then to get the current time:
    Code:
     time_t plaintime;
     tm *ptm;
     
     time(&plaintime);
     ptm = localtime(&plaintime);
    and ptm contains all of the information about the current time.

    Here's the information stored in the ptm structure:

    Quoted from msdn
    tm_sec

    Seconds after minute (0 – 59)

    tm_min

    Minutes after hour (0 – 59)

    tm_hour

    Hours after midnight (0 – 23)

    tm_mday

    Day of month (1 – 31)

    tm_mon

    Month (0 – 11; January = 0)

    tm_year

    Year (current year minus 1900)

    tm_wday

    Day of week (0 – 6; Sunday = 0)

    tm_yday

    Day of year (0 – 365; January 1 = 0)

    tm_isdst

    Positive value if daylight saving time is in effect; 0 if daylight saving time is not in effect; negative value if status of daylight saving time is unknown. The C run-time library assumes the United States’s rules for implementing the calculation of Daylight Saving Time (DST).






    and to access each one just use:

    Code:
     ptm->tm_mon
    or whatever you want to access.

    -Hope that helps

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Wow! Thank you very much, that's exactly what I need!

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    101
    >>#include <time.h>
    #include <ctime> in C++. The functions are also placed within the std namespace, so qualification is required if the compiler conforms to the standard.

    >>
    Code:
    time_t plaintime;
    tm *ptm;
     
    time(&plaintime);
    ptm = localtime(&plaintime);
    Both of these functions can fail. Always test return values.
    Code:
    time_t plaintime;
    tm *ptm;
    
    if (time(&plaintime) == (time_t)-1) {
      // Handle the error
    }
    ptm = localtime(&plaintime);
    if (ptm == 0) {
      // Handle the error
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM