# C++读取本地时间，用数码形式输出

- URL: https://www.yidoo.xyz/cpp-reads-the-local-time-in-digital-form-output
- 作者: 核动力蜗牛
- 发布时间: 2010-03-07
- 更新时间: 2010-03-07
- 分类: 技术笔记
- 标签: cpp

这个要是用 Qt 或者 MFC 等图形库估计很容易就实现了，可是用字符界面还是很麻烦的，具体看代码

```cpp
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
string num[5][11] = {"****", " *", "****", "****", "* *", "****", "****", "****", "****", "****", " ",
 "* *", " *", " *", " *", "* *", "* ", "* ", " *", "* *", "* *", " * ",
 "* *", " *", "****", "****", "****", "****", "****", " *", "****", "****", " ",
 "* *", " *", "* ", " *", " *", " *", "* *", " *", "* *", " *", " * ",
 "****", " *", "****", "****", " *", "****", "****", " *", "****", "****", " "};

int main()
{
	int i,j;
	/*for(i= 0;i <a href="http://grandruby.co.uk/tips-on-choosing-the-best-poker-rooms">online casinos</a>  < 10; i ) {
		for(j = 0; j < 10; j ) {
			cout << num[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl << endl;*/

	time_t t= time(0);
	tm *curtime = localtime(&t);

	int hour = curtime->tm_hour;
	int min = curtime->tm_min;
	int sec = curtime->tm_sec;
	cout << hour << ":" << min << ":" << sec << endl;
	cout << endl;

	int timeArray[8];
	timeArray[0] = hour / 10;
	timeArray[1] = hour % 10;
	timeArray[2] = 10;
	timeArray[3] = min / 10;
	timeArray[4] = min % 10;
	timeArray[5] = 10;
	timeArray[6] = sec / 10;
	timeArray[7] = sec % 10;

	for(i = 0; i < 5; i ) {
		for(j = 0; j < 8; j ) {
			cout << num[i][timeArray[j]] << " ";
		}
		cout << endl;
	}
	return 0;
}
```

这里用到两个函数 time 和 localtime，两个函数包含与 time.h 里面
time_t time( time_t *time );
功能： 函数返回当前时间，如果发生错误返回零。如果给定参数 time ，那么当前时间存储到参数 time 中。
struct tm *gmtime( const time_t \*time );
功能：函数返回给定的统一世界时间（通常是格林威治时间），如果系统不支持统一世界时间系统返回 NULL。

PS：C 语言果然博大精深....
