你试试下面的程序:
#include
#include
#include
int
main()
{
clock_t
start,
finish;
double
elapsed_time;
start=clock();
//do
sonething
finish=clock();
elapsed_time
=
finish-start;
}
我在vc++
60下运行,可以得到以毫秒为单位的计时
程序主要通过当前系统日历的struct tm结构体获得,主要代码如下,
#include <stdioh>
#include <timeh>
//程序功能输出当前时间在24H下的小时数
int main(int argc, char argv[])
{
struct tm ptr;
time_t lt;
time(<);//当前系统时间
ptr=localtime(<);//获取本地日历时间指针
printf("hour=%d(24H )\n",ptr->tm_hour);//输出24H下的小时数
return 0;
}
结构体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()为负。/
long int tm_gmtoff; /指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数/
const char tm_zone; /当前时区的名字(与环境变量TZ有关)/
};
1、C语言中读取系统时间的函数为time(),其函数原型为:
#include <timeh>
time_t time( time_t ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
2、C语言还提供了将秒数转换成相应的时间格式的函数:
char ctime(const time_t timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值
struct tm gmtime(const time_t timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm 变量来接收结果
struct tm localtime(const time_t timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm 变量来接收结果
3、例程:
#include <timeh>void main()
{
time_t t;
struct tm pt ;
char pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}
//时间结构体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()为负。/
};
以上就是关于请问在C语言里怎么获取当前时间和日期(精确到毫秒)全部的内容,包括:请问在C语言里怎么获取当前时间和日期(精确到毫秒)、如何利用 C 语言获取系统时间、获取时间的C C++函数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)