由於電腦在跑代碼,10個小時的空閒,就看庫源碼消遣吧。
先挑出一個重要的結構體:
#ifndef _TM_DEFINED struct tm { int tm_sec; /* seconds after the minute - [0,59] */ int tm_min; /* minutes after the hour - [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_yday; /* days since January 1 - [0,365] */ int tm_isdst; /* daylight savings time flag */ }; #define _TM_DEFINED #endif
這個也就是我們經常要用的一個數據塊。
/* Clock ticks macro - ANSI version */ #define CLOCKS_PER_SEC 1000常用的一個參數,用作毫秒轉秒用。
#ifndef _TIME_T_DEFINED #ifdef _USE_32BIT_TIME_T typedef __time32_t time_t; /* time value */ #else typedef __time64_t time_t; /* time value */ #endif #define _TIME_T_DEFINED /* avoid multiple def's of time_t */ #endif得到從標準計時點(一般是1970年1月1日午夜)到當前時間的秒數。
#ifndef _CLOCK_T_DEFINED typedef long clock_t; #define _CLOCK_T_DEFINED #endif得到從進程啓動到此次函數調用的累計的時鐘滴答數。
_Check_return_ _CRT_INSECURE_DEPRECATE(asctime_s) _CRTIMP char * __cdecl asctime(_In_ const struct tm * _Tm); #if __STDC_WANT_SECURE_LIB__ _Check_return_wat_ _CRTIMP errno_t __cdecl asctime_s(_Out_writes_(_SizeInBytes) _Post_readable_size_(26) char *_Buf, _In_ size_t _SizeInBytes, _In_ const struct tm * _Tm); #endif __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, asctime_s, _Post_readable_size_(26) char, _Buffer, _In_ const struct tm *, _Time)把分解時間tm輸出到字符串,結果的格式爲"Www Mmm dd hh:mm:ss yyyy",即“周幾 月份數 日數 小時數:分鐘數:秒鐘數 年份數”。函數返回的字符串爲靜態分配,長度不大於26,與ctime函數共用。函數的每次調用將覆蓋該字符串內容。
_CRT_INSECURE_DEPRECATE(_ctime32_s) _CRTIMP char * __cdecl _ctime32(_In_ const __time32_t * _Time); _CRTIMP errno_t __cdecl _ctime32_s(_Out_writes_(_SizeInBytes) _Post_readable_size_(26) char *_Buf, _In_ size_t _SizeInBytes, _In_ const __time32_t *_Time); __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1(errno_t, _ctime32_s, _Post_readable_size_(26) char, _Buffer, _In_ const __time32_t *, _Time)
把日曆時間time_t timer輸出到字符串,輸出格式與asctime函數一樣 _CRTIMP size_t __cdecl strftime(_Out_writes_z_(_SizeInBytes) char * _Buf, _In_ size_t _SizeInBytes, _In_z_ _Printf_format_string_ const char * _Format, _In_ const struct tm * _Tm); _CRTIMP size_t __cdecl _strftime_l(_Pre_notnull_ _Post_z_ char *_Buf, _In_ size_t _Max_size, _In_z_ _Printf_format_string_ const char * _Format, _In_ const struct tm *_Tm, _In_opt_ _locale_t _Locale);
把分解時間tm轉換爲自定義格式的字符串,類似於常見的字符串格式輸出函數sprintf _CRTIMP double __cdecl _difftime32(_In_ __time32_t _Time1, _In_ __time32_t _Time2);
比較兩個日曆時間之差。