zoukankan      html  css  js  c++  java
  • Linux: Linux C 获取当前系统时间的时间戳(精确到秒、毫秒、微秒) gettimeofday

    说明

    获取当前的时间的秒数和微秒数本方法需要用到 gettimeofday() 函数,该函数需要引入的头文件是  <sys/time.h>  。

    函数说明 int gettimeofday (struct timeval * tv, struct timezone * tz)

    1、返回值:该函数成功时返回0,失败时返回-1 
    2、参数 
    struct timeval{ 
      long tv_sec; //
      long tv_usec; //微秒 
    }; 
    struct timezone 
    { 
      int tz_minuteswest; //和Greenwich 时间差了多少分钟 
      int tz_dsttime; //日光节约时间的状态 
    }; 

    示例

    #include <sys/time.h>
    #include <stdio.h>
    #include <unistd.h>
    
    
    int main()
    {
        
        struct timeval tv;
        gettimeofday(&tv, NULL);
        
        printf("second: %ld
    ", tv.tv_sec); //
        printf("millisecond: %ld
    ", tv.tv_sec * 1000 + tv.tv_usec / 1000); // 毫秒
        printf("microsecond: %ld
    ", tv.tv_sec * 1000000 + tv.tv_usec); // 徽秒
        
        sleep(3); // 让程序休眠3秒
        printf("---------------------sleep 3 second-------------------
    ");
        
        gettimeofday(&tv, NULL);
            
        printf("second: %ld
    ", tv.tv_sec); //
        printf("millisecond: %ld
    ", tv.tv_sec * 1000 + tv.tv_usec / 1000); // 毫秒
        printf("microsecond: %ld
    ", tv.tv_sec * 1000000 + tv.tv_usec); // 徽秒
    
        return 0;
    }

    运行结果:

    second: 1554963664
    millisecond: 1554963664748
    microsecond: 1554963664748007
    ---------------------sleep 3 second-------------------
    second: 1554963667
    millisecond: 1554963667748
    microsecond: 1554963667748621

  • 相关阅读:
    python 连接操作mysql数据库
    (转)postfix疯狂外发垃圾邮件之分析与解决
    ansible 常用方法
    用python2.7.9 写个小程序搜索某个目录下行有某关键字
    python获取文件扩展名的方法(转)
    ceph 池管理
    UVALive 5412 Street Directions
    UVALive 3231 Fair Share
    UVA 11478 Halum
    2015 Multi-University Training Contest 4 hdu 5338 ZZX and Permutations
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/10689534.html
Copyright © 2011-2022 走看看