سؤال

أنا أستخدم C ++ ضمن تجميع Linux مع GCC القياسية. في برنامجي ، أريد إضافة ساعة بسيطة تعرض HH: MM: SS. ما هي أسهل طريقة للقيام بذلك؟

هل كانت مفيدة؟

المحلول

طريقة جيدة للاستخدام الوقت المحلي

نصائح أخرى

يمكنك الاستفادة من الوقت المحلي جنبا إلى جنب مع strftime.

رابط العمل

حلي السريع والدوري:

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

هذا أيضًا لا صفر (والذي ربما تريده).

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

ال أسهل طريق؟

system("date +%T");

ينظر الى getTimeStamp() يمكنك ضبط هذا على أي تنسيق تريده

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top