time 函數(shù)

?

?????? 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);????????????????????// 以秒為單位獲得當(dāng)前系統(tǒng)時(shí)間, int

?????? tTime2 = time(NULL);

????tTime3 = mktime(&sTime);?????????// 轉(zhuǎn)換本地時(shí)間為日歷值,若當(dāng)?shù)貢r(shí)間在 1970 1 1
???????????????// 午夜之前,則返回 time_t 類型的 -

????????????????????????????????????// 其實(shí)是把秒數(shù)轉(zhuǎn)為年月日時(shí)分秒

?

???ptime = localtime(&tTime2);?????// 把一個(gè)時(shí)間數(shù)據(jù)轉(zhuǎn)換為當(dāng)?shù)貢r(shí)區(qū)的時(shí)間,若時(shí)間在

???????????????// 1970 1 1 日之前,返回 NULL

??????

?????? timeLen = strftime(?strTime,
???????????????????????????33,?
???????????????????????????"%Y(Y)%m(M)%d(D)%H(H)%M(M)%S(S)",
???????????????????????????
pTime);??????????????????// 格式化一個(gè)時(shí)間字符串

?

?

strftime()

格式化一個(gè)時(shí)間字符串

size_t ??? strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );

?

這個(gè)例子中將以“星期 - - - 年”的格式顯示當(dāng)前日期: ????

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()

把當(dāng)前時(shí)間復(fù)制到緩沖區(qū)中,

char *_strtime( char *timestr );

?

這個(gè)例子中獲取了當(dāng)前時(shí)間: ?

char buffer[10];??

_strtime(buffer);?

?

?

time()

以秒為單位獲取系統(tǒng)時(shí)間

time_t time( time_t *timer );

?

?

_utime()

為文件設(shè)置修改時(shí)間,若成功則返回0,否則返回 -1

int _utime( unsigned char *filename, struct _utimbuf *times );

?

這個(gè)例子中把文件 sample.txt 的修改時(shí)間變成當(dāng)前的系統(tǒng)時(shí)間:   

system("dir sample.txt");??

_utime("sample.txt",NULL);??

system("dir sample.txt");

?

?

asctime()

tm 型的變量轉(zhuǎn)換為一個(gè)字符串

char *asctime( const struct tm *timeptr );

?

得到系統(tǒng)當(dāng)前日期和時(shí)間:

time_t Elapse;?

struct tm *Now;?

time(&Elapse);?

Now=locatime(&Elapse);?

cout<<" 當(dāng)前的日期和時(shí)間為: "<<asctime(Now);

?

?

ctime()

把一個(gè) time_t 型的時(shí)間值轉(zhuǎn)換為一個(gè)字符串,并且根據(jù)當(dāng)?shù)氐臅r(shí)區(qū)設(shè)置對字符串進(jìn)行調(diào)整

char *ctime( const time_t *timer );

?

這個(gè)例子中可以顯示當(dāng)?shù)貢r(shí)間: ??

time_t ltime;?

time(&ltime);???

printf( "The time is %s\n", ctime( &ltime ) );

?

?

difftime()

返回兩個(gè) time_t 型變量之間的時(shí)間間隔

double difftime( time_t timer1, time_t timer0 );

?

這個(gè)例子中實(shí)現(xiàn)了計(jì)算電腦執(zhí)行 100 萬次 for 語句所需的時(shí)間: ???

clock_t Start,Finish;????

long? n=1000000L;??

double Elapse;??

time(&Start);??

for( ; n--; ) { ; }??

time(&Finish);???

Elapsed= difftime(Finish,Start);

?

?

_ftime()

返回當(dāng)前時(shí)間

void _ftime( struct _timeb *timeptr );

這個(gè)例子中把當(dāng)前時(shí)間的信息寫入結(jié)構(gòu) timebuffer 中: ??

struct_time timebuffer;??

char *p;??

_ftime(&timebuffer);???

p=ctime(&(timebuffer.time));

?

?

_futime()

為某個(gè)已經(jīng)打開的文件設(shè)置修改時(shí)間,若成功則返回 0 ,否則返回 -1

int _futime( int handle, struct _utimbuf *filetime );

這個(gè)例子中使用了 _futime 函數(shù)來把當(dāng)前的系統(tǒng)時(shí)間設(shè)置為文件修改的時(shí)間: ????

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()

把一個(gè) time_t 類型的數(shù)據(jù)轉(zhuǎn)換為 tm 類型

struct tm *gmtime( const time_t *timer );

這個(gè)例子中實(shí)現(xiàn)了以“星期、年月日、時(shí)分秒”的形式顯示當(dāng)前時(shí)間:   

long LocalTime;???

struct tm *GlobalTime;??

time(&LocalTime);??

GlobalTime=gmtime(&LocalTime);??

cout<<asctime(GlobalTime);

?

?

localtime()

把一個(gè)時(shí)間數(shù)據(jù)轉(zhuǎn)換為當(dāng)?shù)貢r(shí)區(qū)的時(shí)間,若時(shí)間在 1970 1 1 之前,返回 NULL

struct tm *localtime( const time_t *timer );

?

?

mktime()

轉(zhuǎn)換本地時(shí)間為日歷值,若當(dāng)?shù)貢r(shí)間在 1970 1 1 日午夜前 ,則返回 time_t 類型的 -

time_t mktime( struct tm *timeptr );

這個(gè)例子將顯示當(dāng)前的時(shí)間和日期: 

time_t Now,Result;?

struct? tm LocalTime;??

time(&Now);??

LocatTime-*locatimt(&Now);?

Result=mktime(&LocalTime);?

cout<<" 當(dāng)前的時(shí)間和日期是: "<<asctime(&LocalTime);

?

?

_wutime()

為一個(gè)文件設(shè)置修改時(shí)間,若成功則返回 0 ,否則返回 -1 ,本函數(shù)只適用于寬字符編程

int _wutime( wchar_t *filename, struct _utimbuf *times );

?

?

_wstrtime()

把當(dāng)前時(shí)間復(fù)制到緩沖區(qū)中,本函數(shù)只適用于寬字符編程

wchar_t *_wstrtime( wchar_t *timestr );

?

?

_wctime()

把一個(gè) time_t 型的時(shí)間值轉(zhuǎn)換為一個(gè)字符串,并且根據(jù)當(dāng)?shù)氐臅r(shí)區(qū)設(shè)置對字符串進(jìn)行調(diào)整

wchar_t *_wctime( const time_t *timer );

?

?

wcsftime()

格式化一個(gè)時(shí)間字符串

size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );

?

?

wasctime()

tm 型的變量轉(zhuǎn)換為一個(gè)寬字符串

wchar_t *_wasctime( const struct tm *timeptr );

?

轉(zhuǎn)換取系統(tǒng)時(shí)間為在數(shù)據(jù)庫中取時(shí)間

?

?????? 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??