C++ to achieve the current time dynamic display method

  • 2020-04-02 03:10:21
  • OfStack

This paper illustrates the method of dynamic display of current time in C++. Share with you for your reference. The details are as follows:



//struct tm {
//    int tm_sec;   
//    int tm_min;   
//    int tm_hour;  
//    int tm_mday;  
//    int tm_mon;   
//    int tm_year;  
//    int tm_wday;  
//    int tm_yday;  
//    int tm_isdst;  
//   }; 
#include <iostream> 
#include <time.h> 
#include "dos.h" 
#include <windows.h> 
using namespace std;
int main()
{
  char *myweek[]={" day "," one "," two "," three "," four "," five "," six "};
  time_t nowtime;  //Typedef long time_t; In the header file defined by the compiler
  nowtime = time(NULL);  //Gets the current time which is represented by a long reconstructive
  struct tm *local; 
  local = localtime(&nowtime); //Gets the current system clock
  while (1)
  {
     cout<<" Current time: "; 
     cout<<local->tm_year+1900<<" years "<<local->tm_mon+1<<" month "<<local->tm_mday<<" day "<<" "; 
     cout<<local->tm_hour<<" when "<<local->tm_min<<" points "<<local->tm_sec<<" seconds "<<" ";
     cout<<" week "<<myweek[local->tm_wday]<<endl;
     
     if(local->tm_sec==59 && local->tm_min!=59)
     //When the seconds go to 59, and before the minutes go to 59, the minutes add 1, and the seconds clear 0
     {
        local->tm_min++; 
        local->tm_sec=0;
     } 
     //When both seconds and minutes are 59 instead of 23, both seconds and minutes are zero, and the clock is one
      else if(local->tm_sec==59 && local->tm_min==59 && local->tm_hour!=23) 
      {
        local->tm_min=0; 
        local->tm_sec=0;
        local->tm_hour++;
      } 
      //When the seconds and minutes are 59 and the minutes are 23, the seconds, minutes and clock are all clear to 0
      else if(local->tm_sec==59&&local->tm_min==59&&local->tm_hour==23) 
      {
        local->tm_sec=0; 
        local->tm_min=0; 
        local->tm_hour=0;
      } 
      else //In other cases, you keep adding 1 and 1 and 1
      {
        local->tm_sec++; 
      }
      Sleep(1000); /*Sleep() In milliseconds, 
      system("cls");  
  } 
  system("pause");
  return 0;
} 

Hope that the article described in the C++ programming to help you.


Related articles: