C++时钟-系统时间
Posted srx-0816
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++时钟-系统时间相关的知识,希望对你有一定的参考价值。
1 # include <iostream> 2 # include <windows.h> 3 # include <time.h> 4 # include <iomanip> 5 # define BUF 255 6 7 using namespace std; 8 9 class Time 10 11 private: 12 int hour,minute,second; 13 public: 14 Time():hour(0),minute(0),second(0) 15 Time(int h,int m,int s):hour(h),minute(m),second(s) 16 17 //成员函数 前++重载 18 /*Time operator++() 19 20 this->second+=1; 21 if(this->second==60) 22 23 this->second=0; 24 this->minute++; 25 26 if(this->minute==60) 27 28 this->minute=0; 29 this->hour++; 30 31 if(this->hour==24) 32 33 this->hour=0; 34 35 */ 36 37 //友元函数 前++重载 38 friend operator++(Time &time1) 39 40 time1.second+=1; 41 if(time1.second==60) 42 43 time1.second=0; 44 time1.minute++; 45 46 if(time1.minute==60) 47 48 time1.minute=0; 49 time1.hour++; 50 51 if(time1.hour==24) 52 53 time1.hour=0; 54 55 56 57 //成员函数 后++重载 58 /*Time operator++(int) 59 60 this->second+=1; 61 if(this->second==60) 62 63 this->second=0; 64 this->minute++; 65 66 if(this->minute==60) 67 68 this->minute=0; 69 this->hour++; 70 71 if(this->hour==24) 72 73 this->hour=0; 74 75 */ 76 77 //友元函数 后++重载 78 friend operator++(Time &time1,int) 79 80 time1.second+=1; 81 if(time1.second==60) 82 83 time1.second=0; 84 time1.minute++; 85 86 if(time1.minute==60) 87 88 time1.minute=0; 89 time1.hour++; 90 91 if(time1.hour==24) 92 93 time1.hour=0; 94 95 96 //重载流插入 <<运算符 97 /*friend ostream &operator<<(ostream &out, const Time &time1) 98 99 out <<setw(2)<<time1.hour<< " : " <<setw(2)<<time1.minute<< " : " <<setw(2)<<time1.second; 100 return out; 101 */ 102 void show() 103 104 time_t t = time( 0 ); 105 char tmpBuf[BUF]; 106 strftime(tmpBuf, BUF, "%Y-%m-%d %H:%M:%S %A", localtime(&t)); 107 printf("time is [%s]",tmpBuf); 108 109 ; 110 111 int main() 112 113 int hour,minute,second; 114 //获取系统时间 115 time_t time2; 116 struct tm *p; 117 time (&time2); 118 p=gmtime(&time2); 119 hour=8+p->tm_hour; 120 minute=p->tm_min; 121 second=p->tm_sec; 122 Time time1(hour,minute,second); 123 while(1) 124 125 time1.show(); 126 Sleep(1000); 127 //++time1; 128 time1++; 129 system("CLS"); 130 131 return 0; 132
以上是关于C++时钟-系统时间的主要内容,如果未能解决你的问题,请参考以下文章
Linux系统时间硬件时间(datetzselectclockhwclockntpdate)