程式語言 - GNU - C/C++ - Time



參考資料:
https://man7.org/linux/man-pages/man7/signal.7.html
https://man7.org/linux/man-pages/man2/timer_create.2.html
https://man7.org/linux/man-pages/man2/timerfd_create.2.html
https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
https://stackoverflow.com/questions/65792258/clock-nanosleep-it-ignores-timespec-structure
https://stackoverflow.com/questions/69099750/what-is-the-precision-of-the-gettimeofday-function

取得目前時間說明
timetime_t (second)
ftimestruct timeb (millisecond)
gettimeofdaystruct timeval (microsecond)
clock_gettimestruct timespec (nanosecond)

等待一段時間
sleep
alarm
usleep
nanosleep
clock_nanosleep
getitimer / setitimer
timer_create / timer_settime / timer_gettime / timer_delete
timerfd_create / timerfd_gettime / timerfd_settime

time

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    time_t t = time(NULL);
    struct tm *now = localtime(&t);

    printf("%d/%d/%d %d:%d:%d\n",
        now->tm_year + 1900, now->tm_mon + 1, now->tm_mday,
        now->tm_hour, now->tm_min, now->tm_sec);
    return 0;
}

ftime

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/timeb.h>
 
int main(int argc, char **argv)
{
    struct timeb t;
 
    ftime(&t);
    printf("%ld(s), %d(ms)\n", t.time, t.millitm);
    return 0;
}

gettimeofday

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>

int main(int argc, char **argv)
{
    struct timeval t;
    struct timezone tz;

    gettimeofday(&t, &tz);
    printf("%ld(s), %ld(us)\n", t.tv_sec, t.tv_usec);
    return 0;
}

clock_gettime

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    struct timespec t;

    clock_gettime(CLOCK_REALTIME, &t);
    printf("%ld(s), %ld(ns)\n", t.tv_sec, t.tv_nsec);
    return 0;
}

sleep、alarm

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>

void handle_signal(int sig)
{
    printf("%s\n", __func__);
}

int main(int argc, char **argv)
{
    signal(SIGALRM, handle_signal);
    alarm(3);
    pause();

    sleep(3);
    return 0;
}

usleep、nanosleep

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>

int main(int argc, char **argv)
{
    struct timespec req = {0};
    struct timespec rem = {0};

    usleep(3000000);

    req.tv_sec = 3;
    req.tv_nsec = 0;
    nanosleep(&req, &rem);
    return 0;
}

P.S. 依據man文件描述,usleep的輸入參數必須小於1000000

clock_nanosleep

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
 
int main(int argc, char **argv)
{
    struct timespec req = {0};
    struct timespec rem = {0};

    req.tv_sec = 3;
    req.tv_nsec = 0;
    clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem);
    return 0;
}

setitimer

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>

void handle_signal(int sig)
{
    printf("%s\n", __func__);
}

int main(int argc, char **argv)
{
    struct itimerval t = {0};

    signal(SIGALRM, handle_signal);

    t.it_value.tv_sec = 3;
    setitimer(ITIMER_REAL, &t, NULL);
    pause();
    return 0;
}

timer_create

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/timerfd.h>

void handle_signal(int sig)
{
    printf("%s\n", __func__);
}

int main(int argc, char **argv)
{
    timer_t fd = 0;
    struct sigevent ev = {0};
    struct  itimerspec ts = {0};

    ev.sigev_value.sival_ptr = &fd;
    ev.sigev_notify = SIGEV_SIGNAL;
    ev.sigev_signo = SIGALRM;
    signal(SIGALRM, handle_signal);

    timer_create(CLOCK_REALTIME, &ev, &fd);

    ts.it_value.tv_sec = 3;
    timer_settime(fd, 0, &ts, NULL);
    pause();
    timer_delete(&fd);
    return 0;
}

timerfd_create

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/timerfd.h>

int main(int argc, char **argv)
{
    uint64_t exp = 0;
    struct timespec now = {0};
    struct itimerspec t = {0};
    int fd = timerfd_create(CLOCK_REALTIME, 0);

    clock_gettime(CLOCK_REALTIME, &now);

    t.it_value.tv_sec = now.tv_sec + 3;
    timerfd_settime(fd, TFD_TIMER_ABSTIME, &t, NULL);

    read(fd, &exp, sizeof(uint64_t));
    printf("expired %ld\n", exp);
    return 0;
}