Getting Windows system time in C++
Results 1 to 9 of 9

Thread: Getting Windows system time in C++

  1. #1
    Join Date
    Jul 2004

    Getting Windows system time in C++

    As the title says, how do you get the system time in C++ on Windows? And putting it into a variable would be the same as any other typecasting thing, right?
    Tell me if you think I\'m spamming or doing something stupid, please.

  2. #2

    these are both API's which can be used to get the time. they are different so i suggest you take a read through those links to determine which one suits your purpose

  3. #3
    Join Date
    Jun 2003
    You could always do a system call. And get the output from that.

  4. #4
    Join Date
    Sep 2004

  5. #5
    Join Date
    Jun 2003
    I wasn't asking how to, I was saying that she/he could.

  6. #6
    that's true, but it will be a lot harder to calculate with and the API's are a lot more powerful.

    this doesn't work under windows, since it WILL display the time, but at the same time asks you for the new time

  7. #7
    Senior Member
    Join Date
    Mar 2004

    I am wondering about that question. Doing a google using "Windows system time in C++"
    provides you with the links you need - fully documented. One interesting ingredient here is
    the remark about using the API's by White Scorpion.


    You can read the system time without being prompted for a new time using
    system("time /T"); but on depending what you want to do with the time, this is
    a bad option.

    high resolution

    If you wish to do high-resolution time measurements there is a possiblity
    to use the system clock. Around 10 years ago, this provided you with
    a resolution of 1/4096 secs per tic (or so...). Nowadays I don't know. Somewhere at
    home, I even might have a code of mine using this ( in Pascal/ASM, as far as I remember).

    time.h, timeb.h

    Here is a an excerpt that is slightly modified by myself of Times.C by the MSDN Library
    (publicly available [1])
    #include <time.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/timeb.h>
    #include <string.h>
    void main()
    // MSDN Library TIMES.C
        char tmpbuf[128], ampm[] = "AM";
        time_t ltime;
        struct _timeb tstruct;
        struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };
        /* Set time zone from TZ environment variable. If TZ is not set,
         * the operating system is queried to obtain the default value 
         * for the variable. 
        /* Display operating system-style date and time. */
        _strtime( tmpbuf );
        printf( "OS time:\t\t\t\t%s\n", tmpbuf );
        _strdate( tmpbuf );
        printf( "OS date:\t\t\t\t%s\n", tmpbuf );
        /* Get UNIX-style time and display as number and string. */
        time( &ltime );
        printf( "Time in seconds since UTC 1/1/70:\t%ld\n", ltime );
        printf( "UNIX time and date:\t\t\t%s", ctime( &ltime ) );
        /* Display UTC. */
        gmt = gmtime( &ltime );
        printf( "Coordinated universal time:\t\t%s", asctime( gmt ) );
        /* Convert to time structure and adjust for PM if necessary. */
        today = localtime( &ltime );
        if( today->tm_hour > 12 )
       strcpy( ampm, "PM" );
       today->tm_hour -= 12;
        if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
       today->tm_hour = 12;
        /* Note how pointer addition is used to skip the first 11 
         * characters and printf is used to trim off terminating 
         * characters.
        printf( "12-hour time:\t\t\t\t%.8s %s\n",
           asctime( today ) + 11, ampm );
        /* Print additional time information. */
        _ftime( &tstruct );
        printf( "Plus milliseconds:\t\t\t%u\n", tstruct.millitm );
        printf( "Zone difference in seconds from UTC:\t%u\n", 
                 tstruct.timezone );
        printf( "Time zone name:\t\t\t\t%s\n", _tzname[0] );
        printf( "Daylight savings:\t\t\t%s\n", 
                 tstruct.dstflag ? "YES" : "NO" );
        /* Make time for noon on Christmas, 1993. */
        if( mktime( &xmas ) != (time_t)-1 )
       printf( "Christmas\t\t\t\t%s\n", asctime( &xmas ) );
        /* Use time structure to build a customized time string. */
        today = localtime( &ltime );
        /* Use strftime to build a customized time string. */
        strftime( tmpbuf, 128,
             "Today is %A, day %d of %B in the year %Y.\n", today );
        printf( tmpbuf );
    I hope this answers your question fully and you can use 1-1 the examples.


    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  8. #8
    Junior Member
    Join Date
    Jul 2008

    Thumbs up EZTime c++ Header

    The EZTime header file filled all of my time and date needs. Also, the next version due out on 7/19/08 (<<That date format is availiable in the header) will include a ticking clock. Check it out, the 10 functions it include are completely self explainatory, I should know, as I wrote the file. Download the current version here :

  9. #9
    Just Another Geek
    Join Date
    Jul 2002
    Rotterdam, Netherlands
    It's a 4 year old thread... ^ spam?
    Oliver's Law:
    Experience is something you don't get until just after you need it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

 Security News


       Security Trends


           Buying Guides