获取当前时间及计算时间差

获取当前时间的方法很多:

1 获取当前时间的 时分秒。

#include<stdio.h>
#include<time.h>

int main()
{
    time_t t1,tim;
    struct tm *p;
    time(&t1);
    p = localtime(&t1);
    int Hour = p->tm_hour;
    int Minute = p->tm_min;
    int Second = p->tm_sec;
    char hour[20];
    char minute[20];
    char second[20];
    printf("%d:%d:%d\n",Hour,Minute,Second);
    return 0;
}

2 获取当前时间的年月日时分秒

#include<stdio.h>
#include<time.h>

int main()
{
    time_t t;
    char buf[1024];
    time(&t);
    ctime_r(&t,buf);
    printf("%s",buf);
    return 0;
}

 时间差

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
 
int main()
{
	time_t c_start, t_start, c_end, t_end;
 
	c_start = clock();    //!< 单位为ms
	t_start = time(NULL);  //!< 单位为s
	system("pause");
	c_end   = clock();
	t_end	= time(NULL);
 
	//!<difftime(time_t, time_t)返回两个time_t变量间的时间间隔,即时间差
	printf("The pause used %f ms by clock()\n",difftime(c_end,c_start)); 
	printf("The pause used %f s by time()\n",difftime(t_end,t_start));
	system("pause");
 
	return 0;
}

 以下为网路资源

参考:   http://www.cnblogs.com/krythur/archive/2013/02/25/2932647.html

第一章  获取时间函数

1. char * asctime(const struct tm * timeptr);  

函数说明
 asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993\n”
 
返回值
 若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
 
附加说明
 返回一字符串表示目前当地的时间日期。
 
范例
 #include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
 
执行
 Sat Oct 28 02:10:06 2000

2. 定义函数
 char *ctime(const time_t *timep);
 
函数说明
 ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
 
返回值
 返回一字符串表示目前当地的时间日期。
 
范例
 #include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}
 
执行
 Sat Oct 28 10 : 12 : 05 2000

3. struct tm*gmtime(const time_t*timep);
 
函数说明
 gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
 
返回值
 返回结构tm代表目前UTC 时间
 
范例
 #include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
 
执行
 2000/10/28 Sat 8:15:38

4.  struct tm *localtime(const time_t * timep);
 
函数说明
 localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
 
返回值
 返回结构tm代表目前的当地时间。
 
范例
 #include<time.h>
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
 
执行
 2000/10/28 Sat 11:12:22

5. time_t mktime(strcut tm * timeptr);
 
函数说明
 mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
 
返回值
 返回经过的秒数。
 
范例
 /* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d \n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
 
执行
 time():974943297
time()->localtime()->mktime():974943297

6.  time_t time(time_t *t);
 
函数说明
 此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
 
返回值
 成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
 
范例
 #include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
 

7. gettimeofday() :可获得微妙级(0.000001秒)的系统时间,调用两次gettimeofday(),前后做减法,从而达到定时或者计算时间的目的。

       char *asctime(const struct tm *tm);   

       char *asctime_r(const struct tm *tm, char *buf);

       char *ctime(const time_t *timep);

       char *ctime_r(const time_t *timep, char *buf);

       struct tm *gmtime(const time_t *timep); //获取的为英国时间

       struct tm *gmtime_r(const time_t *timep, struct tm *result);

       struct tm *localtime(const time_t *timep);      //获取的为本地时间,注意与英国时间的区别。

       struct tm *localtime_r(const time_t *timep, struct tm *result);

       time_t mktime(struct tm *tm);

       double difftime(time_t time1, time_t time0);

       int gettimeofday(struct timeval *tv, struct timezone *tz);

       int settimeofday(const struct timeval *tv , const struct timezone *tz);

第二章 计算时间差

1. 实时函数clock_gettime  单位是十亿分之一秒,也就是纳秒(ns),使用的是标准POSIX实时时钟, 计算出来的结果可能有误差。

在POSIX1003.1中增添了这个函数,它的原型如下:

int clock_gettime(clockid_t clk_id, struct timespec *tp);

它有以下的特点:
1)它也有一个时间结构体:timespec ,timespec计算时间次数的单位是十亿分之一秒.
strace timespec{
 time_t tv_sec;
 long tv_nsec;
}

2)clockid_t是确定哪个时钟类型.

CLOCK_REALTIME: 标准POSIX实时时钟
CLOCK_MONOTONIC: POSIX时钟,以恒定速率运行;不会复位和调整,它的取值和CLOCK_REALTIME是一样的.
CLOCK_PROCESS_CPUTIME_ID和CLOCK_THREAD_CPUTIME_ID是CPU中的硬件计时器中实现的.


3)测试:
#include<time.h>
#include<stdio.h>
#include<stdlib.h>

#define MILLION 1000000


int main(void)
{
        long int loop = 1000;
        struct timespec tpstart;
        struct timespec tpend;
        long timedif;

        clock_gettime(CLOCK_MONOTONIC, &tpstart);

        while (--loop){
                system("cd");
        }

        clock_gettime(CLOCK_MONOTONIC, &tpend);
        timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000;
        fprintf(stdout, "it took %ld microseconds\n", timedif);

        return 0;
}

编译:
gcc test3.c -lrt -o test3

计算时间:
time ./test3
it took 3463843 microseconds

2.  int gettimeofday(struct timeval *tv,struct timezone*tz),会把目前的时间tv所指的结构返回,当地时区的信息则放到tz所指的结构中。这两个结构都放在/usr/include/sys/time.h中。  单位 微秒(μs)

#include <stdio.h>
#include <stdlib.h> //malloc要用,没有的话,会有警告信息:隐式声明与内建函数'malloc'不兼容。不过警告信息不用管也没事

#include <assert.h>
#include <sys/time.h>

int main()
{
float time_use=0;
struct timeval start;
struct timeval end;
//struct timezone tz; //后面有说明
gettimeofday(&start,NULL);//gettimeofday(&start,&tz);结果一样
printf("start.tv_sec:%d\n",start.tv_sec);
printf("start.tv_usec:%d\n",start.tv_usec);

sleep(3);
gettimeofday(&end,NULL);
printf("end.tv_sec:%d\n",end.tv_sec);
printf("end.tv_usec:%d\n",end.tv_usec);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %f\n",time_use);

//输出:time_use is 3001410.000000

//下面的采用指针的方式也可以,但是要注意指针类型若不分配内存的话,编译正确,但是运行结果会不对

}

3. #include <sys/times.h>
clock_t times(struct tms *buf); 单位10毫秒(ms)  times实际上面就是调用clock() 实现的。

times() 函数返回从过去一个任意的时间点所经过的时钟数。返回值可能会超出 clock_t  (一般为 long 型) 的范围(溢出)。如果发生错误,则返回 (clock_t ) -1 类型,然后设置相应的 errno 值。

系统每秒的时钟可以通过 sysconf(_SC_CLK_TCK); 函数获得。

tms结构体如下:
strace tms{
 clock_t tms_utime;
 clock_t tms_stime;
 clock_t tms_cutime;
 clock_t tms_cstime;
}

注释:
tms_utime记录的是进程执行用户代码的时间.
tms_stime记录的是进程执行内核代码的时间.
tms_cutime记录的是子进程执行用户代码的时间.
tms_cstime记录的是子进程执行内核代码的时间.


2)测试:

vi test2.c
#include <sys/times.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

static void do_cmd(char *);
static void pr_times(clock_t, struct tms *, struct tms *);

int main(int argc, char *argv[]){
        int i;
        for(i=1; argv[i]!=NULL; i++){
                do_cmd(argv[i]);
        }
        exit(1);
}
static void do_cmd(char *cmd){
        struct tms tmsstart, tmsend;
        clock_t start, end;
        int status;
        if((start=times(&tmsstart))== -1)
                puts("times error");
        if((status=system(cmd))<0)
                puts("system error");
        if((end=times(&tmsend))== -1)
                puts("times error");
        pr_times(end-start, &tmsstart, &tmsend);
        exit(0);
}
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend){
        static long clktck=0;
        if(0 == clktck)
                if((clktck=sysconf(_SC_CLK_TCK))<0)
                           puts("sysconf err");
        printf("real:%7.2f\n", real/(double)clktck);
        printf("user-cpu:%7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime)/(double)clktck);
        printf("system-cpu:%7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime)/(double)clktck);
        printf("child-user-cpu:%7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime)/(double)clktck);
        printf("child-system-cpu:%7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime)/(double)clktck);
}

编译:
gcc test2.c -o test2

测试这个程序:
time ./test2 "dd if=/dev/zero f=/dev/null bs=1M count=10000"
10000+0 records in
10000+0 records out
10485760000 bytes (10 GB) copied, 4.93028 s, 2.1 GB/s
real:   4.94
user-cpu:   0.00
system-cpu:   0.00
child-user-cpu:   0.01
child-system-cpu:   4.82

4. #include <time.h> 

  clock_t clock(void);    单位10毫秒(ms)

 int clock_gettime(clockid_t clk_id, struct timespec *tp);
系统每秒的时钟可以通过 sysconf(_SC_CLK_TCK); 函数获得。
 

第三章 总结

1)精确度比较:

以下是各种精确度的类型转换:
1秒=1000毫秒(ms), 1毫秒=1/1000秒(s);
1秒=1000000 微秒(μs), 1微秒=1/1000000秒(s);
1秒=1000000000 纳秒(ns),1纳秒=1/1000000000秒(s);


2)
clock()函数的精确度是10毫秒(ms)
times()函数的精确度是10毫秒(ms)
gettimofday()函数的精确度是微秒(μs)
clock_gettime()函数的计量单位为十亿分之一,也就是纳秒(ns)

3) times() 和 clock()   

默认的Linux时钟周期是100HZ,而现在最新的内核时钟周期默认为250HZ.
如何得到内核的时钟周期呢?
grep ^CONFIG_HZ /boot/config-2.6.26-1-xen-amd64

CONFIG_HZ_250=y
CONFIG_HZ=250

结果就是250HZ.

而用sysconf(_SC_CLK_TCK);得到的却是100HZ
例如:

#include    <stdio.h>
#include    <stdlib.h>
#include    <unistd.h>
#include    <time.h>
#include    <sys/times.h>
#include    <sys/time.h>

int
main ( int argc, char *argv[] )
{

    long tps = sysconf(_SC_CLK_TCK);
    printf("%ld\n", tps);
   
    return EXIT_SUCCESS;
}

为什么得到的是不同的值呢?
因为sysconf(_SC_CLK_TCK)和CONFIG_HZ所代表的意义是不同的.
sysconf(_SC_CLK_TCK)是GNU标准库的clock_t频率.
它的定义位置在:/usr/include/asm/param.h

例如:
#ifndef HZ
#define HZ 100
#endif

最后总结一下内核时间:
内核的标准时间是jiffy,一个jiffy就是一个内部时钟周期,而内部时钟周期是由250HZ的频率所产生中的,也就是一个时钟滴答,间隔时间是4毫秒(ms).

也就是说:
1个jiffy=1个内部时钟周期=250HZ=1个时钟滴答=4毫秒

sysconf(_SC_CLK_TCK)使用默认的Linux时钟周期是100HZ,  1个jiffy=1个内部时钟周期=100HZ=1个时钟滴答=10毫秒,所以clock() 和 times()的最新单位是10ms

每经过一个时钟滴答就会调用一次时钟中断处理程序,处理程序用jiffy来累计时钟滴答数,每发生一次时钟中断就增1.
而每个中断之后,系统通过调度程序跟据时间片选择是否要进程继续运行,或让进程进入就绪状态

猜你喜欢

转载自blog.csdn.net/chenxinntu/article/details/81586041