- 論壇徽章:
- 0
|
本帖最后由 浪子雄心 于 2011-05-07 21:13 編輯
/*
**
** 文件: time.c
**
** 功 能:關(guān)于時(shí)間操作的一些簡(jiǎn)單實(shí)現(xiàn)
**
** 編譯及運(yùn)行:
** $cc -o time -g -Wall time.c
** $./time
**
**
** 作 者:沒(méi)版權(quán),隨便用
**
** 說(shuō)明:本函數(shù)功能可以自由拷貝使用,
** 可能存在沒(méi)有發(fā)現(xiàn)在的Bug,
** 如有缺陷,還需使用者自行調(diào)試
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<time.h>
/*
** 功能說(shuō)明:得到指定日期的后一天
** 輸入?yún)?shù):struct tm *p
** 輸出參數(shù):int
** 返回值說(shuō)明:0- 成功
*/
int getNextDay(struct tm *p){
time_t timep;
p->tm_sec=0;
p->tm_min=0;
p->tm_hour=0;
timep = mktime(p)+24*60*60;
p=localtime(&timep);
return 0;
}
/*
** 功能說(shuō)明:是否為閏年
** 輸入?yún)?shù):struct tm *p
** 輸出參數(shù):int
** 返回值說(shuō)明:1-是 0- 否
*/
int isLeapYear(struct tm *p)
{
return ((1900+p->tm_year) & 3) == 0 && ((1900+p->tm_year)%100 || \
((1900+p->tm_year)%400 == 0 && (1900+p->tm_year))) ?1 : 0;
}
/*
** 功能說(shuō)明:是否為年未
** 輸入?yún)?shù):struct tm *p
** 輸出參數(shù):int
** 返回值說(shuō)明:1- 是 0- 否
*/
int isYearEnd(struct tm *p){
if( (11==p->tm_mon)&&(31== p->tm_mday)){
return 1;
}else{
return 0;
}
}
/*
** 功能說(shuō)明:是否為月未
** 輸入?yún)?shù):struct tm *p
** 輸出參數(shù):int
** 返回值說(shuō)明:1- 是 0- 否
*/
int isMonthEnd(struct tm *p){
int Mday[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(1== p->tm_mon){
Mday[p->tm_mon] += isLeapYear(p);
}
if( p->tm_mday == Mday[p->tm_mon]){
return 1;
}else{
return 0;
}
}
/*
** 功能說(shuō)明:是否是周未(周六與周日)
** 輸入?yún)?shù):struct tm *p
** 輸出參數(shù):int
** 返回值說(shuō)明:1- 是 0- 否
*/
int isWeekEnd(struct tm *p){
return((0==p->tm_wday)||(6==p->tm_wday))?1:0;
}
/*
** 功能說(shuō)明:從一個(gè)時(shí)間字符串 YYMMDD, YYYYMMDD,
** YYYYMMDDHHMMSS,YY-MM-DD, YYYY-MM-DD,
** YY-MM-DD HH.MM.SS
** 找到對(duì)應(yīng)的日期并轉(zhuǎn)為 struct tm 結(jié)構(gòu)
** 輸入?yún)?shù):char *pos, struct tm *p
** 輸出參數(shù):int
** 返回值說(shuō)明:0 -成功 -1 -日期識(shí)別出錯(cuò)
*/
int find_date(char *pos, struct tm *p)
{
int Mday[]={31,28,31,30,31,30,31,31,30,31,30,31};
int length,value,vek[4];
int flag=1*4+2*16;
char *start,*k;
k=pos;
bzero((char*) vek,sizeof(int)*4);
while (*pos && !isdigit(*pos))
pos++;
length=(int) strlen(pos);
int i=0;
for (i=0 ; i< 3; i++)
{
start=pos; value=0;
while (isdigit(pos[0]) &&
((pos-start) < 2 || ((pos-start) < 4 && length >= 8 &&
!(flag & 3))))
{
value=value*10 + (int) (char) (*pos - '0');
pos++;
}
vek[flag & 3]=value; flag>>=2;
while (*pos && (ispunct(*pos) || isspace(*pos)))
pos++;
}
char tmp[8];
bzero(tmp,sizeof(tmp));
sprintf(tmp,"%d",vek[0]);
if ((4 > strlen(tmp))&&(vek[0] < 70)){
vek[0]+= 2000;
}else if(4 != strlen(tmp)){
vek[0]+= 1900;
}
p->tm_year=vek[0]-1900;
p->tm_mon=vek[1]-1;
p->tm_mday=vek[2];
p->tm_sec=0;
p->tm_min=0;
p->tm_hour=0;
if( 0>vek[0] || vek[0]>9999){
printf("time str: [%s] Year err!\n",k);
return -1;
}
if( 0>vek[1] || vek[1]>12){
printf("time str: [%s] Month [%d] err!\n",k ,vek[1]);
return -1;
}
Mday[1]+=isLeapYear(p);
if( 0>vek[2] || vek[2]>Mday[vek[1]-1]){
printf("time str: [%s] Day [%d] err!\n",k , vek[2]);
return -1;
}
return 0;
}
int test(char *sTime){
printf("\n");
struct tm *p;
time_t timep;
time(&timep);
p=localtime(&timep);
printf(" 當(dāng)前日期 [%04d%02d%02d] \n", \
(1900+p->tm_year),( 1+p->tm_mon), p->tm_mday);
printf("被識(shí)別串為 [%s]\n",sTime);
if(0 != find_date(sTime, p)){
exit(0);
};
printf("日期識(shí)別為 [%04d%02d%02d]\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday);
getNextDay(p);
printf("第二天日期 [%04d%02d%02d]\n",1900+p->tm_year,1+p->tm_mon,p->tm_mday);
printf("是否為閏年 isLeapYear[%d]\n", isLeapYear(p));
printf("是否是周末 isWeekend[%d] %d\n", p->tm_wday,isWeekEnd(p));
printf("是否是月末 isMonthend[%04d%02d%02d] %d\n", \
1900+p->tm_year,1+p->tm_mon,p->tm_mday, isMonthEnd(p));
printf("是否是年末 isYearend[%04d%02d%02d] %d\n", \
1900+p->tm_year,1+p->tm_mon,p->tm_mday, isYearEnd(p));
printf("\n");
return 0;
}
int main(){
/*
識(shí)別格式
YYMMDD,
YYYYMMDD,
YYYYMMDDHHMMSS,
YY-MM-DD,
YYYY-MM-DD,
YY-MM-DD HH.MM.SS
*/
test("040528");
test("20051230 12:21:12");
test("20060227122112");
test("06-02-28");
test("2006-12-31");
test("06-07-22 12.34.56");
test("06-07-30 12.34.56");
return 0;
}
[ 本帖最后由 浪子雄心 于 2006-7-26 16:47 編輯 ] |
|