C语言time函数
                            
                            
                            在unix/Linux系統中,時間的表示方法是以1970年1月1日00:00:00所經過的秒數,使用基本系統數據類型time_t表示,在/usr/include下查找time_t類型的定義.
 
可以通過time()函數來獲得計算機系統當前的日歷時間(Calendar Time),處理日期時間的函數都是以本函數的返回值為基礎進行運算。其原型為:time_t time(time_t * t); 如果你已經聲明了參數t,你可以從參數t返回現在的日歷時間,同時也可以通過返回值返回現在的日歷時間,即從一個時間點(例如:1970年1月1日0時0分0秒)到現在此時的秒數。如果參數為空(NULL),函數將只通過返回值返回現在的日歷時間。
比如下面這個例子用來顯示當前的日歷時間: #include<stdio.h> #include<time.h> int main() {time_t t; //time for secondsint d; //time for daysint y; //time for yearst=time(NULL);printf("The number of seconds since January 1,1970 is %d\n",t);d=t/(3600*24);printf("The number of days since January 1,1970 is %d\n",d);y=d/365;printf("The number of years since January 1,1970 is %d\n",y);y=y+1970;printf("This year is %d\n",y);return 0; }
可以得到運行結果:
 
 
 
 
1. sys/types.h
#define __need_timer_t
#define __need_clockid_t
#include <time.h>
   
2.time.h
typedef __time_t time_t;
# include <bits/types.h> ? ? ? ?/* This defines __time_t for us. ?*/
3.bits/types.h
__STD_TYPE __TIME_T_TYPE __time_t; ? ? ?/* Seconds since the Epoch. ?*/
# define __STD_TYPE ? ? ? ? ? ? __extension__ typedef
4.bits/typesizes.h
#define __TIME_T_TYPE ? ? ? ? ? __SLONGWORD_TYPE
5. bits/types.h
#define __SLONGWORD_TYPE ? ? ? ?long int
   
這里,基本就可以得出結論了:
  
__extension__ typedef long int time_t
則time_t類型的變量最大值為0x7fffffff
代碼: #include<stdio.h> #include<sys/types.h> #include<time.h> int main(void) {time_t cur_time=time(NULL), max_time=0x7fffffff,new_time=max_time+1;printf("Cur time: cur_time=0x%08x/n", cur_time);printf("/tLocal: %s", asctime(localtime(&cur_time)));printf("/tGMT : %s/n", asctime(gmtime(&cur_time)));printf("Max time: max_time=0x%08x/n", max_time);printf("/tLocal: %s", asctime(localtime(&max_time)));printf("/tGMT : %s/n", asctime(gmtime(&max_time)));printf("New time: new_time=0x%08x/n", new_time);printf("/tLocal: %s", asctime(localtime(&new_time)));printf("/tGMT : %s/n", asctime(gmtime(&new_time)));return 0; }
可以的到運行結果:
 
 
從結果得出,32位的time_t最遲能表示到2038年1月19日 11:14:07(Asia/Shanghai時間) 或2038年1月19日 03:14:07(GMT時間),再過1秒,time_t數據將變為負數,變為1901年12月14日 04:51:44(本地時間),或1901年12月13日 20:45:52(GMT時間).
 
 
借鑒出處:?http://blog.csdn.net/zhangyang0402/archive/2010/07/18/5744475.aspx
 
 
C語言中time_t數據類型詳細介紹
包含文件:<time.h> #ifndef __TIME_T #define __TIME_T ? ? /* 避免重復定義 time_t */ typedef long ? ? time_t; ? ?/* 時間值time_t 為長整型的別名*/ #endif 既然time_t實際上是長整型(long int),用來保存從1970年1月1日0時0分0秒到現在時刻的秒數。到未來的某一天,從一個時間點(一般是1970年1月1日0時0分0秒)到那時的秒數(即日歷時間)超出了長整形所能表示的數的范圍怎么辦?對time_t數據類型的值來說,它所表示的時間不能晚于2038年1月18日19時14分07秒。為了能夠表示更久遠的時間,一些編譯器廠商引入了64位甚至更長的整形數來保存日歷時間。比如微軟在Visual C++中采用了__time64_t數據類型來保存日歷時間,并通過_time64()函數來獲得日歷時間(而不是通過使用32位字的time()函數),這樣就可以通過該數據類型保存3001年1月1日0時0分0秒(不包括該時間點)之前的時間。 在time.h頭文件中,我們還可以看到一些函數,它們都是以time_t為參數類型或返回值類型的函數: double difftime(time_t time1, time_t time0); time_t mktime(struct tm * timeptr); time_t time(time_t * timer); char * asctime(const struct tm * timeptr); char * ctime(const time_t *timer); 此外,time.h還提供了兩種不同的函數將日歷時間(一個用time_t表示的整數)轉換為我們平時看到的把年月日時分秒分開顯示的時間格式tm: struct tm * gmtime(const time_t *timer); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? struct tm * localtime(const time_t * timer); 通過查閱MSDN,我們可以知道Microsoft C/C++ 7.0中時間點的值(time_t對象的值)是從1899年12月31日0時0分0秒到該時間點所經過的秒數,而其它各種版本的Microsoft C/C++和所有不同版本的Visual C++都是計算的從1970年1月1日0時0分0秒到該時間點所經過的秒數。
 
 
C語言中的time函數<time.h>
/* * time.h* This file has no copyright assigned and is placed in the Public Domain.* 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 <_mingw.h>#define __need_wchar_t #define __need_size_t #define __need_NULL #ifndef RC_INVOKED #include <stddef.h> #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 current 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 _TIME32_T_DEFINED typedef __int32 __time32_t; #define _TIME32_T_DEFINED #endif#ifndef __STRICT_ANSI__ /* A 64-bit time_t to get to Y3K */ #ifndef _TIME64_T_DEFINED typedef __int64 __time64_t; #define _TIME64_T_DEFINED #endif #endif#ifndef _TIME_T_DEFINED /* FIXME __STRICT_ANSI__ ! */ #if __MSVCRT_VERSION__ >= 0x0800 #ifndef _USE_32BIT_TIME_T typedef __time64_t time_t; #else typedef __time32_t time_t; #endif /* !_USE_32BIT_TIME_T */ #else typedef __time32_t time_t; #endif /* __MSVCRT_VERSION__ >= 0x0800 */ #define _TIME_T_DEFINED #endif/** A type for measuring processor time (in clock ticks).*/ #ifndef _CLOCK_T_DEFINED typedef 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.*/ struct tm {int 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_yday; /* Days since Jan. 1: 0-365 */int tm_isdst; /* +1 Daylight Savings Time, 0 No DST,* -1 don't know */ }; #define _TM_DEFINED #endif#ifdef __cplusplus extern "C" { #endif_CRTIMP clock_t __cdecl __MINGW_NOTHROW clock (void); #if __MSVCRT_VERSION__ < 0x0800 _CRTIMP time_t __cdecl __MINGW_NOTHROW time (time_t*); _CRTIMP double __cdecl __MINGW_NOTHROW difftime (time_t, time_t); _CRTIMP time_t __cdecl __MINGW_NOTHROW mktime (struct tm*); #endif/** 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 calling 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 __MINGW_NOTHROW asctime (const struct tm*); #if __MSVCRT_VERSION__ < 0x0800 _CRTIMP char* __cdecl __MINGW_NOTHROW ctime (const time_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW gmtime (const time_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW localtime (const time_t*); #endif_CRTIMP size_t __cdecl __MINGW_NOTHROW strftime (char*, size_t, const char*, const struct tm*);#ifndef __STRICT_ANSI__extern _CRTIMP void __cdecl __MINGW_NOTHROW _tzset (void);#ifndef _NO_OLDNAMES extern _CRTIMP void __cdecl __MINGW_NOTHROW tzset (void); #endif_CRTIMP char* __cdecl __MINGW_NOTHROW _strdate(char*); _CRTIMP char* __cdecl __MINGW_NOTHROW _strtime(char*);/* These require newer versions of msvcrt.dll (6.10 or higher). */ #if __MSVCRT_VERSION__ >= 0x0601 _CRTIMP __time64_t __cdecl __MINGW_NOTHROW _time64( __time64_t*); _CRTIMP __time64_t __cdecl __MINGW_NOTHROW _mktime64 (struct tm*); _CRTIMP char* __cdecl __MINGW_NOTHROW _ctime64 (const __time64_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _gmtime64 (const __time64_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _localtime64 (const __time64_t*); #endif /* __MSVCRT_VERSION__ >= 0x0601 *//* These require newer versions of msvcrt.dll (8.00 or higher). */ #if __MSVCRT_VERSION__ >= 0x0800 _CRTIMP __time32_t __cdecl __MINGW_NOTHROW _time32 (__time32_t*); _CRTIMP double __cdecl __MINGW_NOTHROW _difftime32 (__time32_t, __time32_t); _CRTIMP double __cdecl __MINGW_NOTHROW _difftime64 (__time64_t, __time64_t); _CRTIMP __time32_t __cdecl __MINGW_NOTHROW _mktime32 (struct tm*); _CRTIMP __time32_t __cdecl __MINGW_NOTHROW _mkgmtime32 (struct tm*); _CRTIMP __time64_t __cdecl __MINGW_NOTHROW _mkgmtime64 (struct tm*); _CRTIMP char* __cdecl __MINGW_NOTHROW _ctime32 (const __time32_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _gmtime32 (const __time32_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _localtime32 (const __time32_t*); #ifndef _USE_32BIT_TIME_T _CRTALIAS time_t __cdecl __MINGW_NOTHROW time (time_t* _v) { return(_time64 (_v)); } _CRTALIAS double __cdecl __MINGW_NOTHROW difftime (time_t _v1, time_t _v2) { return(_difftime64 (_v1,_v2)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW mktime (struct tm* _v) { return(_mktime64 (_v)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW _mkgmtime (struct tm* _v) { return(_mkgmtime64 (_v)); } _CRTALIAS char* __cdecl __MINGW_NOTHROW ctime (const time_t* _v) { return(_ctime64 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW gmtime (const time_t* _v) { return(_gmtime64 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW localtime (const time_t* _v) { return(_localtime64 (_v)); } #else _CRTALIAS time_t __cdecl __MINGW_NOTHROW time (time_t* _v) { return(_time32 (_v)); } _CRTALIAS double __cdecl __MINGW_NOTHROW difftime (time_t _v1, time_t _v2) { return(_difftime32 (_v1,_v2)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW mktime (struct tm* _v) { return(_mktime32 (_v)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW _mkgmtime (struct tm* _v) { return(_mkgmtime32 (_v)); } _CRTALIAS char* __cdecl __MINGW_NOTHROW ctime (const time_t* _v) { return(_ctime32 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW gmtime (const time_t* _v) { return(_gmtime32 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW localtime (const time_t* _v) { return(_localtime32 (_v)); } #endif /* !_USE_32BIT_TIME_T */ #endif /* __MSVCRT_VERSION__ >= 0x0800 *//** _daylight: non zero if daylight savings time is used.* _timezone: difference in seconds between GMT and local time.* _tzname: standard/daylight savings 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 __MINGW_NOTHROW __p__daylight (void); extern _CRTIMP long* __cdecl __MINGW_NOTHROW __p__timezone (void); extern _CRTIMP char** __cdecl __MINGW_NOTHROW __p__tzname (void);__MINGW_IMPORT int _daylight; __MINGW_IMPORT long _timezone; __MINGW_IMPORT char *_tzname[2];#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___tzname)#else /* __DECLSPEC_SUPPORTED */__MINGW_IMPORT int _daylight_dll; __MINGW_IMPORT long _timezone_dll; __MINGW_IMPORT char* _tzname[2];#define _daylight _daylight_dll #define _timezone _timezone_dll#endif /* __DECLSPEC_SUPPORTED */#endif /* not __MSVCRT__ */#endif /* Not __STRICT_ANSI__ */#ifndef _NO_OLDNAMES#ifdef __MSVCRT__/* These go in the oldnames import library for MSVCRT. */ __MINGW_IMPORT int daylight; __MINGW_IMPORT long timezone; __MINGW_IMPORT char *tzname[2];#else /* not __MSVCRT__ *//* CRTDLL is royally messed up when it comes to these macros.TODO: import and alias these via oldnames import library instead of macros. */#define daylight _daylight /* NOTE: timezone not defined as macro because it would conflict withstruct timezone in sys/time.h.Also, tzname used to a be macro, but now it's in moldname. */ __MINGW_IMPORT char *tzname[2];#endif /* not __MSVCRT__ */#endif /* Not _NO_OLDNAMES */#ifndef _WTIME_DEFINED /* wide function prototypes, also declared in wchar.h */ #ifndef __STRICT_ANSI__ #ifdef __MSVCRT__ _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wasctime(const struct tm*); #if __MSVCRT_VERSION__ < 0x0800 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wctime(const time_t*); #endif _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wstrdate(wchar_t*); _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wstrtime(wchar_t*); #if __MSVCRT_VERSION__ >= 0x0601 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wctime64 (const __time64_t*); #endif #if __MSVCRT_VERSION__ >= 0x0800 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wctime32 (const __time32_t*); #ifndef _USE_32BIT_TIME_T _CRTALIAS wchar_t* __cdecl __MINGW_NOTHROW _wctime (const time_t* _v) { return(_wctime64 (_v)); } #else _CRTALIAS wchar_t* __cdecl __MINGW_NOTHROW _wctime (const time_t* _v) { return(_wctime32 (_v)); } #endif #endif /* __MSVCRT_VERSION__ >= 0x0800 */ #endif /* __MSVCRT__ */ #endif /* __STRICT_ANSI__ */ _CRTIMP size_t __cdecl __MINGW_NOTHROW 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_ */
  
C語言中兩種方式表示時間日期值time_t和struct tm類型的相互轉換 ① ? ? 使用gmtime函數或localtime函數將time_t類型的時間日期轉換為struct tm類型: 使用time函數返回的是一個long值,該值對用戶的意義不大,一般不能根據其值確定具體的年、月、日等數據。gmtime函數可以方便的對time_t類型數據進行轉換,將其轉換為tm結構的數據方便數據閱讀。 gmtime函數的原型如下: struct tm *gmtime(time_t *timep); localtime函數的原型如下: struct tm *localtime(time_t *timep); 將參數timep所指的time_t類型信息轉換成實際所使用的時間日期表示方法,將結果返回到結構tm結構類型的變量。 gmtime函數用來存放實際日期時間的結構變量是靜態分配的,每次調用gmtime函數都將重寫該結構變量。如果希望保存結構變量中的內容,必須將其復制到tm結構的另一個變量中。 gmtime函數與localtime函數的區別: gmtime函數返回的時間日期未經時區轉換,是UTC時間(又稱為世界時間,即格林尼治時間)。 localtime函數返回當前時區的時間, 轉換日期時間表示形式time_t類型轉換為struct tm類型示例: #include <stdio.h> #include <time.h> #include<string.h> int main() {const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字符數組*/time_t t;struct tm *p;t=time(NULL);/*獲取從1970年1月1日零時到現在的秒數,保存到變量t中*/p=gmtime(&t); /*變量t的值轉換為實際日期時間的表示格式*/printf("%d年%02d月%02d日",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);printf(" %s ", wday[p->tm_wday]);printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);return 0; }// time(&rawtime); // 獲取時間,以秒計,從1970年1月一日起算,存于rawtime // timeinfo=localtime(&rawtime); //轉為當地時間,tm 時間結構 // printf("The current data/time is &s ",asctime(timeinfo)); //asctime() // 轉為標準ASCII時間格式: 注意:p=gmtime(&t);此行若改為p=localtime(&t);則返回當前時區的時間
輸出結果:
  
② ? ? 使用mktime函數將struct tm類型的時間日期轉換為time_t類型:
表頭文件
#include <time.h>
定義函數
time_t mktime(strcut tm * timeptr);
函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
?
日期轉換為秒數示例:
#include <stdio.h> #include <time.h> #include <cstdlib> int main() {time_t t;struct tm stm;printf("請輸入日期時間值(按yyyy/mm/dd hh:mm:ss格式):");scanf("%d/%d/%d %d:%d:%d",&stm.tm_year,&stm.tm_mon,&stm.tm_mday,&stm.tm_hour,&stm.tm_min,&stm.tm_sec); stm.tm_year-=1900; /*年份值減去1900,得到tm結構中保存的年份序數*/ stm.tm_mon-=1; /*月份值減去1,得到tm結構中保存的月份序數*/ t=mktime(&stm); /* 若用戶輸入的日期時間有誤,則函數返回值為-1*/ if(-1==t) {printf("輸入的日期時間格式出錯!\n");exit(1); } printf("1970/01/01 00:00:00~%d/%02d/%02d %02d:%02d:%02d共%d秒\n",stm.tm_year+1900,stm.tm_mon,stm.tm_mday,stm.tm_hour,stm.tm_min,stm.tm_sec,t);return 0; }
輸出結果:
  
其中出現的一些小問題: char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字符數組*/
  
error:deprecated conversation from strig constant to 'char'
當我們將一個character pointer variable 初始化成一個string literal的時候, 就會出現此類錯誤。
解決方法:const ?char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字符數組*/ 方案2:使用string:string x = "hello";? 方案3:將string literal轉型為char* 的type: char* x = (char*)"hello";
if(-1==t) { printf("輸入的日期時間格式出錯!\n"); exit(1); }
error: ‘exit’ was not declared in this scope 的解決方法
解決方法:添加#include <cstdlib>
  
最后貼一篇博客 c語言中time函數的用法 http://blog.csdn.net/wangluojisuan/article/details/7045592/
 
   
 
 
 
 
 
                            
                        
                        
                        可以通過time()函數來獲得計算機系統當前的日歷時間(Calendar Time),處理日期時間的函數都是以本函數的返回值為基礎進行運算。其原型為:time_t time(time_t * t); 如果你已經聲明了參數t,你可以從參數t返回現在的日歷時間,同時也可以通過返回值返回現在的日歷時間,即從一個時間點(例如:1970年1月1日0時0分0秒)到現在此時的秒數。如果參數為空(NULL),函數將只通過返回值返回現在的日歷時間。
比如下面這個例子用來顯示當前的日歷時間: #include<stdio.h> #include<time.h> int main() {time_t t; //time for secondsint d; //time for daysint y; //time for yearst=time(NULL);printf("The number of seconds since January 1,1970 is %d\n",t);d=t/(3600*24);printf("The number of days since January 1,1970 is %d\n",d);y=d/365;printf("The number of years since January 1,1970 is %d\n",y);y=y+1970;printf("This year is %d\n",y);return 0; }
可以得到運行結果:
1. sys/types.h
#define __need_timer_t
#define __need_clockid_t
#include <time.h>
2.time.h
typedef __time_t time_t;
# include <bits/types.h> ? ? ? ?/* This defines __time_t for us. ?*/
3.bits/types.h
__STD_TYPE __TIME_T_TYPE __time_t; ? ? ?/* Seconds since the Epoch. ?*/
# define __STD_TYPE ? ? ? ? ? ? __extension__ typedef
4.bits/typesizes.h
#define __TIME_T_TYPE ? ? ? ? ? __SLONGWORD_TYPE
5. bits/types.h
#define __SLONGWORD_TYPE ? ? ? ?long int
這里,基本就可以得出結論了:
__extension__ typedef long int time_t
則time_t類型的變量最大值為0x7fffffff
代碼: #include<stdio.h> #include<sys/types.h> #include<time.h> int main(void) {time_t cur_time=time(NULL), max_time=0x7fffffff,new_time=max_time+1;printf("Cur time: cur_time=0x%08x/n", cur_time);printf("/tLocal: %s", asctime(localtime(&cur_time)));printf("/tGMT : %s/n", asctime(gmtime(&cur_time)));printf("Max time: max_time=0x%08x/n", max_time);printf("/tLocal: %s", asctime(localtime(&max_time)));printf("/tGMT : %s/n", asctime(gmtime(&max_time)));printf("New time: new_time=0x%08x/n", new_time);printf("/tLocal: %s", asctime(localtime(&new_time)));printf("/tGMT : %s/n", asctime(gmtime(&new_time)));return 0; }
可以的到運行結果:
從結果得出,32位的time_t最遲能表示到2038年1月19日 11:14:07(Asia/Shanghai時間) 或2038年1月19日 03:14:07(GMT時間),再過1秒,time_t數據將變為負數,變為1901年12月14日 04:51:44(本地時間),或1901年12月13日 20:45:52(GMT時間).
借鑒出處:?http://blog.csdn.net/zhangyang0402/archive/2010/07/18/5744475.aspx
C語言中time_t數據類型詳細介紹
包含文件:<time.h> #ifndef __TIME_T #define __TIME_T ? ? /* 避免重復定義 time_t */ typedef long ? ? time_t; ? ?/* 時間值time_t 為長整型的別名*/ #endif 既然time_t實際上是長整型(long int),用來保存從1970年1月1日0時0分0秒到現在時刻的秒數。到未來的某一天,從一個時間點(一般是1970年1月1日0時0分0秒)到那時的秒數(即日歷時間)超出了長整形所能表示的數的范圍怎么辦?對time_t數據類型的值來說,它所表示的時間不能晚于2038年1月18日19時14分07秒。為了能夠表示更久遠的時間,一些編譯器廠商引入了64位甚至更長的整形數來保存日歷時間。比如微軟在Visual C++中采用了__time64_t數據類型來保存日歷時間,并通過_time64()函數來獲得日歷時間(而不是通過使用32位字的time()函數),這樣就可以通過該數據類型保存3001年1月1日0時0分0秒(不包括該時間點)之前的時間。 在time.h頭文件中,我們還可以看到一些函數,它們都是以time_t為參數類型或返回值類型的函數: double difftime(time_t time1, time_t time0); time_t mktime(struct tm * timeptr); time_t time(time_t * timer); char * asctime(const struct tm * timeptr); char * ctime(const time_t *timer); 此外,time.h還提供了兩種不同的函數將日歷時間(一個用time_t表示的整數)轉換為我們平時看到的把年月日時分秒分開顯示的時間格式tm: struct tm * gmtime(const time_t *timer); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? struct tm * localtime(const time_t * timer); 通過查閱MSDN,我們可以知道Microsoft C/C++ 7.0中時間點的值(time_t對象的值)是從1899年12月31日0時0分0秒到該時間點所經過的秒數,而其它各種版本的Microsoft C/C++和所有不同版本的Visual C++都是計算的從1970年1月1日0時0分0秒到該時間點所經過的秒數。
C語言中的time函數<time.h>
/* * time.h* This file has no copyright assigned and is placed in the Public Domain.* 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 <_mingw.h>#define __need_wchar_t #define __need_size_t #define __need_NULL #ifndef RC_INVOKED #include <stddef.h> #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 current 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 _TIME32_T_DEFINED typedef __int32 __time32_t; #define _TIME32_T_DEFINED #endif#ifndef __STRICT_ANSI__ /* A 64-bit time_t to get to Y3K */ #ifndef _TIME64_T_DEFINED typedef __int64 __time64_t; #define _TIME64_T_DEFINED #endif #endif#ifndef _TIME_T_DEFINED /* FIXME __STRICT_ANSI__ ! */ #if __MSVCRT_VERSION__ >= 0x0800 #ifndef _USE_32BIT_TIME_T typedef __time64_t time_t; #else typedef __time32_t time_t; #endif /* !_USE_32BIT_TIME_T */ #else typedef __time32_t time_t; #endif /* __MSVCRT_VERSION__ >= 0x0800 */ #define _TIME_T_DEFINED #endif/** A type for measuring processor time (in clock ticks).*/ #ifndef _CLOCK_T_DEFINED typedef 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.*/ struct tm {int 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_yday; /* Days since Jan. 1: 0-365 */int tm_isdst; /* +1 Daylight Savings Time, 0 No DST,* -1 don't know */ }; #define _TM_DEFINED #endif#ifdef __cplusplus extern "C" { #endif_CRTIMP clock_t __cdecl __MINGW_NOTHROW clock (void); #if __MSVCRT_VERSION__ < 0x0800 _CRTIMP time_t __cdecl __MINGW_NOTHROW time (time_t*); _CRTIMP double __cdecl __MINGW_NOTHROW difftime (time_t, time_t); _CRTIMP time_t __cdecl __MINGW_NOTHROW mktime (struct tm*); #endif/** 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 calling 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 __MINGW_NOTHROW asctime (const struct tm*); #if __MSVCRT_VERSION__ < 0x0800 _CRTIMP char* __cdecl __MINGW_NOTHROW ctime (const time_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW gmtime (const time_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW localtime (const time_t*); #endif_CRTIMP size_t __cdecl __MINGW_NOTHROW strftime (char*, size_t, const char*, const struct tm*);#ifndef __STRICT_ANSI__extern _CRTIMP void __cdecl __MINGW_NOTHROW _tzset (void);#ifndef _NO_OLDNAMES extern _CRTIMP void __cdecl __MINGW_NOTHROW tzset (void); #endif_CRTIMP char* __cdecl __MINGW_NOTHROW _strdate(char*); _CRTIMP char* __cdecl __MINGW_NOTHROW _strtime(char*);/* These require newer versions of msvcrt.dll (6.10 or higher). */ #if __MSVCRT_VERSION__ >= 0x0601 _CRTIMP __time64_t __cdecl __MINGW_NOTHROW _time64( __time64_t*); _CRTIMP __time64_t __cdecl __MINGW_NOTHROW _mktime64 (struct tm*); _CRTIMP char* __cdecl __MINGW_NOTHROW _ctime64 (const __time64_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _gmtime64 (const __time64_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _localtime64 (const __time64_t*); #endif /* __MSVCRT_VERSION__ >= 0x0601 *//* These require newer versions of msvcrt.dll (8.00 or higher). */ #if __MSVCRT_VERSION__ >= 0x0800 _CRTIMP __time32_t __cdecl __MINGW_NOTHROW _time32 (__time32_t*); _CRTIMP double __cdecl __MINGW_NOTHROW _difftime32 (__time32_t, __time32_t); _CRTIMP double __cdecl __MINGW_NOTHROW _difftime64 (__time64_t, __time64_t); _CRTIMP __time32_t __cdecl __MINGW_NOTHROW _mktime32 (struct tm*); _CRTIMP __time32_t __cdecl __MINGW_NOTHROW _mkgmtime32 (struct tm*); _CRTIMP __time64_t __cdecl __MINGW_NOTHROW _mkgmtime64 (struct tm*); _CRTIMP char* __cdecl __MINGW_NOTHROW _ctime32 (const __time32_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _gmtime32 (const __time32_t*); _CRTIMP struct tm* __cdecl __MINGW_NOTHROW _localtime32 (const __time32_t*); #ifndef _USE_32BIT_TIME_T _CRTALIAS time_t __cdecl __MINGW_NOTHROW time (time_t* _v) { return(_time64 (_v)); } _CRTALIAS double __cdecl __MINGW_NOTHROW difftime (time_t _v1, time_t _v2) { return(_difftime64 (_v1,_v2)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW mktime (struct tm* _v) { return(_mktime64 (_v)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW _mkgmtime (struct tm* _v) { return(_mkgmtime64 (_v)); } _CRTALIAS char* __cdecl __MINGW_NOTHROW ctime (const time_t* _v) { return(_ctime64 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW gmtime (const time_t* _v) { return(_gmtime64 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW localtime (const time_t* _v) { return(_localtime64 (_v)); } #else _CRTALIAS time_t __cdecl __MINGW_NOTHROW time (time_t* _v) { return(_time32 (_v)); } _CRTALIAS double __cdecl __MINGW_NOTHROW difftime (time_t _v1, time_t _v2) { return(_difftime32 (_v1,_v2)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW mktime (struct tm* _v) { return(_mktime32 (_v)); } _CRTALIAS time_t __cdecl __MINGW_NOTHROW _mkgmtime (struct tm* _v) { return(_mkgmtime32 (_v)); } _CRTALIAS char* __cdecl __MINGW_NOTHROW ctime (const time_t* _v) { return(_ctime32 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW gmtime (const time_t* _v) { return(_gmtime32 (_v)); } _CRTALIAS struct tm* __cdecl __MINGW_NOTHROW localtime (const time_t* _v) { return(_localtime32 (_v)); } #endif /* !_USE_32BIT_TIME_T */ #endif /* __MSVCRT_VERSION__ >= 0x0800 *//** _daylight: non zero if daylight savings time is used.* _timezone: difference in seconds between GMT and local time.* _tzname: standard/daylight savings 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 __MINGW_NOTHROW __p__daylight (void); extern _CRTIMP long* __cdecl __MINGW_NOTHROW __p__timezone (void); extern _CRTIMP char** __cdecl __MINGW_NOTHROW __p__tzname (void);__MINGW_IMPORT int _daylight; __MINGW_IMPORT long _timezone; __MINGW_IMPORT char *_tzname[2];#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___tzname)#else /* __DECLSPEC_SUPPORTED */__MINGW_IMPORT int _daylight_dll; __MINGW_IMPORT long _timezone_dll; __MINGW_IMPORT char* _tzname[2];#define _daylight _daylight_dll #define _timezone _timezone_dll#endif /* __DECLSPEC_SUPPORTED */#endif /* not __MSVCRT__ */#endif /* Not __STRICT_ANSI__ */#ifndef _NO_OLDNAMES#ifdef __MSVCRT__/* These go in the oldnames import library for MSVCRT. */ __MINGW_IMPORT int daylight; __MINGW_IMPORT long timezone; __MINGW_IMPORT char *tzname[2];#else /* not __MSVCRT__ *//* CRTDLL is royally messed up when it comes to these macros.TODO: import and alias these via oldnames import library instead of macros. */#define daylight _daylight /* NOTE: timezone not defined as macro because it would conflict withstruct timezone in sys/time.h.Also, tzname used to a be macro, but now it's in moldname. */ __MINGW_IMPORT char *tzname[2];#endif /* not __MSVCRT__ */#endif /* Not _NO_OLDNAMES */#ifndef _WTIME_DEFINED /* wide function prototypes, also declared in wchar.h */ #ifndef __STRICT_ANSI__ #ifdef __MSVCRT__ _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wasctime(const struct tm*); #if __MSVCRT_VERSION__ < 0x0800 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wctime(const time_t*); #endif _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wstrdate(wchar_t*); _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wstrtime(wchar_t*); #if __MSVCRT_VERSION__ >= 0x0601 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wctime64 (const __time64_t*); #endif #if __MSVCRT_VERSION__ >= 0x0800 _CRTIMP wchar_t* __cdecl __MINGW_NOTHROW _wctime32 (const __time32_t*); #ifndef _USE_32BIT_TIME_T _CRTALIAS wchar_t* __cdecl __MINGW_NOTHROW _wctime (const time_t* _v) { return(_wctime64 (_v)); } #else _CRTALIAS wchar_t* __cdecl __MINGW_NOTHROW _wctime (const time_t* _v) { return(_wctime32 (_v)); } #endif #endif /* __MSVCRT_VERSION__ >= 0x0800 */ #endif /* __MSVCRT__ */ #endif /* __STRICT_ANSI__ */ _CRTIMP size_t __cdecl __MINGW_NOTHROW 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_ */
C語言中兩種方式表示時間日期值time_t和struct tm類型的相互轉換 ① ? ? 使用gmtime函數或localtime函數將time_t類型的時間日期轉換為struct tm類型: 使用time函數返回的是一個long值,該值對用戶的意義不大,一般不能根據其值確定具體的年、月、日等數據。gmtime函數可以方便的對time_t類型數據進行轉換,將其轉換為tm結構的數據方便數據閱讀。 gmtime函數的原型如下: struct tm *gmtime(time_t *timep); localtime函數的原型如下: struct tm *localtime(time_t *timep); 將參數timep所指的time_t類型信息轉換成實際所使用的時間日期表示方法,將結果返回到結構tm結構類型的變量。 gmtime函數用來存放實際日期時間的結構變量是靜態分配的,每次調用gmtime函數都將重寫該結構變量。如果希望保存結構變量中的內容,必須將其復制到tm結構的另一個變量中。 gmtime函數與localtime函數的區別: gmtime函數返回的時間日期未經時區轉換,是UTC時間(又稱為世界時間,即格林尼治時間)。 localtime函數返回當前時區的時間, 轉換日期時間表示形式time_t類型轉換為struct tm類型示例: #include <stdio.h> #include <time.h> #include<string.h> int main() {const char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字符數組*/time_t t;struct tm *p;t=time(NULL);/*獲取從1970年1月1日零時到現在的秒數,保存到變量t中*/p=gmtime(&t); /*變量t的值轉換為實際日期時間的表示格式*/printf("%d年%02d月%02d日",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);printf(" %s ", wday[p->tm_wday]);printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);return 0; }// time(&rawtime); // 獲取時間,以秒計,從1970年1月一日起算,存于rawtime // timeinfo=localtime(&rawtime); //轉為當地時間,tm 時間結構 // printf("The current data/time is &s ",asctime(timeinfo)); //asctime() // 轉為標準ASCII時間格式: 注意:p=gmtime(&t);此行若改為p=localtime(&t);則返回當前時區的時間
輸出結果:
② ? ? 使用mktime函數將struct tm類型的時間日期轉換為time_t類型:
表頭文件
#include <time.h>
定義函數
time_t mktime(strcut tm * timeptr);
函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
?
日期轉換為秒數示例:
#include <stdio.h> #include <time.h> #include <cstdlib> int main() {time_t t;struct tm stm;printf("請輸入日期時間值(按yyyy/mm/dd hh:mm:ss格式):");scanf("%d/%d/%d %d:%d:%d",&stm.tm_year,&stm.tm_mon,&stm.tm_mday,&stm.tm_hour,&stm.tm_min,&stm.tm_sec); stm.tm_year-=1900; /*年份值減去1900,得到tm結構中保存的年份序數*/ stm.tm_mon-=1; /*月份值減去1,得到tm結構中保存的月份序數*/ t=mktime(&stm); /* 若用戶輸入的日期時間有誤,則函數返回值為-1*/ if(-1==t) {printf("輸入的日期時間格式出錯!\n");exit(1); } printf("1970/01/01 00:00:00~%d/%02d/%02d %02d:%02d:%02d共%d秒\n",stm.tm_year+1900,stm.tm_mon,stm.tm_mday,stm.tm_hour,stm.tm_min,stm.tm_sec,t);return 0; }
輸出結果:
其中出現的一些小問題: char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字符數組*/
error:deprecated conversation from strig constant to 'char'
當我們將一個character pointer variable 初始化成一個string literal的時候, 就會出現此類錯誤。
解決方法:const ?char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指針字符數組*/ 方案2:使用string:string x = "hello";? 方案3:將string literal轉型為char* 的type: char* x = (char*)"hello";
if(-1==t) { printf("輸入的日期時間格式出錯!\n"); exit(1); }
error: ‘exit’ was not declared in this scope 的解決方法
解決方法:添加#include <cstdlib>
最后貼一篇博客 c語言中time函數的用法 http://blog.csdn.net/wangluojisuan/article/details/7045592/
總結
 
                            
                        - 上一篇: 魅族android9更新,魅族 17 系
- 下一篇: C# 调用ffmepg 读取海康或大华
