Linux C 如何获取get请求的具体时间日期
时间: 2024-02-12 18:02:34 浏览: 20
Linux C 获取 GET 请求的具体时间日期,可以使用 localtime_r 函数和 strftime 函数。localtime_r 函数可以将时间戳转换为本地时间,strftime 函数可以将本地时间格式化为字符串。下面是一个简单的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
int main()
{
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
// 这里放置GET请求的代码
gettimeofday(&end_time, NULL);
long long start_sec = start_time.tv_sec;
struct tm start_tm;
localtime_r(&start_sec, &start_tm);
char start_time_str[20];
strftime(start_time_str, sizeof(start_time_str), "%Y-%m-%d %H:%M:%S", &start_tm);
printf("Start time: %s\n", start_time_str);
long long end_sec = end_time.tv_sec;
struct tm end_tm;
localtime_r(&end_sec, &end_tm);
char end_time_str[20];
strftime(end_time_str, sizeof(end_time_str), "%Y-%m-%d %H:%M:%S", &end_tm);
printf("End time: %s\n", end_time_str);
return 0;
}
```
在这个例子中,我们用 gettimeofday 函数获取了 GET 请求的开始时间和结束时间,并将其转换为本地时间,然后用 strftime 函数将本地时间格式化为字符串。需要注意的是,strftime 函数的第一个参数是目标字符串缓冲区,第二个参数是缓冲区大小,第三个参数是格式化字符串,第四个参数是本地时间结构体。