Linux 获取系统时间、设置系统时间、定时器

http://blog.csdn.net/weihan0208/article/details/52686212

一、获取系统时间


函数:  time
头文件: time.h
函数原型:  time_t  time(time_t  *timer)
获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后调用localtime将time_t所表示的CUT时间转换为本地时间(我们是+8区,比CUT多8个小时)并转成struct tm类型,该类型的各数据成员分别表示年月日时分秒。


struct tm *localtime(const time_t *clock);

localtime返回类型为struct  tm类型的指针,如下为struct  tm的结构体,分别为时、分、秒、日、月、年、等

struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,
}


接下来干货代码分析:

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<time.h>  
  4.   
  5. int main()  
  6. {  
  7.     time_t cur_time;  
  8.     struct tm *local_time;  
  9.     //get current time;  
  10.     cur_time = time(NULL);  
  11.     local_time = localtime(&cur_time);  
  12.     printf("current time: Hour = %d Min = %d Sec = %d\n",local_time->tm_hour,local_time->tm_min,local_time->tm_sec);  
  13.     exit(0);  
  14. }  


二、设置系统时间

Linux下通过gettimeofday()获取系统当前的时间及地区信息,其中时间可以达到微秒级;

头文件:#include<sys/time.h>
函数原型:int gettimeofday(struct  timeval*tv,struct  timezone *tz )
其中struct timeval获取时间的结构体,struct  timezone为获取时区信息结构体:

struct timeval{
long  tv_sec;
long  tv_usec;
}

struct  timezone{
int  tz_minuteswest;  //和greenwich时间差了多少分钟
int  tz_dsttime;
}
而settimeofday()函数是设置当前的时间和地区,其函数如下所示

头文件:#include<sys/time.h>
函数原型:int  settimeofday(struct  timeval *tv,struct  timezone  *tz)
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构;

[cpp]  view plain  copy
  1. #include<stdlib.h>  
  2. #include<stdio.h>  
  3. #include<sys/time.h>  
  4. int main()  
  5. {  
  6.     struct timeval curtime;  
  7.     struct timezone  curzone;  
  8.     gettimeofday(&curtime,&curzone);  
  9.     printf("current time of sec is %ld, usec is %ld\n",curtime.tv_sec,curtime.tv_usec);  
  10.     printf("current zone is %d, dsttime is %d\n",curzone.tz_minuteswest,curzone.tz_dsttime);  
  11.     //settimeofday  
  12.     curtime.tv_sec +=60;  
  13.     settimeofday(&curtime,&curzone);  
  14.     printf("after settimeofday\n");  
  15.     printf("current time of sec is %ld, usec is %ld\n",curtime.tv_sec,curtime.tv_usec);  
  16.     printf("current zone is %d, dsttime is %d\n",curzone.tz_minuteswest,curzone.tz_dsttime);  
  17.     return 0;  
  18. }  

三、定时器

(1)通过select设置定时器

[cpp]  view plain  copy
  1. #include <sys/time.h>    
  2. #include <sys/select.h>    
  3. #include <time.h>    
  4. #include <stdio.h>    
  5.      
  6. void setTimer(int seconds, int mseconds)    
  7. {    
  8.         struct timeval temp;    
  9.     
  10.         temp.tv_sec = seconds;    
  11.         temp.tv_usec = mseconds;    
  12.     
  13.         select(0, NULL, NULL, NULL, &temp);    
  14.         printf("Hello World\n");    
  15.     
  16.         return ;    
  17. }    
  18.     
  19. int main()    
  20. {    
  21.         int i;    
  22.     
  23.         for(i = 0 ; i < 100; i++)    
  24.                 setTimer(10, 0);    
  25.     
  26.         return 0;    
  27. }    
(2)通过SIGALRM+alarm设置定时器,然而其只能达到秒级别

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<signal.h>  
  3.   
  4. void timer(int signal)  
  5. {  
  6.     if(signal == SIGALRM)  
  7.     {  
  8.         printf("HelloWorld\n");  
  9.         alarm(1);  
  10.     }  
  11.     return ;  
  12. }  
  13. int main()  
  14. {  
  15.     signal(SIGALRM,timer);  
  16.     alarm(1);  
  17.     getchar();  
  18.     return 0;  
  19. }  

猜你喜欢

转载自blog.csdn.net/heart18335101121/article/details/53869730