Frage

Ich bin mit C ++ unter Linux mit Standard-GCC kompilieren. In meinem Programm möchte ich eine einfache Uhr hinzufügen HH zeigt: MM: SS. Was ist der einfachste Weg, das zu tun?

War es hilfreich?

Lösung

Eine gute Möglichkeit ist localtime

Andere Tipps

Sie können von localtime zusammen mit strftime .

Arbeits Link

Meine quick-and-dirty-Lösung:

// 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;
}

Das tut auch Zero-Padding (die Sie wahrscheinlich wollen).

#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;
}

Die einfachste Weg?

system("date +%T");

bei getTimeStamp() Schauen einstellen kann diese in ein beliebiges Format Sie wollen

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top