没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
C 语言中如何获取时间?精度如何?
1 使用 time_t time( time_t * timer ) 精确到秒
2 使用 clock_t clock() 得到的是 CPU 时间精确到 1/CLOCKS_PER_SEC 秒
3 计算时间差使用 double difftime( time_t timer1, time_t timer0 )
4 使用 DWORD GetTickCount() 精确到毫秒
5 如果使用 MFC 的 CTime 类,可以用 CTime::GetCurrentTime() 精确到秒
6 要获取高精度时间,可以使用
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)
获取系统的计数器的频率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)
获取计数器的值
然后用两次计数器的差除以 Frequency 就得到时间。
7 Multimedia Timer Functions
The following functions are used with multimedia timers.
timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
//*********************************************************************
//用标准 C 实现获取当前系统时间的函数
一.time()函数
time(&rawtime)函数获取当前时间距 1970 年 1 月 1 日的秒数,以秒计数单位,存于 rawtime 中。
#include "time.h"
void main ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include -- 必须的时间函数头文件
time_t -- 时间类型(time.h 定义是 typedef long time_t; 追根溯源,time_t 是 long)
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 月一日起算,存于 rawtime
localtime ( &rawtime ); -- 转为当地时间,tm 时间结构
asctime ()-- 转为标准 ASCII 时间格式:
星期 月 日 时:分:秒 年
-----------------------------------------------------------------------------
二 .clock() 函 数 , 用 clock() 函 数 , 得 到 系 统 启 动 以 后 的 毫 秒 级 时 间 , 然 后 除 以
CLOCKS_PER_SEC,就可以换成“秒”,标准 c 函数。
clock_t clock ( void );
#include
clock_t t = clock();
long sec = t / CLOCKS_PER_SEC;
他是记录时钟周期的,实现看来不会很精确,需要试验验证;
---------------------------------------------------------------------------
三.gettime(&t); 据说 tc2.0 的 time 结构含有毫秒信息
#include
#include
int main(void)
{
struct time t;
gettime(&t);
printf("The current time is: -:d:d.d\n",
t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund);
return 0;
}
time 是一个结构体,, 其中成员函数 ti_hund 是毫秒。。。
--------------------------------------------------------------------------------
四.GetTickCount(),这个是 windows 里面常用来计算程序运行时间的函数;
DWORD dwStart = GetTickCount();
//这里运行你的程序代码
DWORD dwEnd = GetTickCount();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位
这个函数只精确到 55ms,1 个 tick 就是 55ms。
--------------------------------------------------------------------------------
五.timeGetTime()t,imeGetTime()基本等于 GetTickCount(),但是精度更高
DWORD dwStart = timeGetTime();
//这里运行你的程序代码
DWORD dwEnd = timeGetTime();
则(dwEnd-dwStart)就是你的程序运行时间, 以毫秒为单位
虽然返回的值单位应该是 ms,但传说精度只有 10ms。
=========================================
//*****************************************************************Unix
##unix 时间相关,也是标准库的
//*********************************************************************
1.timegm 函数只是将 struct tm 结构转成 time_t 结构,不使用时区信息;
time_t timegm(struct tm *tm);
2.mktime 使用时区信息
time_t mktime(struct tm *tm);
timelocal 函数是 GNU 扩展的与 posix 函数 mktime 相当
time_t timelocal (struct tm *tm);
3.gmtime 函数只是将 time_t 结构转成 struct tm 结构,不使用时区信息;
struct tm * gmtime(const time_t *clock);
4.localtime 使用时区信息
struct tm * localtime(const time_t *clock);
1.time 获取时间,stime 设置时间
time_t t;
t = time(&t);
2.stime 其参数应该是 GMT 时间,根据本地时区设置为本地时间;
int stime(time_t *tp)
3.UTC=true 表示采用夏时制;
4.文件的修改时间等信息全部采用 GMT 时间存放,不同的系统在得到修改时间后通过 localtime
转换成本地时间;
5.设置时区推荐使用 setup 来设置;
6. 设置时区也可以先 更 变 /etc/sysconfig/clock 中的设 置 再将 ln -fs /usr/share/zoneinfo/xxxx/xxx
/etc/localtime 才能重效
time_t 只能表示 68 年的范围,即 mktime 只能返回 1970-2038 这一段范围的 time_t
看看你的系统是否有 time_t64,它能表示更大的时间范围
//***************************************************************windows
##Window 里面的一些不一样的
//*********************************************************************
一.CTime () 类
VC 编程一般使用 CTime 类 获得当前日期和时间
CTime t = GetCurrentTime();
SYSTEMTIME 结构包含毫秒信息
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
SYSTEMTIME t1;
剩余22页未读,继续阅读
资源评论
智慧安全方案
- 粉丝: 3702
- 资源: 59万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 2022213854李沐窈程序.cpp
- pod-tomcat.yaml
- 数据库管理工具:dbeaver-ce-23.0.3-amd64.deb
- This module implements the Requests API.
- Delphi 12 控件之unidac-10.2.1-d29pro.exe
- keyword.other.package.java
- Apple//DTD PLIST 1.0//EN
- comment.block.documentation.rust
- there is no way to distinguish perfectly
- 数据库管理工具:dbeaver-ce-24.0.2-x86-64-setup.exe
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功