Thread: how to make the prog. read time from pc clock

  1. #1

    Question how to make the prog. read time from pc clock

    I have trouble finding out hot to get my program use the pc clock to get the time. Is it possible. How do I do it?

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    it is possible to read the system time, i cant remember the time at the moment but you use a struct called time. check the headers time.h and dos.h - soryy i cant be more specific at the moment
    Monday - what a way to spend a seventh of your life

  3. #3
    include time.h

    time(NULL) i think does it.

  4. #4
    Registered User
    Join Date
    Dec 2001


    For Visual Basic
    you can access time by using
    or Now

    For c++
    Use the functions in time.h and (possibly dos.h)
    im not to sure what they are but I think they are time();
    and another one.

  5. #5
    time() returns the system time and stores it as a pointer to type time_t passed as the argument, or as variable of type time_t. The prototype is below and is found in time.h or ctime (depending on the version of your compiler)

    time_t time(time_t*);

    time_t is typedefed as type long and represents the difference in seconds from a given point in time (which point in time is compiler version specific). Since this information is useful in some circumstances and not in others, there are several other functions that will convert the time_t* into a more useful version. One is ctime(). Here's the prototype:

    char * ctime(time_t*);

    The string returned is 28 or so char long and has a format similar to :

    Monday Dec 31, 1999 21:43:56

    I forget the exact format.

    localtime() is another useful function. It has the prototype:

    tm localtime(time_t*);

    tm is a struct defined in time.h something like this:

    struct tm
    int tm_month;
    int tm_day;
    int tm_year;
    int tm_hour;
    int tm_min;
    int tm_sec;
    int tm_wkday;

    that you can then manipulate at your discretion.

    As previously discussed you should look all these functions and structs up in your compilers help section for all the specific details. Here's some off the cuff, non-compiled examples of how you might use these functions/stucts/whatever:

    #include <time.h>
    #include <string.h>
    //whatever else

    time_t now;
    time_t * pNow;
    char szNow[30];

    now = time(NULL);


    pNow = &now;

    tm tmNow = localtime(pNow);

    cout << tmNow.hour << ":" << tmNow.min << ":" << tmNow.sec << endl;

    strcpy(szNow, ctime(pNow));
    cout << szNow;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  2. How to read from date and time picker?
    By vikernes in forum Windows Programming
    Replies: 3
    Last Post: 06-17-2006, 07:10 PM
  3. How to read in time format?
    By witchiz brews in forum C Programming
    Replies: 1
    Last Post: 02-12-2006, 02:10 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  5. PingPong But how to make 2 thing at the same time..
    By Gugge in forum C Programming
    Replies: 5
    Last Post: 04-02-2002, 06:13 PM