您的位置:首页 > 编程语言 > C语言/C++

C语言 时间类操作

2014-03-18 11:56 190 查看

1.struct tm

//tm *p里面的数据不可以赋值,会报错的,不许修改系统信息。

在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

#ifndef _TM_DEFINED

struct tm {

int tm_sec; /* 秒–取值区间为[0,59] */

int tm_min; /* 分 - 取值区间为[0,59] */

int tm_hour; /* 时 - 取值区间为[0,23] */

int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */

int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */

int tm_year; /* 年份,其值从1900开始 */

int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */

int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */

int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/

long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/

const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/

};

#define _TM_DEFINED

#endif

ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。

格式转换

可以使用的函数是gmtime()和localtime()将time(NULL)获得的日历时间time_t结构体转换成tm结构体。

其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。

Example:

#include <stdio.h>

#include <time.h>

int main(void)

{

struct tm *ptr;

time_t lt;

lt =time(NULL);

ptr=localtime(<);

printf("second:%d\n",ptr->tm_sec);

printf("minute:%d\n",ptr->tm_min);

printf("hour:%d\n",ptr->tm_hour);

printf("mday:%d\n",ptr->tm_mday);

printf("month:%d\n",ptr->tm_mon+1);

printf("year:%d\n",ptr->tm_year+1900);

return 0;

}


2.time()

time(NULL) 函数返回自 Unix 纪元(January 1 1970 00:00:00 GMT)起的当前时间的秒数。

3.localtime

localtime是 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 。

功 能: 把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为本地时间,而gmtime函数转换后的时间没有经过时区变换,是UTC时间 。

说明:此函数获得的tm结构体的时间是日历时间。

用 法: struct tm *localtime(const time_t *clock);

返回值:返回指向tm 结构体的指针.tm结构体是time.h中定义的用于分别存储时间的各个量(年月日等)的结构体.

Example:

#include <stdio.h>

#include <stddef.h>

#include <time.h>

int main(void)

{

time_t timer;//time_t就是long int 类型

struct tm *tblock;

timer = time(NULL);

tblock = localtime(&timer);

printf("Local time is: %s\n",asctime(tblock));

return 0;

}

执行结果:

Local time is: Mon Feb 16 11:29:26 2009


OR

#include<time.h>

#include<stdio.h>

int main()

{

struct tm *t;

time_t tt;

time(&tt);

t=localtime(&tt);

printf("%4d年%02d月%02d日 %02d:%02d:%02d\n",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);

return 0;

}


4.asctime

函数原型char* asctime (const struct tm * timeptr).

功能:把timeptr指向的tm结构体中储存的时间转换为字符串字符串格式返回,格式为:Www Mmm dd hh:mm:ss yyyy。其中Www为星期;Mmm为月份;dd为日;hh为时;mm为分;ss为秒;yyyy为年份。
Example:

/* asctime example */
#include <stdio.h>      /* printf */
#include <time.h>       /* time_t, struct tm, time, localtime, asctime */

int main ()
{
time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "The current date/time is: %s", asctime (timeinfo) );

return 0;
}
函数范例的输出:
1

The current date/time is: Wed Feb 13 15:46:11 2013

5.clock()

CLOCK计时函数

clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:

clock_t clock(void) ;

简单而言,就是该程序从启动到函数调用占用CPU的时间。这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:

#ifndef _CLOCK_T_DEFINED

typedef long clock_t;

#define _CLOCK_T_DEFINED

#endif

很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:

#define CLOCKS_PER_SEC ((clock_t)1000)

在linux系统下,CLOCKS_PER_SEC的值可能有所不同,目前使用的linux打印出来的值是1000000,表示的是微妙。这一点需要注意。

可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:

void elapsed_time()
{
printf("Elapsed time:%u secs.\n",clock()/CLOCKS_PER_SEC);
}
当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
long i = 10000000L;
clock_t start, finish;
double duration;
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i) ;
start = clock();
while( i-- );
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", duration );
system("pause");
}

在笔者的机器上,运行结果如下:
Time to do 10000000 empty loops is 0.03000 seconds
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言