C++中各种系统时间的调用.docx
- 文档编号:30102683
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:33
- 大小:70.97KB
C++中各种系统时间的调用.docx
《C++中各种系统时间的调用.docx》由会员分享,可在线阅读,更多相关《C++中各种系统时间的调用.docx(33页珍藏版)》请在冰豆网上搜索。
C++中各种系统时间的调用
C++中各种关于系统时间调用
1.一个最简单易用的时钟程序,调用系统处理器时间,实现年月日,星期的输出
#include
#include
#include
voidmain()
{
time_tt;
while
(1)//实现循环,不断调用系统时间
{
time(&t);
cout<<"Today'sdateandtime:
"< Sleep(1000);//延时n秒后刷新 system("cls");//清屏,如果无此项,输出时将会是没刷新一次就会印出一个时间 } } 2."time.h"的一些基本信息 time_t和clock_t和size_t都是与long有同等意义得名字。 本类别给出时间和日期处理函数。 头文件为time.h。 日期和时间函数 time_t和clock_t和size_t都是与long有同等意义的名字。 本类别给出时间和日期处理函数 头文件time.h 函数列表 函数类别函数用途详细说明 longclock(void);返回程序开始执行后占用的处理器时间。 得到时间差 delay(intn);表明延时n秒; sleep(intn);延时n秒后刷新; doubledifftime(longt2,longt1);返回t2-t1得值。 longmktime(structtm*);将结构体中的当地时间转换为日历时间。 得到时间 longtime(long*tp);返回当前日历时间,同时将返回值赋给*tp; 时间转换函数得到以ASCII码表示的时间char*asctime(conststructtm*tp); 得到字符串表示的时间char*ctime(consttime_t*tp); 得到指定格式的时间size_tstrftime(char*s,size_tsmax,constchar*fmt,conststructtm*tp); time.h的原文件为 /* *time.h *ThisfilehasnocopyrightassignedandisplacedinthePublicDomain. *Thisfileisapartofthemingw-runtimepackage. *Nowarrantyisgiven;refertothefileDISCLAIMERwithinthepackage. * *Dateandtimefunctionsandtypes. * */ #ifndef_TIME_H_ #define_TIME_H_ /*Alltheheadersincludethisfile.*/ #include<_mingw.h> #define__need_wchar_t #define__need_size_t #define__need_NULL #ifndefRC_INVOKED #include #endif/*NotRC_INVOKED*/ /* *Numberofclocktickspersecond.Aclocktickistheunitbywhich *processortimeismeasuredandisreturnedby'clock'. */ #defineCLOCKS_PER_SEC((clock_t)1000) #defineCLK_TCKCLOCKS_PER_SEC #ifndefRC_INVOKED /* *Atypeforstoringthecurrenttimeanddate.Thisisthenumberof *secondssincemidnightJan1,1970. *NOTE: Thisisalsodefinedinnon-ISOsys/types.h. */ #ifndef_TIME_T_DEFINED typedeflongtime_t; #define_TIME_T_DEFINED #endif #ifndef__STRICT_ANSI__ /*A64-bittime_ttogettoY3K*/ #ifndef_TIME64_T_DEFINED typedef__int64__time64_t; #define_TIME64_T_DEFINED #endif #endif /* *Atypeformeasuringprocessortime(inclockticks). */ #ifndef_CLOCK_T_DEFINED typedeflongclock_t; #define_CLOCK_T_DEFINED #endif #ifndef_TM_DEFINED /* *Astructureforstoringallkindsofusefulinformationaboutthe *current(oranother)time. */ structtm { inttm_sec;/*Seconds: 0-59(K&Rsays0-61? )*/ inttm_min;/*Minutes: 0-59*/ inttm_hour;/*Hourssincemidnight: 0-23*/ inttm_mday;/*Dayofthemonth: 1-31*/ inttm_mon;/*Months*since*january: 0-11*/ inttm_year;/*Yearssince1900*/ inttm_wday;/*DayssinceSunday(0-6)*/ inttm_yday;/*DayssinceJan.1: 0-365*/ inttm_isdst;/*+1DaylightSavingsTime,0NoDST, *-1don'tknow*/ }; #define_TM_DEFINED #endif #ifdef__cplusplus extern"C"{ #endif _CRTIMPclock_t__cdeclclock(void); _CRTIMPtime_t__cdecltime(time_t*); _CRTIMPdouble__cdecldifftime(time_t,time_t); _CRTIMPtime_t__cdeclmktime(structtm*); /* *Thesefunctionswritetoandreturnpointerstostaticbuffersthatmay *beoverwrittenbyotherfunctioncalls.Yikes! * *NOTE: localtime,andperhapstheothersofthefourfunctionsgrouped *belowmayreturnNULLiftheirargumentisnot'acceptable'.Alsonote *thatcallingasctimewithaNULLpointerwillproduceanInvalidPage *Faultandcrapoutyourprogram.GuesshowIknow.Hint: statcalledon *adirectorygives'invalid'timesinst_atimeetc... */ _CRTIMPchar*__cdeclasctime(conststructtm*); _CRTIMPchar*__cdeclctime(consttime_t*); _CRTIMPstructtm*__cdeclgmtime(consttime_t*); _CRTIMPstructtm*__cdecllocaltime(consttime_t*); _CRTIMPsize_t__cdeclstrftime(char*,size_t,constchar*,conststructtm*); #ifndef__STRICT_ANSI__ extern_CRTIMPvoid__cdecl_tzset(void); #ifndef_NO_OLDNAMES extern_CRTIMPvoid__cdecltzset(void); #endif _CRTIMPchar*__cdecl_strdate(char*); _CRTIMPchar*__cdecl_strtime(char*); /*Theserequirenewerversionsofmsvcrt.dll(6.10orhigher).*/ #if__MSVCRT_VERSION__>=0x0601 _CRTIMP__time64_t__cdecl_time64(__time64_t); _CRTIMP__time64_t__cdecl_mktime64(structtm*); _CRTIMPchar*__cdecl_ctime64(const__time64_t*); _CRTIMPstructtm*__cdecl_gmtime64(const__time64_t*); _CRTIMPstructtm*__cdecl_localtime64(const__time64_t*); #endif/*__MSVCRT_VERSION__>=0x0601*/ /* *_daylight: nonzeroifdaylightsavingstimeisused. *_timezone: differenceinsecondsbetweenGMTandlocaltime. *_tzname: standard/daylightsavingstimezonenames(anarraywithtwo *elements). */ #ifdef__MSVCRT__ /*Theseareforcompatibilitywithpre-VC5.0suppiedMSVCRT.*/ extern_CRTIMPint*__cdecl__p__daylight(void); extern_CRTIMPlong*__cdecl__p__timezone(void); extern_CRTIMPchar**__cdecl__p__tzname(void); __MINGW_IMPORTint_daylight; __MINGW_IMPORTlong_timezone; __MINGW_IMPORTchar*_tzname[2]; #else/*not__MSVCRT(ie.crtdll)*/ #ifndef__DECLSPEC_SUPPORTED externint*_imp___daylight_dll; externlong*_imp___timezone_dll; externchar**_imp___tzname; #define_daylight(*_imp___daylight_dll) #define_timezone(*_imp___timezone_dll) #define_tzname(*_imp___tzname) #else/*__DECLSPEC_SUPPORTED*/ __MINGW_IMPORTint_daylight_dll; __MINGW_IMPORTlong_timezone_dll; __MINGW_IMPORTchar*_tzname[2]; #define_daylight_daylight_dll #define_timezone_timezone_dll #endif/*__DECLSPEC_SUPPORTED*/ #endif/*not__MSVCRT__*/ #ifndef_NO_OLDNAMES #ifdef__MSVCRT__ /*ThesegointheoldnamesimportlibraryforMSVCRT.*/ __MINGW_IMPORTintdaylight; __MINGW_IMPORTlongtimezone; __MINGW_IMPORTchar*tzname[2]; #else/*not__MSVCRT__*/ /*CRTDLLisroyallymessedupwhenitcomestothesemacros. TODO: importandaliastheseviaoldnamesimportlibraryinstead ofmacros.*/ #definedaylight_daylight /*NOTE: timezonenotdefinedbecauseitwouldconflictwithsys/timeb.h. Also,tznameusedtoabemacro,butnowit'sinmoldname.*/ __MINGW_IMPORTchar*tzname[2]; #endif/*not__MSVCRT__*/ #endif/*Not_NO_OLDNAMES*/ #endif/*Not__STRICT_ANSI__*/ #ifndef_WTIME_DEFINED /*widefunctionprototypes,alsodeclaredinwchar.h*/ #ifndef__STRICT_ANSI__ #ifdef__MSVCRT__ _CRTIMPwchar_t*__cdecl_wasctime(conststructtm*); _CRTIMPwchar_t*__cdecl_wctime(consttime_t*); _CRTIMPwchar_t*__cdecl_wstrdate(wchar_t*); _CRTIMPwchar_t*__cdecl_wstrtime(wchar_t*); #if__MSVCRT_VERSION__>=0x0601 _CRTIMPwchar_t*__cdecl_wctime64(const__time64_t*); #endif #endif/*__MSVCRT__*/ #endif/*__STRICT_ANSI__*/ _CRTIMPsize_t__cdeclwcsftime(wchar_t*,size_t,constwchar_t*,conststructtm*); #define_WTIME_DEFINED #endif/*_WTIME_DEFINED*/ #ifdef__cplusplus } #endif #endif/*NotRC_INVOKED*/ #endif/*Not_TIME_H_*/ 3.C语言-time.h从头学 本文从介绍基础概念入手,探讨了在C/C++中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述。 本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的周详使用方法。 关键字: UTC(世界标准时间),CalendarTime(日历时间),epoch(时间点),clocktick(时钟计时单元) .概念 在C/C ++中,对字符串的操作有非常多值得注意的问题,同样,C/C++对时间的操作也有许多值得大家注意的地方。 最近,在技术群中有非常多网友也多次问到过C++ 语言中对时间的操作、获取和显示等等的问题。 下面,在这篇文章中,笔者将主要介绍在C/C++中时间和日期的使用方法. 通过学习许多C/C++库,你能有非常多操作、使用时间的方法。 但在这之前你需要了解一些“时间”和“日期”的概念,主要有以下几个: Coordinated UniversalTime(UTC): 协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(GreenwichMean Time,GMT)。 比如,中国内地的时间和UTC的时差为+8,也就是UTC+8。 美国是UTC-5。 Calendar Time: 日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。 这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来 说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以能说日历时间是“相对时间”,不过无论你在哪一个时区, 在同一时刻对同一个标准时间点来说,日历时间都是相同的。 epoch: 时间点。 时间点在标准C/C++中是个整数,他用此时的时间和标准时间点相差的秒数(即日历时间)来表示。 clocktick: 时钟计时单元(而不把他叫做时钟滴答次数),一个时钟计时单元的时间长短是由CPU控制的。 一个clocktick不是CPU的一个时钟周期,而是C/C++的一个基本计时单位。 我们能使用ANSI标准库中的time.h头文件。 这个头文件中定义的时间和日期所使用的方法,无论是在结构定义,还是命名,都具有明显的C语言风格。 下面,我将说明在C/C++中怎样使用日期的时间功能。 .计时 C/C++中的计时函数是clock(),而和其相关的数据类型是clock_t。 在MSDN中,查得对clock函数定义如下: clock_tclock(void); 这 个函数返回从“开启这个程式进程”到“程式中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock)。 其中clock_t是用来保存时间的数据类型,在time.h文件中,我们能找到对 他的定义: #ifndef_CLOCK_T_DEFINED typedeflongclock_t; #define_CLOCK_T_DEFINED #endif 非常明显,clock_t是个长整形数。 在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,他用来表示一秒钟会有多少个时钟计时单元,其定义如下: #defineCLOCKS_PER_SEC((clock_t)1000) 能看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。 下面举个例子,你能使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间: voidelapsed_time() { printf("Elapsedtime: %usecs.\n",clock()/CLOCKS_PER_SEC); } 当然,你也能用clock函数来计算你的机器运行一个循环或处理其他事件到底花了多少时间: #include“stdio.h” #include“stdlib.h” #include“time.h” intmain(void) { longi=10000000L; clock_tstart,finish; doubleduration; /*测量一个事件持续的时间*/ printf("Timetodo%ldemptyloopsis",i); start=clock(); while(i--); finish=clock(); duration=(double)(finish-start)/CLOCKS_PER_SEC; printf("%fseconds\n",duration); system("pause"); } 在笔者的机器上,运行结果如下: Timetodo10000000emptyloopsis0.03000seconds 上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不能通过改动CLOCKS_PER_SEC的定义,通过把他定义的大一些,从而使计时精度更高呢? 通过尝试,你会发现这样是不行的。 在标准C/C++中,最小的计时单位是一毫秒。 .和日期和时间相关的数据结构 在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef_TM_DEFINED structtm{ inttm_sec;/*秒? 取值区间为[0,59]*/ inttm_min;/*分-取值区间为[0,59]*/ inttm_hour;/*时-取值区间为[0,23]*/ inttm_mday;/*一个月中的日期-取值区间为[1,31]*/ inttm_mon;/*月份(从一月开始,0代表一月)-取值区间为[0,11]*/ inttm_year;/*年份,其值等于实际年份减去1900*/ inttm_wday;/*星期? 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推*/ inttm_yday;/*从每年的1月1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 各种 系统 时间 调用
![提示](https://static.bdocx.com/images/bang_tan.gif)