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; //日光节约时间的状态 
}; 
3、示例

#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>

int main(){
    struct timeval tv;
    gettimeofday(&tv,NULL);
    printf("second:%ld\n",tv.tv_sec);  //秒
    printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000);  //毫秒
    printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec);  //微秒

    sleep(3); // 为方便观看,让程序睡三秒后对比
    std::cout << "3s later:" << std::endl;

    gettimeofday(&tv,NULL);
    printf("second:%ld\n",tv.tv_sec);  //秒
    printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000);  //毫秒
    printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec);  //微秒
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

运行结果: 
second:1467523986 
millisecond:1467523986800 
microsecond:1467523986800434 
3s later: 
second:1467523989 
millisecond:1467523989800 
microsecond:1467523989800697

4、附 
一秒等于1000毫秒 
一秒等于1000000微秒 
一秒等于1000000000纳秒


<think> 我们参考用户提供的引用问题,用户希望在Linux环境下用C语言获取当前时刻的毫秒微秒。 根据引用[1]引用[2],我们可以使用`gettimeofday`函数获取精确到微秒时间。该函数在`sys/time.h`头文件中声明。 此外,引用[3]提到了使用`ftime`函数,但该方法在Linux中并不推荐,且精度可能不如`gettimeofday`。 因此,推荐使用`gettimeofday`函数,该函数可以获取到从1970-01-01 00:00:00 UTC(Epoch)至今的秒数微秒数。 然后,我们可以使用`localtime`或`localtime_r`函数将秒数转换为本地时间的结构体`struct tm`,但注意`struct tm`并不包含毫秒微秒部分,所以我们需要单独处理微秒部分。 步骤: 1. 包含头文件:`#include <sys/time.h>` `#include <time.h>` 2. 调用`gettimeofday`获取时间,该函数时间存储在`struct timeval`结构中,该结构有两个成员:`tv_sec`(秒)`tv_usec`(微秒)。 3. 将`tv_sec`转换为本地时间结构体`struct tm`,可以使用`localtime`函数(注意:`localtime`不是线程安全的),或者使用线程安全的`localtime_r`。 4. 格式化输出时间,包括毫秒微秒除以1000)微秒。 注意:毫秒微秒除以1000,所以我们可以同时获取毫秒微秒。 下面是一个示例程序: </think> 在 Linux 环境下使用 C 语言获取当前时刻的毫秒微秒,推荐使用 `gettimeofday()` 系统调用(精度达微秒级)或 `clock_gettime()`(精度达纳秒级)。以下是具体实现方法: ### 方法一:使用 `gettimeofday()`(微秒级) ```c #include <stdio.h> #include <sys/time.h> #include <time.h> int main() { struct timeval tv; gettimeofday(&tv, NULL); // 获取当前时间 // 提取秒、毫秒微秒 time_t seconds = tv.tv_sec; long milliseconds = tv.tv_usec / 1000; // 微秒毫秒 long microseconds = tv.tv_usec; // 微秒值 // 转换为可读格式(本地时间) struct tm *timeinfo = localtime(&seconds); char buffer[80]; strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo); // 输出完整时间(含毫秒/微秒) printf("当前时间: %s.%03ld (毫秒)\n", buffer, milliseconds); printf("当前时间: %s.%06ld (微秒)\n", buffer, microseconds); return 0; } ``` ### 方法二:使用 `clock_gettime()`(纳秒级,更高精度) ```c #include <stdio.h> #include <time.h> int main() { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); // 获取系统实时时间 // 提取秒、毫秒微秒 time_t seconds = ts.tv_sec; long milliseconds = ts.tv_nsec / 1000000; // 纳秒→毫秒 long microseconds = ts.tv_nsec / 1000; // 纳秒→微秒 // 转换为可读格式(本地时间) struct tm *timeinfo = localtime(&seconds); char buffer[80]; strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo); // 输出完整时间 printf("当前时间: %s.%03ld (毫秒)\n", buffer, milliseconds); printf("当前时间: %s.%06ld (微秒)\n", buffer, microseconds); return 0; } ``` ### 关键说明: 1. **精度对比**: - `gettimeofday()`:微秒级(`struct timeval`) - `clock_gettime()`:纳秒级(`struct timespec`),精度更高 2. **时间结构**: ```c // gettimeofday() struct timeval { time_t tv_sec; // 秒 suseconds_t tv_usec; // 微秒 }; // clock_gettime() struct timespec { time_t tv_sec; // 秒 long tv_nsec; // 纳秒 }; ``` 3. **编译选项**: - 使用 `clock_gettime()` 需链接 `librt` 库: ```bash gcc program.c -o program -lrt ``` ### 注意事项: - `localtime()` 函数用于将秒数转换为本地时间格式,但**不包含毫秒/微秒**,需额外拼接[^1]。 - 对于时间间隔测量(如性能分析),建议直接使用原始微秒/纳秒数值计算差值[^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值