c获取时间

1. 获取时间戳gettimeofday(struct  timeval*tv,struct  timezone *tz)

2. time(time_t *t)获取日期

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

int main() {
    // timestamp
    struct timeval timeval;
    gettimeofday(&timeval, NULL);
    printf("millsec:%d\n", timeval.tv_sec * 1000 + timeval.tv_usec / 1000);
    sleep(1);
    gettimeofday(&timeval, NULL);
    printf("millsec:%d\n", timeval.tv_sec * 1000 + timeval.tv_usec / 1000);

    // date
    time_t  seconds;
    struct tm *date;
    time(&seconds);
    date = gmtime(&seconds);

    printf("sec:%d\n", date->tm_sec);
    printf("min:%d\n", date->tm_min);
    printf("hour:%d\n", date->tm_hour);

    printf("day:%d\n", date->tm_mday);
    printf("month:%d\n", date->tm_mon);
    printf("year:%d\n", date->tm_year + 1900);

    return 0;
}

  

posted on 2020-02-07 21:44  luckygxf  阅读(590)  评论(0编辑  收藏  举报

导航