简体   繁体   English

在python中添加时间添加功能

[英]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 :) PS我不能使用python内置函数:)

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) 如果检测到leap年则进行调整(每隔4年,但有特殊情况)
  • 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) 如果小于当前月份的天数,则结束,否则减去当前月份的天数并重试( while循环)
  • when last month reached, increase year 当上个月到达时,增加年份
  • add 1 to day and month in the end 最后在日期和月份中加1

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! 其他使用timedatetime time模块!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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