using setTime format

This is a discussion on using setTime format within the Windows Programming forums, part of the Platform Specific Boards category; Hey everyone, Im trying to add a clock to my program in the status bar that just shows how long ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    215

    using setTime format

    Hey everyone, Im trying to add a clock to my program in the status bar that just shows how long the user has been using the program. so itll be basically a timer, so when the user first executes the program itll start and look like this: 0:00:00. Then after 1 second, itll look like this 0:00:01 and so on.

    Anyways, I went on www.msdn.com and checked out the library and the only function i could see that would be of any use to me was the setTime format. I was wondering if I am on the right track and how would i add it to the status bar.

    Thanks a lot!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    How about:

    Code:
    char buffer[50];
    int hours   = seconds_count / 3600;         /* 3600 seconds in an hour. */
    int minutes = (seconds_count % 3600) / 60;  /* Get remainder after removing hours
                                                 * and divide by 60 to get minutes. */
    int seconds = (seconds_count % 60);         /* Get remainder after removing minutes. */
    
    sprintf(buffer, "%d:%02d:%02d", hours, minutes, seconds);
    
    SetWindowText(hwndStatus, buffer);

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    215
    How do i make it tick. Like itll start at 00:00:00, how do i make it change it 00:00:01, then 00:00:02 and so on?

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Use a timer.

    Setup:
    Code:
    /* Get the start time. */
    g_timeStart = time();
    
    /* Set a timer to fire every second (1000 milliseconds) */
    SetTimer(NULL, 0, 1000, TimerProc);
    Your TimerProc:
    Code:
    VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
    {
        time_t timeNow = time();
        int seconds_count = (int) difftime(timeNow, g_timeStart);
        char buffer[50];
        int hours   = seconds_count / 3600;         /* 3600 seconds in an hour. */
        int minutes = (seconds_count % 3600) / 60;  /* Get remainder after removing hours
                                                     * and divide by 60 to get minutes. */
        int seconds = (seconds_count % 60);         /* Get remainder after removing minutes. */
    
        sprintf(buffer, "%d:%02d:%02d", hours, minutes, seconds);
    
        SetWindowText(hwndStatus, buffer);
    }

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    215
    Hey, where should i declare the first part. I put it in my MainWndproc. Anyways i got 5 errors,and here they are.

    D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(9126) : error C2065: 'TimerProc' : undeclared identifier
    D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(9123) : error C2198: 'time' : too few actual parameters
    D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(9123) : error C2065: 'g_timeStart' : undeclared identifier
    D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10621) : error C2373: 'TimerProc' : redefinition; different type modifiers
    D:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10622) : error C2198: 'time' : too few actual parameters

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think you could have worked through these errors:

    1. error C2065: 'TimerProc' : undeclared identifier - Use a prototype.
    2. error C2198: 'time' : too few actual parameters - time() takes one argument so the code should be:
      Code:
      g_timeStart = time(NULL);
    3. error C2065: 'g_timeStart' : undeclared identifier - The g_ indicates a global variable. It is up to you to declare it somewhere:
      Code:
      time_t g_timeStart;

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    215
    this is the error i got

    C:\Documents and Settings\Desktop\STP\STP\stpwin1.c(647) : error C2099: initializer is not a constant

    when i did this

    g_timeStart = time(NULL);

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    215
    also this caused me an error:

    time_t timeNow = time();

    C:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10619) : error C2198: 'time' : too few actual parameters

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> error C2198: 'time' : too few actual parameters <<

    See number 2 above!

    >> error C2099: initializer is not a constant <<

    In C, you can't call a function outside a function.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    215
    for time_t timeNow = time(NULL) i got this error

    C:\Documents and Settings\Desktop\STP\STP\stpwin1.c(10640) : error C2275: 'time_t' : illegal use of this type as an expression

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    215
    ok nevermind, i got it to compile but it doesnt show up on my window
    Last edited by osal; 07-14-2004 at 02:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  3. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 07:25 AM
  4. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 06:41 AM
  5. Can't Format C
    By caroundw5h in forum Tech Board
    Replies: 40
    Last Post: 04-26-2004, 10:57 AM

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