ImageVerifierCode 换一换
格式:DOCX , 页数:33 ,大小:70.97KB ,
资源ID:30102683      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/30102683.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++中各种系统时间的调用.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++中各种系统时间的调用.docx

1、C+中各种系统时间的调用C+中各种关于系统时间调用1.一个最简单易用的时钟程序,调用系统处理器时间,实现年月日,星期的输出#include #include #include void main() time_t t; while(1) /实现循环,不断调用系统时间 time(&t); coutTodays date and time:endlctime(&t)endl; Sleep(1000); /延时n秒后刷新 system(cls); /清屏,如果无此项,输出时将会是没刷新一次就会印出一个时间 2.time.h的一些基本信息time_t和clock_t和size_t都是与long有同等意

2、义得名字。本类别给出时间和日期处理函数。头文件为 time.h 。日期和时间函数time_t和clock_t和size_t都是与long有同等意义的名字。本类别给出时间和日期处理函数头文件 time.h函数列表函数类别函数用途详细说明long clock(void); 返回程序开始执行后占用的处理器时间。得到时间差delay(int n);表明延时n秒;sleep(int n);延时n秒后刷新;double difftime(long t2,long t1) ;返回t2-t1得值。long mktime (struct tm*);将结构体中的当地时间转换为日历时间。得到时间long time

3、(long *tp);返回当前日历时间,同时将返回值赋给*tp;时间转换函数得到以ASCII码表示的时间 char *asctime (const struct tm *tp);得到字符串表示的时间 char *ctime(const time_t *tp);得到指定格式的时间 size_t strftime(char *s,size_t smax,const char *fmt,const struct tm *tp);time.h的原文件为/* time.h* This file has no copyright assigned and is placed in the Public D

4、omain.* This file is a part of the mingw-runtime package.* No warranty is given; refer to the file DISCLAIMER within the package.* Date and time functions and types.*/#ifndef _TIME_H_#define _TIME_H_/* All the headers include this file. */#include #define _need_wchar_t#define _need_size_t#define _ne

5、ed_NULL#ifndef RC_INVOKED#include #endif /* Not RC_INVOKED */* Number of clock ticks per second. A clock tick is the unit by which* processor time is measured and is returned by clock.*/#define CLOCKS_PER_SEC (clock_t)1000)#define CLK_TCK CLOCKS_PER_SEC#ifndef RC_INVOKED/* A type for storing the cur

6、rent time and date. This is the number of* seconds since midnight Jan 1, 1970.* NOTE: This is also defined in non-ISO sys/types.h.*/#ifndef _TIME_T_DEFINEDtypedef long time_t;#define _TIME_T_DEFINED#endif#ifndef _STRICT_ANSI_/* A 64-bit time_t to get to Y3K */#ifndef _TIME64_T_DEFINEDtypedef _int64

7、_time64_t;#define _TIME64_T_DEFINED#endif#endif/* A type for measuring processor time (in clock ticks).*/#ifndef _CLOCK_T_DEFINEDtypedef long clock_t;#define _CLOCK_T_DEFINED#endif#ifndef _TM_DEFINED/* A structure for storing all kinds of useful information about the* current (or another) time.*/str

8、uct tmint tm_sec; /* Seconds: 0-59 (K&R says 0-61?) */int tm_min; /* Minutes: 0-59 */int tm_hour; /* Hours since midnight: 0-23 */int tm_mday; /* Day of the month: 1-31 */int tm_mon; /* Months *since* january: 0-11 */int tm_year; /* Years since 1900 */int tm_wday; /* Days since Sunday (0-6) */int tm

9、_yday; /* Days since Jan. 1: 0-365 */int tm_isdst; /* +1 Daylight Savings Time, 0 No DST,* -1 dont know */;#define _TM_DEFINED#endif#ifdef _cplusplusextern C #endif_CRTIMP clock_t _cdecl clock (void);_CRTIMP time_t _cdecl time (time_t*);_CRTIMP double _cdecl difftime (time_t, time_t);_CRTIMP time_t

10、_cdecl mktime (struct tm*);/* These functions write to and return pointers to static buffers that may* be overwritten by other function calls. Yikes!* NOTE: localtime, and perhaps the others of the four functions grouped* below may return NULL if their argument is not acceptable. Also note* that cal

11、ling asctime with a NULL pointer will produce an Invalid Page* Fault and crap out your program. Guess how I know. Hint: stat called on* a directory gives invalid times in st_atime etc.*/_CRTIMP char* _cdecl asctime (const struct tm*);_CRTIMP char* _cdecl ctime (const time_t*);_CRTIMP struct tm* _cde

12、cl gmtime (const time_t*);_CRTIMP struct tm* _cdecl localtime (const time_t*);_CRTIMP size_t _cdecl strftime (char*, size_t, const char*, const struct tm*);#ifndef _STRICT_ANSI_extern _CRTIMP void _cdecl _tzset (void);#ifndef _NO_OLDNAMESextern _CRTIMP void _cdecl tzset (void);#endif_CRTIMP char* _c

13、decl _strdate(char*);_CRTIMP char* _cdecl _strtime(char*);/* These require newer versions of msvcrt.dll (6.10 or higher). */#if _MSVCRT_VERSION_ = 0x0601_CRTIMP _time64_t _cdecl _time64( _time64_t);_CRTIMP _time64_t _cdecl _mktime64 (struct tm*);_CRTIMP char* _cdecl _ctime64 (const _time64_t*);_CRTI

14、MP struct tm* _cdecl _gmtime64 (const _time64_t*);_CRTIMP struct tm* _cdecl _localtime64 (const _time64_t*);#endif /* _MSVCRT_VERSION_ = 0x0601 */* _daylight: non zero if daylight savings time is used.* _timezone: difference in seconds between GMT and local time.* _tzname: standard/daylight savings

15、time zone names (an array with two* elements).*/#ifdef _MSVCRT_/* These are for compatibility with pre-VC 5.0 suppied MSVCRT. */extern _CRTIMP int* _cdecl _p_daylight (void);extern _CRTIMP long* _cdecl _p_timezone (void);extern _CRTIMP char* _cdecl _p_tzname (void);_MINGW_IMPORT int _daylight;_MINGW

16、_IMPORT long _timezone;_MINGW_IMPORT char *_tzname2;#else /* not _MSVCRT (ie. crtdll) */#ifndef _DECLSPEC_SUPPORTEDextern int* _imp_daylight_dll;extern long* _imp_timezone_dll;extern char* _imp_tzname;#define _daylight (*_imp_daylight_dll)#define _timezone (*_imp_timezone_dll)#define _tzname (*_imp_

17、tzname)#else /* _DECLSPEC_SUPPORTED */_MINGW_IMPORT int _daylight_dll;_MINGW_IMPORT long _timezone_dll;_MINGW_IMPORT char* _tzname2;#define _daylight _daylight_dll#define _timezone _timezone_dll#endif /* _DECLSPEC_SUPPORTED */#endif /* not _MSVCRT_ */#ifndef _NO_OLDNAMES#ifdef _MSVCRT_/* These go in

18、 the oldnames import library for MSVCRT. */_MINGW_IMPORT int daylight;_MINGW_IMPORT long timezone;_MINGW_IMPORT char *tzname2;#else /* not _MSVCRT_ */* CRTDLL is royally messed up when it comes to these macros.TODO: import and alias these via oldnames import library insteadof macros. */#define dayli

19、ght _daylight/* NOTE: timezone not defined because it would conflict with sys/timeb.h.Also, tzname used to a be macro, but now its in moldname. */_MINGW_IMPORT char *tzname2;#endif /* not _MSVCRT_ */#endif /* Not _NO_OLDNAMES */#endif /* Not _STRICT_ANSI_ */#ifndef _WTIME_DEFINED/* wide function pro

20、totypes, also declared in wchar.h */#ifndef _STRICT_ANSI_#ifdef _MSVCRT_CRTIMP wchar_t* _cdecl _wasctime(const struct tm*);_CRTIMP wchar_t* _cdecl _wctime(const time_t*);_CRTIMP wchar_t* _cdecl _wstrdate(wchar_t*);_CRTIMP wchar_t* _cdecl _wstrtime(wchar_t*);#if _MSVCRT_VERSION_ = 0x0601_CRTIMP wchar

21、_t* _cdecl _wctime64 (const _time64_t*);#endif#endif /* _MSVCRT_ */#endif /* _STRICT_ANSI_ */_CRTIMP size_t _cdecl wcsftime (wchar_t*, size_t, const wchar_t*, const struct tm*);#define _WTIME_DEFINED#endif /* _WTIME_DEFINED */#ifdef _cplusplus#endif#endif /* Not RC_INVOKED */#endif /* Not _TIME_H_ *

22、/3.C语言-time.h从头学本文从介绍基础概念入手,探讨了在C/C+中对日期和时间操作所用到的数据结构和函数,并对计时、时间的获取、时间的计算和显示格式等方面进行了阐述。本文还通过大量的实例向你展示了time.h头文件中声明的各种函数和数据结构的周详使用方法。关键字:UTC(世界标准时间),Calendar Time(日历时间),epoch(时间点),clock tick(时钟计时单元)概念在C/C+中,对字符串的操作有非常多值得注意的问题,同样,C/C+对时间的操作也有许多值得大家注意的地方。最近,在技术群中有非常多网友也多次问到过C+语言中对时间的操作、获取和显示等等的问题。下面,在这

23、篇文章中,笔者将主要介绍在C/C+中时间和日期的使用方法. 通过学习许多C/C+库,你能有非常多操作、使用时间的方法。但在这之前你需要了解一些“时间”和“日期”的概念,主要有以下几个:CoordinatedUniversal Time(UTC):协调世界时,又称为世界标准时间,也就是大家所熟知的格林威治标准时间(Greenwich MeanTime,GMT)。比如,中国内地的时间和UTC的时差为+8,也就是UTC+8。美国是UTC-5。CalendarTime:日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说

24、,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以能说日历时间是“相对时间”,不过无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是相同的。epoch:时间点。时间点在标准C/C+中是个整数,他用此时的时间和标准时间点相差的秒数(即日历时间)来表示。clock tick:时钟计时单元(而不把他叫做时钟滴答次数),一个时钟计时单元的时间长短是由CPU控制的。一个clock tick不是CPU的一个时钟周期,而是C/C+的一个基本计时单位。我们能使用ANSI标准库中的time.h头文件。这个头文件中定义的时间和日期所使用的方法,无论是在结构定义

25、,还是命名,都具有明显的C语言风格。下面,我将说明在C/C+中怎样使用日期的时间功能。计时C/C+中的计时函数是clock(),而和其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:clock_t clock( void ); 这个函数返回从“开启这个程式进程”到“程式中调用clock()函数”时之间的CPU时钟计时单元(clocktick)数,在MSDN中称之为挂钟时间(wal-clock)。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们能找到对他的定义:#ifndef _CLOCK_T_DEFINED typedef long clock

26、_t; #define _CLOCK_T_DEFINED #endif 非常明显,clock_t是个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,他用来表示一秒钟会有多少个时钟计时单元,其定义如下:#define CLOCKS_PER_SEC (clock_t)1000) 能看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你能使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:void elapsed_time() printf(Elapsed time:%u secs.n,clock()/CLOC

27、KS_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();

28、 duration = (double)(finish - start) / CLOCKS_PER_SEC; printf( %f secondsn, duration ); system(pause); 在笔者的机器上,运行结果如下:Time to do 10000000 empty loops is 0.03000 seconds 上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不能通过改动CLOCKS_PER_SEC的定义,通过把他定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C+中,最小的计时单位是一毫秒。和日期和时间相关的数

29、据结构在标准C/C+中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:#ifndef _TM_DEFINED 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

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1