本文目录一览:
用c语言获取时间
#include stdio.h
#include time.h
int main()
{
time_t rawtime;
struct tm * timeinfo;
time ( rawtime );
timeinfo = localtime ( rawtime );
printf ( "当前系统时间: %s", asctime(timeinfo) );
return 0;
}
说明:
time_t
:时间类型(time.h
定义)struct tm
:时间结构,time.h
定义如下:
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;
time(rawtime)
:获取时间,以秒计,从1970年1月1日起算,存于rawtime
localtime(rawtime)
:转为当地时间,tm
时间结构asctime()
:转为标准ASCII时间格式
就是直接打印
tm
,tm_year
从1900年计算,所以要加1900;tm_mon
从0计算,所以要加1。
c语言编程,怎么计算时间
#include stdio.h
#include stdlib.h
#include time.h
void main()
{
unsigned char time1[] = { 10, 8, 31, 9, 26 };
unsigned char time2[] = { 10, 8, 31, 9, 50 };
struct tm t1 = {0};
struct tm t2 = {0};
time_t _t1;
time_t _t2;
double diff;
t1.tm_year = time1[0] + 100;
t1.tm_mon = time1[1];
t1.tm_mday = time1[2];
t1.tm_hour = time1[3];
t1.tm_min = time1[4];
t2.tm_year = time2[0] + 100;
t2.tm_mon = time2[1];
t2.tm_mday = time2[2];
t2.tm_hour = time2[3];
t2.tm_min = time2[4];
_t1 = _mkgmtime(t1);
_t2 = _mkgmtime(t2);
diff = difftime(_t2, _t1);
printf("相差 %.0f 分钟\n", diff / 60);
}
扩展资料:
C语言中有两个相关的函数用来计算时间差,分别是:
time_t time(time_t *t)
clock_t clock(void)
头文件:time.h
计算的时间单位分别为: 秒(s)、毫秒(ms) 返回值说明:
time
:返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果参数t
非空指针的话,返回的时间会保存在t
所指向的内存。clock
:返回从“开启这个程序进程”到“程序中调用clock()
函数”时之间的CPU时钟计时单元(clock tick)数。1单元 = 1 ms。
示例代码:
#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);
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;
}
因此,要计算某一函数块的占用时间时,只需要在执行该函数块之前和执行完该函数块之后调用同一个时间计算函数,再调用函数 difftime()
计算两者的差,即可得到耗费时间。
C语言计算时间
在C语言中计算时间,可以使用标准库中的计时函数——clock()
。
函数原型:
clock_t clock(void);
其中 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)
可以看到每过千分之一秒(1毫秒),调用 clock()
函数返回的值就加1。
示例代码:
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");
}
C语言中有没有能显示系统日期和时间的函数?
C语言中读取系统时间的函数为 time()
,其函数原型为:
#include time.h
time_t time(time_t *);
time_t
就是 long
,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒到现在所经过的秒数。可以调用 ctime()
函数进行时间转换输出:
char *ctime(const time_t *timer);
将日历时间转换成本地时间,按年月日格式进行输出,例如:
Wed Sep 23 08:43:03 2015
C语言还提供了将秒数转换成相应的时间结构的函数:
struct tm *gmtime(const time_t *timer);
将日历时间转化为世界标准时间(即格林尼治时间)struct tm *localtime(const time_t *timer);
将日历时间转化为本地时间 将通过time()
函数返回的值,转换成时间结构struct 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()为负。*/
};
编程者可以根据程序功能的情况,灵活地进行日期的读取与输出。
示例代码:
#include time.h
main()
{
time_t timep;
struct tm *p;
time(&timep);
p = gmtime(&timep);
printf("%d\n", p->tm_sec); /* 获取当前秒 */
printf("%d\n", p->tm_min); /* 获取当前分 */
printf("%d\n", 8 + p->tm_hour); /* 获取当前时,这里获取西方的时间,刚好相差八个小时 */
printf("%d\n", p->tm_mday); /* 获取当前月份日数,范围是1-31 */
printf("%d\n", 1 + p->tm_mon); /* 获取当前月份,范围是0-11,所以要加1 */
printf("%d\n", 1900 + p->tm_year); /* 获取当前年份,从1900开始,所以要加1900 */
printf("%d\n", p->tm_yday); /* 从今年1月1日算起至今的天数,范围为0-365 */
}