C 时区timezone

作者: Jesson3264 | 来源:发表于2021-05-08 10:12 被阅读0次
void test_time()
{
    time_t time_utc;
    struct tm tm_local;

    // Get the UTC time
    time(&time_utc);

    // Get the local time
    // Use localtime_r for threads safe
    localtime_r(&time_utc, &tm_local);

    time_t time_local;
    struct tm tm_gmt;

    // Change tm to time_t
    time_local = mktime(&tm_local);

    // Change it to GMT tm
    gmtime_r(&time_utc, &tm_gmt);

    int time_zone = tm_local.tm_hour - tm_gmt.tm_hour;
    if (time_zone < -12) {
        time_zone += 24;
    } else if (time_zone > 12) {
        time_zone -= 24;
    }

    char cur_time[256];
    strftime (cur_time,256, " %B %A %Y年%m月%d日  %H时%M分%S秒   %c  %x %X", &tm_local);
    printf("LOCAL TIME  :%s\n", cur_time);

    strftime(cur_time,256, " %B %A %Y年%m月%d日  %H时%M分%S秒   %c  %x %X", &tm_gmt);
    printf("GMT TIME    :%s\n", cur_time);

    printf("Your time zone is +%d. (- is west, + is east)\n", time_zone);
}

相关文章

网友评论

    本文标题:C 时区timezone

    本文链接:https://www.haomeiwen.com/subject/juxudltx.html