日期与时间 - C语言标准链接库函数小结


#include <time.h>

 

time_t timer;

 

time(&timer);

struct tm *tm_val = gmtime( const time_t *timer);

char * stra = ctime(&timer);

char * strb = asctime(tm_val);

 

time_t  与系统实现有关,一般为long,自1970-01-01 00:00:00(Epoch)到现在的秒数

struct tm{

    int tm_sec;

    int tm_min;

    int tm_hour;

    int tm_mday;    //本月的第[1, 31]天

    int tm_mon;     //本月第[0, 11]月

    int tm_year;     //该值为 实际年份-1900

    int tm_wday;    //周[0, 6]   周日为0

    int tm_yday;    //是本年的第[0, 365]天

    int tm_isdst;   //夏令时标志

};

其它类似 localetime(), strftime(), 以及宽字符版的wcsftime()都只是输出变体

*除了time(),其它函数都不是线程安全的, POSIX扩展指定了相对应的线程安全函数

 

时钟

struct timespec tpstart;

int err = clock_gettime(CLOCK_REALTIME,  &tpstart);   //获取系统时钟

 

struct timespec{

     time_t   tv_sec;     //秒

     long      tv_nsec;   //纳秒(1微秒=1000纳秒)

};

 

int err = nanosleep(……);      //可替代sleep(), usleep(), 且不会影响包括SIGALRM在内的任何信号的使用



 

#include <sys/time.h>

 

struct timeval{

    time_t  tv_sec;     //存储秒

    time_t  tv_usec;   //存储微秒 (1秒=1000毫秒=1000000微秒)

};

 

struct timeval *now;

int err = gettimeofday(now, NULL);

此时,now->tv_sec 表示自Epoch以来的秒数,now->tv_usec 表示自Epoch以来的微秒数

 

要记录进程运行的时间

clock_t times(struct tms *buffer);   //buffer中详细记录了进程的各项详细时间

 



间隔计时器:

   “过1个小时后吃第一次药,然后每隔4个小时吃一次药。”

struct itimerval{                           //itimerval

       struct timeval it_value;        //初始间隔

       struct timeval it_interval;     //重复间隔

};

 

int ret = setitimer(int which, const struct itimerval *newval, struct itimerval *oldval);

getitimer(……);

which: ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF             //进程常备的三个时钟

 

Example: CPU时间每过2s,打印一个数字

#include <errno.h>

#include <signal.h>

#include <unistd.h>

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>

 

enum { MAX_VALUE = 20 };

int main(){

        int  setupinterrupt(void);

        int setupitimer(void);

   

        if(setupinterrupt() == -1){

                perror("setupinterrupt error");

                return 1;

        }

        if(setupitimer() == -1){

                perror("setupitimer error");

                return 1;

        }

 

        while(1);            //不好的代码,仅仅演示

}

/* 发生CPU时钟信号时,打印数字*/

static  void  handler(int signo){

         static  int  tick = MAX_VALUE;

         int  err = errno;

         fprintf(stderr, "%d. ", tick);

         errno = err;

         if(tick <= 0){

              write(STDERR_FILENO, "Done.", strlen("Done."));

              exit(0);

         }

}

/* 安装CPU时间中断信号 */

int setupinterrupt(){

         struct  sigaction  act;

         act.sa_handler = handler;

         act.sa_flags = 0;

         return (sigemptyset(&act.sa_mask) && sigaction(SIGPROF, &act, NULL));

}

/* 设置间隔时间计时器为2.0ss,初始间隔等于重复间隔*/

int setupitimer(){

        struct itimerval tm;

        tm.it_interval.tv_sec = 2;

        tm.it_interval.tv_usce = 0;

        tm.it_value = tm.it_interval;

        return setitimer(ITIMER_PROF, &tm, NULL);

}


时钟和信号结合需要认真学习,后续补充

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值