Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 388, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

linux 更改时区

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

注意必须联网,否则无效

posted on 2018-12-02 00:17 Benjamin 阅读(475) 评论(0)  编辑 收藏 引用 所属分类: C/C++linux


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理