time 函數
?????? time_t??? tTime1 = 0;
?????? time_t??? tTime2 = 0;
?????? time_t??? tTime3 = 0;
?????? struct tm ???? *pTime = NULL;
?????? struct tm ???? sTime = {0};
?????? size_t??????????? timeLen = 0;
??????
?????? time(&tTime1);????????????????????// 以秒為單位獲得當前系統時間, int 型
?????? tTime2 = time(NULL);
????tTime3 = mktime(&sTime);?????????//
轉換本地時間為日歷值,若當地時間在
????????????????????????????????????// 其實是把秒數轉為年月日時分秒
???ptime = localtime(&tTime2);?????// 把一個時間數據轉換為當地時區的時間,若時間在
???????????????//
??????
?????? timeLen = strftime(?strTime,
???????????????????????????33,?
???????????????????????????"%Y(Y)%m(M)%d(D)%H(H)%M(M)%S(S)",
???????????????????????????
pTime);??????????????????//
格式化一個時間字符串
strftime()
格式化一個時間字符串
size_t ??? strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
這個例子中將以“星期 - 日 - 月 - 年”的格式顯示當前日期: ????
time_t ? LocalTime;???
struct???? tm *Today;???
char ??? Buffer[150],? Format[]=" 今天的日期是: %A %d %B %Y";????
time(&LocalTime);?????
Today=localtime(&LocalTime);????
strftime(Buffer,150,format , Today);??
cout<<Buffer;
_strtime()
把當前時間復制到緩沖區中,
char *_strtime( char *timestr );
這個例子中獲取了當前時間: ?
char buffer[10];??
_strtime(buffer);?
time()
以秒為單位獲取系統時間
time_t time( time_t *timer );
_utime()
為文件設置修改時間,若成功則返回0,否則返回 -1
int _utime( unsigned char *filename, struct _utimbuf *times );
這個例子中把文件 sample.txt 的修改時間變成當前的系統時間:
system("dir sample.txt");??
_utime("sample.txt",NULL);??
system("dir sample.txt");
asctime()
把 tm 型的變量轉換為一個字符串
char *asctime( const struct tm *timeptr );
得到系統當前日期和時間:
time_t Elapse;?
struct tm *Now;?
time(&Elapse);?
Now=locatime(&Elapse);?
cout<<" 當前的日期和時間為: "<<asctime(Now);
ctime()
把一個 time_t 型的時間值轉換為一個字符串,并且根據當地的時區設置對字符串進行調整
char *ctime( const time_t *timer );
這個例子中可以顯示當地時間: ??
time_t ltime;?
time(<ime);???
printf( "The time is %s\n", ctime( <ime ) );
difftime()
返回兩個 time_t 型變量之間的時間間隔
double difftime( time_t timer1, time_t timer0 );
這個例子中實現了計算電腦執行 100 萬次 for 語句所需的時間: ???
clock_t Start,Finish;????
long? n=
double Elapse;??
time(&Start);??
for( ; n--; ) { ; }??
time(&Finish);???
Elapsed= difftime(Finish,Start);
_ftime()
返回當前時間
void _ftime( struct _timeb *timeptr );
這個例子中把當前時間的信息寫入結構 timebuffer 中: ??
struct_time timebuffer;??
char *p;??
_ftime(&timebuffer);???
p=ctime(&(timebuffer.time));
_futime()
為某個已經打開的文件設置修改時間,若成功則返回 0 ,否則返回 -1
int _futime( int handle, struct _utimbuf *filetime );
這個例子中使用了 _futime 函數來把當前的系統時間設置為文件修改的時間: ????
int hFile;?
system("dir futime.c");??
hFile=_open("futime.c",_o_RDWR);??
if( _futime(hFile,NULL)==-1)??
perror("_futime failed\n");???
else??
printf( "File time modified\n");??
close (hfile);???
system("dir futime.c");
gmtime()
把一個 time_t 類型的數據轉換為 tm 類型
struct tm *gmtime( const time_t *timer );
這個例子中實現了以“星期、年月日、時分秒”的形式顯示當前時間:
long LocalTime;???
struct tm *GlobalTime;??
time(&LocalTime);??
GlobalTime=gmtime(&LocalTime);??
cout<<asctime(GlobalTime);
localtime()
把一個時間數據轉換為當地時區的時間,若時間在
struct tm *localtime( const time_t *timer );
mktime()
轉換本地時間為日歷值,若當地時間在
time_t mktime( struct tm *timeptr );
這個例子將顯示當前的時間和日期:
time_t Now,Result;?
struct? tm LocalTime;??
time(&Now);??
LocatTime-*locatimt(&Now);?
Result=mktime(&LocalTime);?
cout<<" 當前的時間和日期是: "<<asctime(&LocalTime);
_wutime()
為一個文件設置修改時間,若成功則返回 0 ,否則返回 -1 ,本函數只適用于寬字符編程
int _wutime( wchar_t *filename, struct _utimbuf *times );
_wstrtime()
把當前時間復制到緩沖區中,本函數只適用于寬字符編程
wchar_t *_wstrtime( wchar_t *timestr );
_wctime()
把一個 time_t 型的時間值轉換為一個字符串,并且根據當地的時區設置對字符串進行調整
wchar_t *_wctime( const time_t *timer );
wcsftime()
格式化一個時間字符串
size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );
wasctime()
把 tm 型的變量轉換為一個寬字符串
wchar_t *_wasctime( const struct tm *timeptr );
轉換取系統時間為在數據庫中取時間
?????? char cTime[] = {0};
?????? long?nTime = 0;
?????? DADBHandle????? handle;
?????? time_t??? t_time;
#if 0
?????? time(&t_now);
?????? tp = localtime(&t_now);
?????? ncurmin = tp->tm_min;
?????? ncurhour = tp->tm_hour;
#else
?????? rv = DB_ConnGet(&handle, 0, 0);
?????? if(DAESQLDB_OK != rv)
?????? {
????????????? LOG_ERR("err DA_EsqlDB_ConnGet")?
?????? }
?????? rv = DB_GetDBTime(handle, cTime);
?????? if(DAESQLDB_OK != rv)
?????? {??????????????????????????
????????????? LOG_ERR("err DA_EsqlDB_GetDBTime")
?????? }
?????? rv = DB_ConnFree(handle, 1);
?????? if(DAESQLDB_OK != rv)
?????? {
????????????? LOG_ERR("error in DA_EsqlDB_ConnFree")??? ???????
?????? }
?????? nTime = atol(cTime);
?????? t_time = time(&nTime);
?????? tp = localtime(&t_time);
?????? ncurmin = tp->tm_min;
?????? ncurhour = tp->tm_hour;
#endif??