目录
获取自1970-01-01 00:00:00到此刻的秒数:time_t time(time_t *tloc);
获取自1970-01-01 00:00:00到此刻的秒数和微秒数:int gettimeofday(struct timeval *tv, struct timezone *tz);
获取自1970-01-01 00:00:00到此刻的秒数和纳秒数,一些进程、线程的运行时间int clock_gettime(clockid_t, struct timespec *)
将自1970-01-01 00:00:00到此刻的秒数转换成数据并存放到tm结构体struct tm *localtime(const time_t * timep);
将自1970-01-01 00:00:00到此刻的秒数转换成日历的时间表示方式char *ctime(const time_t *timep);
时间类结构体
时间类结构体,包含微秒struct timeval{}
时间类结构体,包含纳秒struct timespec{}
定时器结构体struct itimerval{}
日历时间结构体struct tm{}
时间类函数
获取自1970-01-01 00:00:00到此刻的秒数:time_t time(time_t *tloc);
参数:
- time_t *tloc:
time_t类型变量的地址,用来保存1970-01-01 00:00:00到此刻的秒数,一般传NULL,用返回值接收1970-01-01 00:00:00到此刻的秒数
返回值:
1970-01-01 00:00:00到此刻的秒数
#include <stdio.h>
#include <time.h>
int main()
{
time_t nowTime;
time_t str;
nowTime = time(&str);
if(nowTime < 0){
perror("time");
}else{
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,nowTime\n",nowTime);
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,str\n",str);
}
}
获取自1970-01-01 00:00:00到此刻的秒数和微秒数:int gettimeofday(struct timeval *tv, struct timezone *tz);
参数:
- struct timeval *tv:
timeval类型结构体地址,用来保存1970-01-01 00:00:00到此刻的秒数和微秒数 - stuct timezone *tz:
|timezone类型结构体地址,用来保存时区,和时间的修正方式,一般传NULL,不关心
返回值:
调用成调用成功返回0,调用失败返回-1
#include <stdio.h>
#include <sys/time.h>
int main()
{
struct timeval nowTime;
if(gettimeofday(&nowTime,NULL) < 0){
perror("gettimeofday");
}else{
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_sec);
printf("microsecond from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_usec);
}
return 0;
}
获取自1970-01-01 00:00:00到此刻的秒数和纳秒数,一些进程、线程的运行时间int clock_gettime(clockid_t, struct timespec *)
参数:
- clockid_t:
CLOCK_REALTIME 获取自1970-01-01 00:00:00到此刻的秒数和纳秒数
CLOCK_MONOTONIC 系统的启动时间,不能被设置
CLOCK_PROCESS_CPUTIME_ID 本进程运行时间
CLOCK_THREAD_CPUTIME_ID 本线程运行时间 - struct timespec *:
保存该函数结果的秒数和纳秒数
返回值:
调用成调用成功返回0,调用失败返回-1
#include <stdio.h>
#include <time.h>
int main()
{
struct timespec nowTime;
if(clock_gettime(CLOCK_REALTIME,&nowTime) < 0){
perror("clock_gettime");
}else{
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_sec);
printf("nanosecond from 1970-01-01 00:00:00 to this moment is %ld\n",nowTime.tv_nsec);
}
return 0;
}
将自1970-01-01 00:00:00到此刻的秒数转换成数据并存放到tm结构体struct tm *localtime(const time_t * timep);
参数:
- const time_t * timep:
time_t类型变量的地址,自1970-01-01 00:00:00到此刻的秒数
返回值:
成功tm类型结构体地址,保存转换后的数据的结构体地址,失败返回NULL
#include <stdio.h>
#include <time.h>
int main()
{
time_t nowTime;
time_t str;
nowTime = time(&str);
if(nowTime < 0){
perror("time");
}else{
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,nowTime\n",nowTime);
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,str\n",str);
}
struct tm *my_tm;
my_tm = localtime(&nowTime);
printf("now year is %d\n",my_tm->tm_year+1900);
printf("now month is %d\n",my_tm->tm_mon+1);
printf("now day is %d\n",my_tm->tm_mday);
printf("now hour is %d\n",my_tm->tm_hour);
printf("now minute is %d\n",my_tm->tm_min);
printf("now second is %d\n",my_tm->tm_sec);
return 0;
}
将自1970-01-01 00:00:00到此刻的秒数转换成日历的时间表示方式char *ctime(const time_t *timep);
参数:
- const time_t *timep:
time_t类型变量的地址,自1970-01-01 00:00:00到此刻的秒数
返回值:
返回一个char类型地址,保存该日历时间的字符串地址
#include <stdio.h>
#include <time.h>
int main()
{
time_t nowTime;
time_t str;
nowTime = time(&str);
if(nowTime < 0){
perror("time");
}else{
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,nowTime\n",nowTime);
printf("seconds from 1970-01-01 00:00:00 to this moment is %ld,str\n",str);
}
char *my_time;
my_time = ctime(&nowTime);
printf("time is %s",my_time);
return 0;
}
Linux时间实际运用
Linux设定定时器定时方式函数int setitimer(int which, const struct itimerval *new_value,struct itimerval *old_value);
参数:
- int which:
- const struct itimerval *new_value:
itimerval类型结构体地址,用来设置定时时间和定时器开启时间 - struct itimerval *old_value:
itimerval类型结构体地址,用来保存定时器原有的值
返回值:
调用成功返回0,调用失败返回-1
Linux定时器:
每1s钟打印一次hello
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>
static int i;
void time_handle(int signum)
{
i++;
if(i == 2000){
printf("hello\n");
i = 0;
}
}
int main(int argc,char *argv[])
{
struct itimerval timer;
//设定启动定时器的时间
timer.it_value.tv_sec = 1;
timer.it_value.tv_usec = 0;
//设定定时时间
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 500; //0.5ms
//设定定时方式
if(setitimer(ITIMER_REAL,&timer,NULL) == -1){
perror("setitimer");
exit(-1);
} //SIGALRM
signal(SIGALRM,time_handle);
while(1);
return 0;
}