Hi Folks,
 
I have written a function that takes a date and an integer representing the number of days.
 
Please test it, comment on the logic .
 
Here is the code:
<CODE BEGINS>
 

""" The function takes two arguments, date and  number of days. It checks for the right format and if the format is okay, it calculates the end date. """

import string
import datetime
import traceback


def dateCheck(date1,num_days):
    flag = True
    startdate = None
    enddate = None
   
    if num_days < 0 or num_days > 31:
        flag = False
        print "The argument for -n has to be between 0 and 31"
        return (flag,startdate,enddate)
   
    else:
       
        date_lst = string.split(date1,"/")
        ln = len(date_lst)
        if ln != 3 :
           
            flag = False
            print "The argument for -D option has to be in the format: dd/mm/yyyy"
            return (flag,startdate,enddate)
        else:
            date_lst.reverse()
            print date_lst
            try:
                if int(date_lst[0]) < 2000:
                    flag = False
                    print "The year cannot be earlier than 2000 and it should be in the format 'yyyy'."
                    return (flag,startdate,enddate)
            except ValueError:
                flag = False
                err_msg = traceback.format_exc()
                index = string.find(err_msg,'Value')
                print err_msg[index:]
                return (flag,startdate,enddate)
               
               
            try:
                startdate = datetime.date(int(date_lst[0]),int(date_lst[1]),int(date_lst[2]))
                enddate = startdate + datetime.timedelta(days=num_days)
                print startdate, enddate  

            except ValueError:
               
                flag = False
                startdate = None
                enddate = None
                err_msg = traceback.format_exc()
                index = string.find(err_msg,'Value')
                print err_msg[index:]
                return (flag,startdate,enddate)

            if enddate > datetime.date.today():
                enddate = datetime.date.today()
               

            if startdate > datetime.date.today():
                flag = False
                startdate = None
                enddate = None
                print "The -D option should have a date not later than today's date"
               
           
            return (flag,startdate,enddate)

<CODE ENDS>
 
 
 
 
 
TIA.
 
Regards,
Asrarahmed
 
 
 
 --
To HIM you shall return.
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to