打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux时间函数总结,特别有无时区转换
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(int argc, char * argv [ ])
{
    // time_t time(time_t T) 
    // 返回1970年1月1日00:00:00,到现在的秒数,即UTC时间
    // 参数不为NULL时,将T设为与返回值一样的值。

    time_t T1,T2;
    T1 = time(&T2);
    printf("%d\n",T1);
    printf("%d\n",T2);
///----------------------------------------------------------------------

    unsigned long Time;
    struct timeval tv;
struct timezone tz;
gettimeofday( &tv, &tz);
    /// 本地时间:秒单位
Time = tv.tv_sec - tz.tz_minuteswest * 60; 
    printf("%lu\n",Time);
    /// 本地时间:毫秒
    Time = tv.tv_sec * 1000 - tz.tz_minuteswest * 60 * 1000 + tv.tv_usec / 1000;
    printf("%lu\n",Time);
///----------------------------------------------------------------------
     
    // struct tm *localtime(time_t *T)
    // 将UTC时间到现在的秒数T,转换成本地时间的结构体struct tm
    // 有时区转换,加时区
    struct tm *ps_tm;
    ps_tm = localtime(&T1);
    printf("%d-%d-%d_%d:%d:%d\n",ps_tm->tm_year+1900,ps_tm->tm_mon+1,
        ps_tm->tm_mday,ps_tm->tm_hour,ps_tm->tm_min,ps_tm->tm_sec);

    // localtime_r(T,&s_tm);
    // struct tm *localtime_r(const time_t *timer, struct tm *result);
    // 功能与localtime一样,但是可重入,前者不可。
    struct tm s_tm;
    localtime_r(&T1,&s_tm);
    printf("%d-%d-%d_%d:%d:%d\n",s_tm.tm_year+1900,s_tm.tm_mon+1,
        s_tm.tm_mday,s_tm.tm_hour,s_tm.tm_min,s_tm.tm_sec);
///----------------------------------------------------------------------
     
    // mktime***有时区转换,减时区
    // time_t mktime(struct tm * timeptr);
    // 将struct tm结构体转换成UTC时间到现在的秒数
    time_t Tmktime = mktime(&s_tm);
    printf("mktime:%d\n",Tmktime);

///----------------------------------------------------------------------
         
    // gmtime***无时区转换
    // struct tm *gmtime(const time_t *time); 
    // 将time转换成struct tm结构体
   
    ps_tm = gmtime(&T1);
    printf("%d-%d-%d_%d:%d:%d\n",ps_tm->tm_year+1900,ps_tm->tm_mon+1,
        ps_tm->tm_mday,ps_tm->tm_hour,ps_tm->tm_min,ps_tm->tm_sec);

///----------------------------------------------------------------------
      
    // ctime***有时区转换,UTC加时区
    // char *ctime(const time_t *time);
    // 将UTC时间的秒数,转换成本地时间的年月日字符串
    char *Tstr=NULL;
    Tstr = ctime(&T1);
    printf("ctime:%s\n",Tstr);

///----------------------------------------------------------------------
     
    // asctime***无时区转换
    // char* asctime(const tm*)
    // 把struct tm结构体中储存的时间转换为字符串格式返回
    Tstr = asctime(&s_tm);
    printf("asctime:%s\n",Tstr);
    
    return 0;
    
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux C函数之时间函数
python工具模块介绍-time 时间访问和转换!
(一)linux下的定时或计时操作(gettimeofday等的用法,秒,微妙,纳秒)_鲍...
c语言中时间的获取
Linux常用C函数-日期时间篇 -
Linux获取精确日历函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服