Thread: Dateadd function

  1. #1
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41

    Dateadd function

    Please look at below code. I have tried for year but I have no
    idea how I go for rest(month,days,hour,minutes and second).


    Code:
    typedef struct{
    	unsigned int date : 5;
    	unsigned int month : 4;
    	unsigned int year : 14;
    	unsigned int hours : 5;
    	unsigned int minutes : 6;
    	unsigned int seconds : 6;
    } DL_DATE;
    
    //Converts the date in the d1 to string if fails returns 0
    int Dldate2Str(DL_DATE * d1,char *date);
    
    //Extract Date from date to d1 if fails returns 0
    int ParseDate(char * date,DL_DATE * d1)
    
    
    
    int DlDaysInMonth(char * date){
    	int days[]={31,0,31,30,31,30,31,31,30,31,30,31};
    	days[1]=((DlYear(date)%4==0 && DlYear(date)%100 !=0) || DlYear(date)%400==0)?29:28;
    	return days[DlMonth(date)-1];
    }
    int DlDaysInYear(char * date){
    	return 	((DlYear(date)%4==0 && DlYear(date)%100 !=0) || DlYear(date)%400==0)?366:365;
    }
    
    
    int DlDateAdd(char ch,int num,char * date){
    		/* y -years
    		 * m -months
    		 * d -days
    		 * h -hours
    		 * n -minutes
    		 * s -seconds
    		 */
    	DL_DATE d1;
    	char tempdate[20];
    	if(ParseDate(date,&d1)){
    	ch=tolower(ch);
    	switch(ch){
    		case 'y':
    			d1.year += num;
    			if ((int)d1.year < 0 || (int)d1.year > 9999 )
    				return 0;
    			Dldate2Str(&d1,date);			
    			break;
    		case 'm':
    		
    			break;
    		case 'd':
    					
    			break;
    		case 'h':
    			break;
    		case 'n':
    			break;
    		case 's':
    			break;
    		default:
    			break;
    	}
    	}else{
    		return 0;
    	}
    		
    	return 1;	
    	
    }
    
    Last edited by shiju; 12-31-2003 at 03:23 AM.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Dateadd function

    Originally posted by shiju
    Please look at below code. I have tried for year but I have no
    idea how I go for rest(month,days,hour,minutes and second).
    What does this mean?
    What are you trying to do?
    What is the code doing now?
    What is is supposed to be doing?
    What are the errors you are seeing?

    You really want do give us enough info to help you, don't you? You don't really want us to try to figure out what your code is supposed to do, do you?

    So many questions, so little time...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    I am not seeing any errors.
    If i call the function as below
    Code:
    char date[20]="01-01-2003";
    dldateadd('y',5,date);
    printf("%s",date);
    It shoud print

    01-01-2008

    if I pass first parameter as 'm'
    it should print

    01-06-2003

    (date as dd-mm-yyyy format)

    and for 'd'
    06-01-2003

    same for hours,minutes and seconds also if I pass second parameter as negetive value it should decrease the date.

    Code:
    char date[20]="01-01-2003";
    dldateadd('y',-5,date);
    printf("%s",date);  //should print "01-01-1998"
    The function when called as
    ParseDate("01-01-2003",&d1);

    Extract days,month,year,hour,minutes and second from the first parameter (char array) fills it into d1.

    Dldate2Str(&d1,date);
    Does opposit fills the char array date with the members in d1.

    Below two functions i didn't used in the Dldateadd function yet. I only included the year calculation But i feel it must be used for remaining switch case.

    I not getting how I write the remaining switch case

    int DlDaysInMonth(char * date);
    //Returns how many days in the inputed month;


    int DlDaysInyear(char * date)
    //Returns how many days in the inputed year if leap year returns 366 else 365

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    This line is your immediate problem:
    if ((int)d1.year < 0 && (int)d1.year > 9999 )

    What you have is the equivalent of
    if A is less than 1 and A is greater than 10 ...
    An impossible situation which is always false.

    Think the if thru again...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    O .....I changed it to OR

    I Apologize .. My mistake

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM