2

我在使用标准 GCC 编译的 Linux 下使用 C++。在我的程序中,我想添加一个显示 HH:MM:SS 的简单时钟。最简单的方法是什么?

4

6 回答 6

5

您可以将localtimestrftime一起使用。

工作链接

于 2010-10-23T20:49:39.827 回答
4

一个好方法是使用本地时间

于 2010-10-23T20:46:14.477 回答
1

我的快速而肮脏的解决方案:

// file now.cc
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

int main()
{
    time_t ct = time(0);
    struct tm* currtime = localtime(&ct);
    cout << setfill('0') << setw(2) << currtime->tm_hour << ":"
         << setw(2) << currtime->tm_min << ":"
         << setw(2) << currtime->tm_sec << endl;
    return 0;
}

这也进行零填充(您可能想要)。

于 2010-11-19T07:03:07.060 回答
0

看看getTimeStamp()你可以把它调整成你想要的任何格式

于 2015-09-25T11:14:55.697 回答
0
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
int main()
{
    time_t myTime;

    while(1)
    {
        time(&myTime);
        printf("%s", asctime(gmtime(&myTime)));
        delay(1000);
        system("cls");
    }
    return 0;
}
于 2015-09-25T09:17:08.500 回答
0

简单的方法?

system("date +%T");
于 2015-09-25T11:12:07.197 回答