time function question

This is a discussion on time function question within the C++ Programming forums, part of the General Programming Boards category; is there some kind of magic function i can use to set a variable equal to the time of the ...

  1. #1
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    time function question

    is there some kind of magic function i can use to set a variable equal to the time of the day?

    if so, how would i use this function?
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  2. #2
    Unregistered
    Guest
    #include <time.h>
    time_t now;
    time_t *pNow;
    now = time(pNow);

    the system time is stored in the variable now and in the pointer pNow. You could use now or pnow alone if desired. Unfortunately time_t and time_t * are not convenient formats so you can convert them like this:

    char sNow[30] = ctime(pnow);
    tm tNow = localtime(pnow);


    the char string returned by ctime() is something like:

    Mon Dec 31, 2001

    and tm is a struct something like this:

    struct tm
    {
    int tm_month;
    int tm_day;
    int tm_year;
    int day_of_week;
    //etc.
    }

    Exact formats, prototypes, definitions, etc. should be available in the help section of your compiler.

  3. #3
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    could the above code be modified to tell me the time of the day according to the internal clock on the comp?
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  4. #4
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169
    I guess what im asking is....

    How could i manipulate the variable holding the day of the week, month, day and time that the variable is holding into just time?
    Last edited by heat511; 01-03-2002 at 11:23 AM.
    "uh uh uh, you didn't say the magic word"
    -Jurassic Park

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Originally posted by Unregistered
    #include <time.h>
    time_t now;
    time_t *pNow;
    now = time(pNow);

    Woah, is that an uninatialized pointer there? It looks like its pointing to nowhere, but then you pass it to the time function, which fills it in, so your filling up some random memory. Not cool

    http://www.cppreference.com/stddate.html has a reference for all the time functions


    The localtime function returns a struct that has a bunch of info
    info here from http://www.devx.com/free/tips/tipvie...ontent_id=1810

    struct tm
    {
    int tm_sec; // the number of elapsed seconds in the minute
    int tm_min; // the number of elapsed minutes in the hour
    int tm_hour; // the number of elapsed hours in the day (0-23)
    int tm_mday; / /the number of elapsed days in the month (1-31)
    int tm_mon; // the number of elapsed months in the year (0-11)
    int tm_year; //the numbers of elapsed years since 1900
    int tm_wday; // the number of elapsed days in the week since Sunday (0-6)
    int tm_yday; //the number of the elapsed days in the year (0-365)
    int tm_isdst; // equals 1 if daylight savings is in effect, zero if not, -1 if unknown
    };

  6. #6
    Unregistered
    Guest
    You're right, using unitialize pointers can be problematic. But it's not as ominus as you make it sound. The code snippet you refer to is no different than this:

    int num = 1;
    int * pNum;
    pNum = &num;

    Used with respect unitialized pointers are not going to blow up or anything.


    To refer to certain members of a struct, but not all, you can use the dot or arrow operators. Using the dot operator it goes something like ths:

    //using snippet from first post:

    cout << tNow.tm_hour << ':' << tNow.tm_min << ':' << tNow.tm_sec << endl;

    leave out the colons if you wish. Manipulate tm structs as you need to to use whatever display format you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function name basic question help
    By kenryuakuma in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2008, 08:48 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21