首先看几个函数的原型的声明(在time.h中):
clock_t clock( void ) clock_t是用来保存时间的数据类型,是long型
double difftime(time_t time1, time_t time0); 取时间间隔的函数
time_t time(time_t * timer); 日历时间函数
char * asctime(const struct tm * timeptr); 将tm 类的时间结构转化为 固定时间格式
char * ctime(const time_t *timer); 将日历时间转化为 固定时间格式
time_t mktime(struct tm * timeptr); 以年、月、日、时、分、秒等分量保存的时间结构
struct tm * gmtime(const time_t *timer); 将日历时间转化为格林尼治时间
struct tm * localtime(const time_t * timer); 将日历时间转化为当地时间
struct timeval结构体在time.h中的定义为:
struct timeval
{
time_t tv_sec; /* Seconds. */
suseconds_t tv_usec; /* Microseconds. */
};
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒
struct timeval结构体在time.h中的定义为:
struct timeval
{
time_t tv_sec; /* Seconds. */
suseconds_t tv_usec; /* Microseconds. */
};
其中,tv_sec为Epoch到创建struct timeval时的秒数,tv_usec为微秒数,即秒后面的零头。比如当前我写博文时的tv_sec为1244770435,tv_usec为442388,即当前时间距Epoch时间1244770435秒,442388微秒。需要注意的是,因为循环过程,新建结构体变量等过程需消耗部分时间,我们作下面的运算时会得到如下结果:
int i;
for (i = 0; i < 4; ++i)
{
gettimeofday(&tv, NULL);
printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
sleep(1);
}
442388 1244770435
443119 1244770436
443543 1244770437
444153 1244770438
前面为微秒数,后面为秒数,可以看出,在这个简单运算中,只能精确到小数点后面一到两位,或者可以看出,每进行一次循环,均需花费0.005秒的时间,用这个程序来作计时器显然是不行的,除非精确计算产生的代码消耗时间。
tm 的定义:
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()为负。*/}
1.获取间隔时间
1. clock_t start, finish;
long i = 10000000;
double duration;
start = clock();
while( i-- ) ;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
cout<
输出的是: i从10000000减到零用的时间,精确到毫秒
2.
double pause1;
time_t start,end;
start = time(NULL);
system("pause");
end = time(NULL);
pause1 =difftime(end,start);
cout<
输出的是: 你停顿(pause)的时间,精确到秒
2.获得日历时间
time_t lt;
lt =time(NULL);//(还不清楚带参的和不带参的区别)
cout<
输出的是: 从1970年1月1日0时0分0秒到此时的秒数
3.获得日期和时间
1. 将日历时间转化为本地时间(格林尼治时间)
struct tm *local
time_t t;
t=time(NULL);
local=localtime(&t);
//local=gmtime(&t);
cout<tm_hour;
2. 以固定的时间格式获得日期和时间:看清这两个函数的参和返回值的类型
char * asctime(const struct tm * timeptr);
char * ctime(const time_t *timer);
1.将日历时间直接转换为 固定的时间格式的日期和时间
char * jieguo;
time_t lt;
lt =time(NULL);
jieguo =ctime(<);
cout<< jieguo;
2.将日历时间经过localtime()和gmtime()转换后在转换为固定的时间格式的日期和时间
struct tm *local;
char * jieguo;
time_t t;
t =time(NULL);
local=localtime(&t);
//local=gmtime(&t);
jieguo=asctime(local);
cout<< jieguo;
4.分解时间转化为日历时间
这里说的分解时间就是以年、月、日、时、分、秒等分量保存的时间结构,在C/C++中是tm结构。我们可
以使用mktime()函数将用tm结构表示的时间转化为日历时间。其函数原型如下:
time_t mktime(struct tm * timeptr);
其返回值就是转化后的日历时间。这样我们就可以先制定一个分解时间,然后对这个时间进行操作了,
下面的例子可以计算出1997年7月1日是星期几:
#include "time.h"
#include "stdio.h"
#include "stdlib.h"
int main(void)
{
struct tm t;
time_t t_of_day;
t.tm_year=1997-1900;
t.tm_mon=6;
t.tm_mday=1;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=1;
t.tm_isdst=0;
t_of_day=mktime(&t);
printf(ctime(&t_of_day));
return 0;
}
运行结果:
Tue Jul 01 00:00:01 1997
现在注意了,有了mktime()函数,是不是我们可以操作现在之前的任何时间呢?你可以通过这种办法算
出1945年8月15号是星期几吗?答案是否定的。因为这个时间在1970年1月1日之前,所以在大多数编译器
中,这样的程序虽然可以编译通过,但运行时会异常终止。
5.还知道了一个system()函数,是执行DOS命令的,system("某DOS命令");头文件是stdlib.h?windows.h
struct tm
{
int tm_sec; /* Seconds. [0-60] (1 leap second) */
int tm_min; /* Minutes. [0-59] */
int tm_hour; /* Hours. [0-23] */
int tm_mday; /* Day. [1-31] */
int tm_mon; /* Month. [0-11] */
int tm_year; /* Year - 1900. */
int tm_wday; /* Day of week. [0-6] */
int tm_yday; /* Days in year.[0-365] */
int tm_isdst; /* DST. [-1/0/1]*/
#ifdef __USE_BSD
long int tm_gmtoff; /* Seconds east of UTC. */
__const char *tm_zone; /* Timezone abbreviation. */
#else
long int __tm_gmtoff; /* Seconds east of UTC. */
__const char *__tm_zone; /* Timezone abbreviation. */
#endif
};
在C语言中
有time_t tm timeval等几种类型的时间
1.time_t为typedef __int64 __time64_t;
2.struct timeval
{
uint tv_sec;
uint tv.usec;
}
具体操作函数
包含文件:
tm *gmtime(time_t * t);
time_t time(time_t *t);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
把tm指针转换为time_t
time_t mktime(struct tm *timeptr);
localtime和gmtime的区别在于gmtime将时间转换为国际标准格式,也就是相对于1970 00:00:00开始的时间戳
而localtime是相对于本地的时区的格式。
#include
#include
#include
#include
#include
void quit_t()
{
printf("eixt now");
exit(-1);
}
int main()
{
/* struct timeval vt;
gettimeofday(&vt , NULL);
while(1)
{
printf("%u:%u\n",vt.tv_sec,vt.tv_usec);
sleep(2);
signal(SIGINT, quit_t);
}
*/
struct tm *tt;
time_t t = time(NULL);
tt = gmtime(&t);
//char *s = asctime(tt);
printf("%d-%d-%d %d:%d:%d",tt->tm_year+1900,tt->tm_mon+1,tt->tm_mday,tt->tm_hour,tt->tm_min,tt->tm_sec);
return 0;
}
Time structure
Structure containing a calendar date and time broken down into its components.
The structure contains nine members of type int, which are (in any order):
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;
The meaning of each is:
Member
Meaning
Range
tm_sec
seconds after the minute
0-61*
tm_min
minutes after the hour
0-59
tm_hour
hours since midnight
0-23
tm_mday
day of the month
1-31
tm_mon
months since January
0-11
tm_year
years since 1900
tm_wday
days since Sunday
0-6
tm_yday
days since January 1
0-365
tm_isdst
Daylight Saving Time flag
The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.
* tm_sec is generally 0-59. Extra range to accommodate for leap seconds in certain systems.