前段時間做了一個計費程序,其中涉及到有關(guān)日期與時間的計算,如求某日某時的前(或后)一段時間是什么時候,UNIX C系統(tǒng)本身并未提供此類函數(shù),筆者經(jīng)摸索,設(shè)計了一個求時間的函數(shù),現(xiàn)介紹給大家 。
功能介紹與參數(shù)說明;
該函數(shù)的主要功能是根據(jù)給定的日期時間及時長求出此前或后(bill_long為負)的日期時間及其星期 。;
參數(shù)說明如下:;
s:為給定的日期時間,如2001年6月2日18點30分04秒為“20010602183004”;
bill_long:給定的時間長度,單位為秒;
d_str:求出的日期,如2001年6月2日為“20010602”;
t_str:求出的時間,如18點28分06秒為“182806”;
函數(shù)返回值為星期,如星期日為0,星期一至六分別對應(yīng)1~6 。
實現(xiàn)代碼
該函數(shù)的具體實現(xiàn)代碼如下:
int GetDateTime( char s,long int bill_long,chard_str, char t_str) {
time_t timer,tim ;
struct tm tb, tb1 ;
int year_off = 1900 ;
int mon_off = 1 ;
char s1[20] ;
if ( strlen( s )!=14 )
return -1;
strncpy( s1, s, 4 );
s1[4] = "" ;
tb.tm_year = atoi( s1 );
strncpy( s1, s 4, 2 );
s1[2] = "" ;
tb.tm_mon = atoi( s1 );
strncpy( s1, s 6, 2 );
s1[2] = "" ;
tb.tm_mday = atoi( s1 );
if ( tb.tm_year==0 || tb.tm_mon==0 ||tb.tm_mday==0 )
return -1;
strncpy( s1, s 8, 2 );
s1[2] = "" ;
tb.tm_hour = atoi( s1 );
strncpy( s1, s 10, 2 );
s1[2] = "" ;
tb.tm_min = atoi( s1 );
strncpy( s1, s 12, 2 );
s1[2] = "" ;
tb.tm_sec = atoi( s1 );
tb.tm_year -= year_off ;
tb.tm_mon -= mon_off ;
tb.tm_isdst = 0 ;
tim=mktime( &tb ) ;
tim=tim-bill_long;
tb1=localtime(&tim);
sprintf(d_str, "%#04d%#02d%#02d",1900 tb1->tm_year,tb1->tm_mon 1,tb1->tm_mday);
sprintf(t_str, "%#02d%#02d%#02d",tb1->tm_hour, tb1->tm_min,tb1->tm_sec);
return (tb1->tm_wday);
} / end of GetDateTime /
【UNIX環(huán)境下的日期程序】
該函數(shù)不僅可求出某個時間前(后)一段時長的日期與時間,而且可得出這個日期是星期幾,給程序設(shè)計帶來不少便利,也方便了費用的計算與核實,讀者可直接調(diào)用該函數(shù) 。
推薦閱讀
- 蘋果怎么下載歌曲到本地
- 下 電腦診治之Windows故障詳解
- 抖音是騰訊旗下的軟件嗎
- 修改酷我音樂歌曲下載保存位置的方法
- 優(yōu)酷下載視頻的方法
- 解決edge“未下載flash播放器”以及“瀏覽器已內(nèi)置flash播放器”的方法
- UNIX系統(tǒng)被刪文件的恢復(fù)策略
- 為什么手機百度新聞打不開
- 教你一起玩小P新版城際通
- 失業(yè)證的作用和用途
