Linux获取系统当前时间(精确到毫秒)

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <time.h>  
  3. #include <sys/time.h>  
  4.   
  5. void sysLocalTime()  
  6. {  
  7.     time_t             timesec;  
  8.     struct tm         *p;  
  9.       
  10.       
  11.     time(×ec);  
  12.     p = localtime(×ec);  
  13.       
  14.     printf("%d%d%d%d%d%d\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);  
  15.       
  16. }  
  17.   
  18. void sysUsecTime()  
  19. {  
  20.     struct timeval    tv;  
  21.     struct timezone tz;  
  22.       
  23.     struct tm         *p;  
  24.       
  25.     gettimeofday(&tv, &tz);  
  26.     printf("tv_sec:%ld\n",tv.tv_sec);  
  27.     printf("tv_usec:%ld\n",tv.tv_usec);  
  28.     printf("tz_minuteswest:%d\n",tz.tz_minuteswest);  
  29.     printf("tz_dsttime:%d\n",tz.tz_dsttime);  
  30.       
  31.     p = localtime(&tv.tv_sec);  
  32.     printf("time_now:%d%d%d%d%d%d.%ld\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);  
  33. }  
  34.   
  35. int main(void)  
  36. {  
  37.     sysLocalTime();  
  38.     printf("============gettimeofday==============\n");  
  39.       
  40.     sysUsecTime();  
  41.       
  42.     return 0;  
  43. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值