T T
T T

Reputation: 27

How to display real time in c++

Can some one tell me how do i display real time in c++. what i mean is that while the program is running you can see the seconds and or minutes counting down like a real clock hanging on the wall

this is what I have:

int main ()
{
  time_t rawtime; //creates and object of the built in time function
  struct tm * timeinfo; //no idea what this do

  time ( &rawtime ); //gets the time from the computer
  timeinfo = localtime ( &rawtime ); //store that time here

  //it displays current date and time except time is frozen and not real time
  cout<< "Current local time and date: "<<asctime (timeinfo)<< endl; 


  system("pause");
  return 0;

}

Upvotes: 1

Views: 22166

Answers (6)

c0d3r
c0d3r

Reputation: 1

Below is the a function within a program of mine that displays the current day of week, time (hh:mm) and date (dd/mm/yyy). When I used the SYSTEMTIME struct, I realized the time displayed was four hours too fast, so I resorted to this method. Hope this helps. Intended for Windows users...

void time()
{
    cout << "The current date is: ";
    system("date/t");
    cout << "The current time is: ";
    system("time/t");
    cout << "Time zone: ";
    system("tzutil /g");
    cout << endl;
}

NOTE: This works by querying your system for the date, time, and timezone. Most seasoned programmers do not recommend utilizing the system() tool, but that is ultimately up to you.

Upvotes: 0

Saumya shekhar
Saumya shekhar

Reputation: 11

Add a system("cls");

Like this:

 time_t rawtime;
 struct tm* timeinfo;
 while(true)
 {
 system("cls");
 time(&rawtime);
 timeinfo=localtime(&rawtime);
 cout<<"Time : "<<asctime(timeinfo);
 Sleep(1000);
 }

Upvotes: 1

The standard C++ language did not have any notion of time till the latest C++11 standard published in 2011, and rarely implemented. On Linux, you could consider using GCC 4.6 or 4.7 which implements most of it.

(older C++03 gives you <ctime>)

Otherwise, time is given by operating system specific libraries and system calls (such as gettimeofday and clock_gettime on Linux and Posix)

If you have a fully C++11 conformant implementation of the latest C++ standard (which may be unlikely, in particular on Windows), you could use the <chrono> standard header.

Upvotes: 0

SChepurin
SChepurin

Reputation: 1854

Not in C++ (in C/Win32) but works.

#include <stdio.h>
#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
    SYSTEMTIME stime;   //structure to store system time (in usual time format)
    FILETIME ltime;     //structure to store local time (local time in 64 bits)
    FILETIME ftTimeStamp;
    char TimeStamp[256];//to store TimeStamp information
    while (true){
    ////Prepare data needed to output the time stamp:
    GetSystemTimeAsFileTime(&ftTimeStamp); // Gets the current system time
    FileTimeToLocalFileTime (&ftTimeStamp,&ltime);//convert in local time and store in ltime
    FileTimeToSystemTime(&ltime,&stime);//convert in system time and store in stime

    sprintf(TimeStamp, "%d:%d:%d, %d.%d.%d \r",stime.wHour,stime.wMinute,stime.wSecond, stime.wDay,stime.wMonth,stime.wYear);
    printf(TimeStamp);

    Sleep(1000);
    }

    system("pause");
    return 0;
} 

Upvotes: 3

John Zwinck
John Zwinck

Reputation: 249502

Try this:

while (true) {
    std::cout << '\r'; // return to the beginning of the line
    getAndPrintTime(); // do what you do now, but don't write endl
}

Assuming that you want to keep overwriting the same place in the terminal, two simple things to use are '\r' for carriage return, and '\b' for backspace (if you want to back up a character rather than a whole line).

Upvotes: 1

RvdK
RvdK

Reputation: 19800

Some basic C++ will come in a long way: www.cplusplus.com

int main ()
{
    time_t rawtime; //creates and object of the built in time function
    struct tm * timeinfo; //no idea what this do

    while (true)
    {
        time( &rawtime ); //gets the time from the computer
        timeinfo = localtime( &rawtime ); //store that time here

        //it displays current date and time except time is frozen and not real time
        cout<< "Current local time and date: "<<asctime (timeinfo)<< endl; 

        sleep(1000); //1 second sleep
    }
    system("pause");
    return 0;
}

Upvotes: 1

Related Questions