#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <time.h>
void* fun(void *arg)
{
printf("fun start\n");
while(1)
{
time_t time_space;
struct tm *time_data;
// 获取当前时间并存储到 time_space 中
time(&time_space);
// 使用 localtime 函数将 time_space 转换为本地时间
time_data = localtime(&time_space);
printf("%02d:%02d:%02d\n", time_data->tm_hour, time_data->tm_min, time_data->tm_sec);
char *str = ctime(&time_space);
char *token = strstr(str, ":");
token[-3] ='\0';
printf("%s\n", str);
sleep(1);
}
}
int main()
{
pthread_t id;
//创建函数线程,并且指定函数线程要执行的函数
pthread_create(&id,NULL,fun,NULL);
pthread_detach(id);
getchar();
}
c语言读取linux系统时间并打印
最新推荐文章于 2024-05-28 16:55:54 发布