|
前段时间做了一个计费程序,其中涉及到有关日期与时间的计算,如求某日某时的前(或后)一段时间是什么时候,UNIX C系统本身并未提供此类函数,笔者经摸索,设计了一个求时间的函数,现介绍给大家。 功能介绍与参数说明 参数说明如下: s:为给定的日期时间,如2001年6月2日18点30分04秒为“20010602183004”; bill_long:给定的时间长度,单位为秒; d_str:求出的日期,如2001年6月2日为“20010602”; t_str:求出的时间,如18点28分06秒为“182806”; 函数返回值为星期,如星期日为0,星期一至六分别对应1~6。 实现代码 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] = '\0' ; tb.tm_year = atoi( s1 ); strncpy( s1, s+4, 2 ); s1[2] = '\0' ; tb.tm_mon = atoi( s1 ); strncpy( s1, s+6, 2 ); s1[2] = '\0' ; 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] = '\0' ; tb.tm_hour = atoi( s1 ); strncpy( s1, s+10, 2 ); s1[2] = '\0' ; tb.tm_min = atoi( s1 ); strncpy( s1, s+12, 2 ); s1[2] = '\0' ; 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 / |
| [返回上一页] [打 印] |
|
上一篇文章:SCO UNIX系统“root密码”丢失的处理 | 下一篇文章:通过COM口管理Linux服务器 |