简体   繁体   中英

Making a time adding function in python

I'm trying to build a function that recieves a date and adds days, updating everything in case it changes, so far i've come up with this:

def addnewDate(date, numberOfDays):

    date = date.split(":")   
    day = int(date[0])
    month = int(date[1])
    year = int(date[2])
    new_days = 0
    l = 0
    l1 = 28
    l2 = 30
    l3 = 31
    #l's are the accordingly days of the month

    while numberOfDays > l:
        numberOfDays  = numberOfDays - l 
        if month != 12:
            month += 1
        else:
            month = 1
            year += 1

        if month in [1, 3, 5, 7, 8, 10, 12]:
            l = l3
        elif month in [4, 6, 9, 11]:
            l = l2
        else:
            l = l1

    return  str(day) + ':' + str(month) + ':' + str(year) #i'll deal 
    #with fact that it doesn't put the 0's in the < 10 digits later

Desired output:

addnewDate('29:12:2016', 5):

'03:01:2017'

I think the problem is with either the variables, or the position i'm using them in, kinda lost though..

Thanks in advance!

ps I can't use python build in functions :)

Since you cannot use standard library, here's my attempt. I hope I did not forget anything.

  • define a table for month lengths
  • tweak it if leap year detected (every 4 year, but special cases)
  • work on zero-indexed days & months, much easier
  • add the number of days. If lesser that current month number of days, end, else, substract current month number of days and retry ( while loop)
  • when last month reached, increase year
  • add 1 to day and month in the end

code:

def addnewDate(date, numberOfDays):
    month_days = [31,28,31,30,31,30,31,31,30,31,30,31]

    date = date.split(":")
    day = int(date[0])-1
    month = int(date[1])-1
    year = int(date[2])
    if year%4==0 and year%400!=0:
        month_days[1]+=1

    new_days = 0
    #l's are the accordingly days of the month

    day += numberOfDays

    nb_days_month = month_days[month]

    done = False   # since you don't want to use break, let's create a flag
    while not done:
        nb_days_month = month_days[month]
        if day < nb_days_month:
            done = True
        else:
            day -= nb_days_month
            month += 1
            if month==12:
                year += 1
                month = 0


return  "{:02}:{:02}:{:04}".format(day+1,month+1,year)

test (may be not exhaustive):

for i in ("28:02:2000","28:02:2004","28:02:2005","31:12:2012","03:02:2015"):
    print(addnewDate(i,2))
    print(addnewDate(i,31))

result:

02:03:2000
31:03:2000
01:03:2004
30:03:2004
02:03:2005
31:03:2005
02:01:2013
31:01:2013
05:02:2015
06:03:2015

of course, this is just for fun. Else use time or datetime modules!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM