Linux时间编程
来源:互联网 发布:惠州综合网络问政平台 编辑:程序博客网 时间:2024/06/13 22:41
1. time_t
time_t记录自1970年1月1日凌晨以来的秒数,在Linux/Unix上定义为long int类型,在32位系统上,time_t最多只能记录2,147,483,647秒,也就是说到了2038年将会产生溢出,但在64位系统上不会出现此问题。
time_t time(time_t *t);例子如下:
- #include <stdio.h>
- #include <time.h>
- int main(void)
- {
- time_t sec;
- sec = time(NULL);
- printf("%ld\n", sec);
- return 0;
- }
double difftime(time_t time1, time_t time0);用于比较两个time_t类型的值。
2. struct tm
由time_t可以知道,操作系统使用的是long int类型的值来存储时间,但对于用户来说肯定是不好理解,所以就有了这里的struct tm类型,定义如下:
struct tm {int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */};----------------------------------------------------------------- tm_sec | 秒,范围是0~59。 tm_min | 分,范围是0~59。 tm_hour | 时,范围是0~23。 tm_mday | 日,范围是1~31。 tm_mon | 月,范围是0~11,注意是0到11。 tm_year | 年,自1900以来的年数。 tm_wday | 星期几,从星期天开始计算,范围是0~6。 tm_yday | 一年中的哪一天,0~365。 tm_isdst | 夏令时间(DST)的一个标志。 -----------------------------------------------------------------
那怎么能得到这个时间呢,有两个函数gmtime和localtime,原型如下:
struct tm *gmtime(const time_t *timep);struct tm *localtime(const time_t *timep);例子如下:
- #include <stdio.h>
- #include <time.h>
- int main(void)
- {
- const char *const days[] = {"星期天", "星期一", "星期二",
- "星期三", "星期四", "星期五", "星期六"};
- time_t sec;
- struct tm *n;
- sec = time(NULL);
- n = localtime(&sec);
- printf("%d年 %d月 %d日 %s %d:%d:%d\n",
- n->tm_year + 1900,
- n->tm_mon + 1,
- n->tm_mday,
- days[n->tm_wday],
- n->tm_hour,
- n->tm_min,
- n->tm_sec);
- return 0;
- }
再介绍两个函数,ctime和asctime,原型如下:
char *ctime(const time_t *timep);char *asctime(const struct tm *tm);例子如下:
- #include <stdio.h>
- #include <time.h>
- int main(void)
- {
- time_t sec;
- sec = time(NULL);
- printf("%s\n", ctime(&sec));
- return 0;
- }
Mon Nov 11 17:40:34 2013
也就说ctime函数将time_t类型的值转换为一个时间相关的字符串,而asctime同ctime函数不同的是,asctime的参数为strut tm类型指针,而ctime的参数为time_t类型的指针,只是参数类型不一样,但是结果都是一样的,都是获取一个时间相关的字符串。
//2015.08.12 add
注意:ctime函数的返回的字符串是静态分配的,下次调用ctime函数会将其覆盖,所以在打印时需要注意一下,例如下面这样的打印语句只能得到一个结果值:
- printf("%s %s", ctime(&time1), ctime(&time2));
必须分开打印,asctime函数也是一样的。
time_t mktime(struct tm *tm);
3. 关于获取和设置系统时钟
前面使用的是time来获取一个时间值,但是它的精度只能达到秒级,如果只是做一个日历足够了,但是如果想获取更精确的时间呢,比如想计算程序的执行时间,显然time函数不能满足我们的要求,那就只能使用这里的gettimeofday函数了,原型如下:
int gettimeofday(struct timeval *tv, struct timezone *tz);
其中struct timeval结构类型定义如下:
struct timeval {struct timeval结构类型提供了一个微秒级成员tv_usec,它的类型同样是一个整型类型。time_t tv_sec;/* seconds */suseconds_t tv_usec;/* microseconds */};
而gettimeofday函数的tz参数用于获取时区信息,在Linux中通常设置为NULL,程序例子如下:
- #include <stdio.h>
- #include <sys/time.h>
- int main(void)
- {
- struct timeval tv;
- int ret;
- ret = gettimeofday(&tv, NULL);
- if (ret) {
- perror("gettimeofday");
- } else {
- printf("seconds=%ld useconds=%ld\n",
- (long)tv.tv_sec, (long)tv.tv_usec);
- }
- return 0;
- }
那如何去设置设置系统时钟呢?这里要介绍两个函数,同time和gettimeofday对应,stime和settimeofday,原型如下:
int stime(time_t *t);int settimeofday(const struct timeval *tv, const struct timezone *tz);只是settimeofday设置的时间比stime更精确罢了。
4. 关于睡眠延时
睡眠延时先介绍两个函数sleep和usleep,原型如下:
unsigned int sleep(unsigned int seconds);int usleep(useconds_t usec);sleep用于睡眠多少秒,而usleep为睡眠多少微妙。
使用这两个函数需要包含头文件unistd.h。
5. 定时器
alarm函数原型如下:
unsigned alarm(unsigned seconds);
例子如下:
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- void alarm_handler(int signum)
- {
- printf("Five seconds passed!\n");
- }
- void func(void)
- {
- signal(SIGALRM, alarm_handler);
- alarm(5);
- pause();
- }
- int main(void)
- {
- func();
- return 0;
- }
除了alarm外,还可以使用setitimer来设置定时器,使用getitimer来获取定时器的状态,原型如下:
int setitimer(int which, const struct itimerval *restrict value, struct itimerval *restrict ovalue);int getitimer(int which, struct itimerval *value);需要包含头文件sys/time.h
which参数有三种取值:
ITIMER_REAL按实际时间记时,时间到了之后发送SIGALRM信号,相当于alarm。ITIMER_VIRTUAL仅当进程执行时才进行记时,发送SIGVTALRM信号。ITIMER_PROF当进程执行时和系统执行该进程时都记时,发送的是SIGPROF信号。
struct itimerval用来指定定时时间,定义如下:
struct itimerval {struct timerval it_interval;/* next value */struct timerval it_value;/* current value */};struct timeval结构前面都见过了,这里就不再描述了。
在setitimer函数中,ovalue如果不为空,则保留上次调用设置的值。例子如下:
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/time.h>
- void alarm_handler(int signo)
- {
- printf("Timer hit!\n");
- }
- void foo(void)
- {
- struct itimerval delay;
- int ret;
- signal(SIGALRM, alarm_handler);
- delay.it_value.tv_sec = 1;
- delay.it_value.tv_usec = 0;
- delay.it_interval.tv_sec = 1;
- delay.it_interval.tv_usec = 0;
- ret = setitimer(ITIMER_REAL, &delay, NULL);
- if (ret) {
- perror("setitimer");
- return;
- }
- while (1);
- }
- int main(void)
- {
- foo();
- return 0;
- }
0 0
- linux下时间编程
- linux历程--时间编程
- Linux时间编程
- Linux应用 - 时间编程
- linux 时间编程
- linux时间编程
- linux时间编程
- Linux时间编程
- Study Linux - 时间编程
- Linux时间编程
- linux下时间编程
- linux时间编程
- Linux时间编程
- Linux编程---时间相关
- Linux时间编程
- linux时间编程
- Linux下时间编程
- Linux 时间编程
- 核电站
- sklearn库学习笔记1——preprocessing库
- java学习笔记___计算机语言发展史___java发展史
- Face Alignment at 3000 FPS通俗易懂讲解一 随机森林的生成
- HDU-畅通工程-1232
- Linux时间编程
- 深入浅出密码学---仿射加密
- android 当Activity中弹出对话框时,无法监听返回键
- nyoj106_背包问题(贪心or多重背包解法)
- 博为峰Java技术文章 ——JavaEE Hibernate实例状态
- 2017危中有机,中国股市和黄金大概率走牛
- 29.最小的K个数
- Web Service 浅谈-写的好清晰
- osg笔记三