Linux时间编程

/*当时写这个程序的时候,系统没安装中文输入法。

注释都是用英文写的,有很多不严谨的地方,大家将就着看吧*/

#include <stdio.h>

#include <stdlib.h>
#include <time.h>
#define PRINT_EXE_TM(fun) printf("the "#fun"() executed for %fs\n",executivetime(fun))
float executivetime(void (*function)(void));
char * currenttime(void);
char * utc(void);
void delay(void);
/*************************************************************************
*
*                MAIN
*
*describe : char *asctime(const struct tm *timeptr),
*        char *ctime(const time_t *timep),
*        int gettimeofday(struct timeval *tv,timezone *tz),
*        struct tm *gmtime(const time_t *timep),
*        struct tm *localtime(const time_t *timep),
*        time_t time(time_t *t);        
*
*************************************************************************/
int main()
{
    PRINT_EXE_TM(delay);
    printf("the current local time is :%s",currenttime());
    sleep(1);    //make this program sleep for 1s
    usleep(1);    //make this program sleep for 1us
    printf("the current UTC is :%s",utc());
    return 0;
}

/**************************************************************************
*
*                executivetime
*
*describe : execute the function and get the executive time
*
*argument : the pointer of a function with no argumens and no returns
*
*return : the executive time (s)
*
**************************************************************************/
float executivetime(void (*function)())
{
    float t;
    struct timeval tpstart,tpend;
    gettimeofday(&tpstart,NULL);
    function();
    gettimeofday(&tpend,NULL);
    t=1000000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_usec-tpstart.tv_usec);
    t/=1000000;
    return t;
}
/*************************************************************************
*
*                currenttime
*
*describe : get current local time
*
*arguments : no
*
*returns : the string of local time
*
*notes : there are 2 ways to achieve this function
*
*************************************************************************/
char * currenttime()
{
    #if 0
    time_t t1;
    char *tp1;
    t1=time(NULL);    //get calendar time
    tp1=ctime(&t1);    //transform the calendar time into the string of local time
    return tp1;
    #endif

    /* here is another way */
    #if 1
    time_t t2;
    char *tp2;
    struct tm *local;
    t2=time(NULL);    //get calendar time
    local=localtime(&t2);    //get the local time ,the datas of date is saved in the struct of *local
    
    /* print the datas of *local */
    /*printf("Local time is %d.%d.%d  %d:%d:%d\n",1900+local->tm_year,
        local->tm_mon,local->tm_mday,local->tm_hour,local->tm_min,
        local->tm_sec);*/

    tp2=asctime(local);    //transform the local time (struct) to the string of local time
    return tp2;
    #endif
}

/*************************************************************************
*
*                utc
*
*describe : get UTC
*
*arguments : no
*
*returns : the string of UTC
*
*************************************************************************/
char * utc()
{
    time_t t;
    char *utcp;
    struct tm *utc;
    
    t=time(NULL);    //get calendar time
    utc=gmtime(&t);    //get UTC , the data of UTC is saved in the struct of utc
    
    /* print the datas of *local */
    /*printf("Local time is %d.%d.%d  %d:%d:%d\n",1900+local->tm_year,
        local->tm_mon,local->tm_mday,local->tm_hour,local->tm_min,
        local->tm_sec);*/
    utcp=asctime(utc);    //transform the UTC(struct) into the string of UTC
    
    return utcp;    
}

/* delay */
void delay()
{
    long int i=100000000;
    while(i--);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值