C-获取系统时间

1 获取本地时间

    time_t current_time;
    time(&current_time);
    struct tm *calendar_time = localtime(&current_time);
    PRINT_INT(calendar_time->tm_year);
    PRINT_INT(calendar_time->tm_mon);
    PRINT_INT(calendar_time->tm_mday);
    PRINT_INT(calendar_time->tm_hour);
    PRINT_INT(calendar_time->tm_min);
    PRINT_INT(calendar_time->tm_sec);

2 将Epoch 时间转换为日历时间

    calendar_time->tm_sec = 70;
    time_t current_time2 = mktime(calendar_time);
    PRINT_INT(calendar_time->tm_year);
    PRINT_INT(calendar_time->tm_mon);
    PRINT_INT(calendar_time->tm_mday);
    PRINT_INT(calendar_time->tm_hour);
    PRINT_INT(calendar_time->tm_min);
    PRINT_INT(calendar_time->tm_sec);
    PRINT_LONG(current_time2);

    time_t current_time2 = mktime(calendar_time);

3 获取格林威治时间

    struct tm *gmt = gmtime(&current_time);
    PRINT_INT(gmt->tm_year);
    PRINT_INT(gmt->tm_mon);
    PRINT_INT(gmt->tm_mday);
    PRINT_INT(gmt->tm_hour);
    PRINT_INT(gmt->tm_min);
    PRINT_INT(gmt->tm_sec);

4 将系统时间转换为可阅读的字符串

    puts(asctime(calendar_time));
    puts(ctime(&current_time));

5 自定义格式化日期字符串

    size_t size = strftime(current_time_s,20,"%Y-%m-%d %H:%M:%S",calendar_time);
//    size_t size = strftime(current_time_s,20,"%F %T",calendar_time);
//    size_t size = strftime(current_time_s, 20, "%Y%m%d%H%M%S", calendar_time);
    PRINT_INT(size);
    puts(current_time_s);

6 实现毫秒

    long_time_t current_time_in_ms = TimeInMillisecond();
    int current_time_millisencond = current_time_in_ms % 1000;


    size_t size1 = sprintf(current_time_s + 19, "%03d", current_time_millisencond);
    PRINT_INT(size1);
    puts(current_time_s);

7 完整代码


#include <io_utils.h>
#include <time_utils.h>
#include <time.h>

int main() {
    long_time_t current_time_in_ms = TimeInMillisecond();
    int current_time_millisencond = current_time_in_ms % 1000;
    time_t current_time;
    time(&current_time);
    struct tm *calendar_time = localtime(&current_time);
    PRINT_INT(calendar_time->tm_year);
    PRINT_INT(calendar_time->tm_mon);
    PRINT_INT(calendar_time->tm_mday);
    PRINT_INT(calendar_time->tm_hour);
    PRINT_INT(calendar_time->tm_min);
    PRINT_INT(calendar_time->tm_sec);

    calendar_time->tm_sec = 70;
    time_t current_time2 = mktime(calendar_time);
    PRINT_INT(calendar_time->tm_year);
    PRINT_INT(calendar_time->tm_mon);
    PRINT_INT(calendar_time->tm_mday);
    PRINT_INT(calendar_time->tm_hour);
    PRINT_INT(calendar_time->tm_min);
    PRINT_INT(calendar_time->tm_sec);
    PRINT_LONG(current_time2);

    struct tm *gmt = gmtime(&current_time);
    PRINT_INT(gmt->tm_year);
    PRINT_INT(gmt->tm_mon);
    PRINT_INT(gmt->tm_mday);
    PRINT_INT(gmt->tm_hour);
    PRINT_INT(gmt->tm_min);
    PRINT_INT(gmt->tm_sec);

    puts(asctime(calendar_time));
    puts(ctime(&current_time));
    char current_time_s[20];
    size_t size = strftime(current_time_s,20,"%Y-%m-%d %H:%M:%S",calendar_time);
//    size_t size = strftime(current_time_s,20,"%F %T",calendar_time);
//    size_t size = strftime(current_time_s, 20, "%Y%m%d%H%M%S", calendar_time);
    PRINT_INT(size);
    puts(current_time_s);
    size_t size1 = sprintf(current_time_s + 19, "%03d", current_time_millisencond);
    PRINT_INT(size1);
    puts(current_time_s);

    return 0;
}