/*当时写这个程序的时候,系统没安装中文输入法。
注释都是用英文写的,有很多不严谨的地方,大家将就着看吧*/
#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--);
}