[英]Python Datetime problem: Is There a better solution
我找到了以下使用python 3 datetime對象的問題的正確解決方案。 但是,我的解決方案似乎真的很混亂,我想知道是否可以得到一些幫助來清理它:
問題:完成which_date()函數,該函數將返回初始日期后指定時間段之后的日期。 可以用兩種不同的方式指定時間段:以天數表示,例如“ 1天”或“ 30天”,或以周數表示,例如“ 2周”或“ 12周”。
def which_date(start_date,time):
"""
This function takes as input a string depicting a date in YYYY/mm/dd
format and a string stating a time period in the form of "X day(s)" or
"Y week(s)". Output should be a string in form YYYY/mm/dd with the date
that is X days or Y weeks after the initial date.
"""
# Every thing after this comment and before 'return end_date' is my
#code to solve this probmem
start_date_split = start_date.split('/')
start_date_split = list(map(int, start_date_split))
year, month, day = start_date_split[0], start_date_split[1],
start_date_split[2]
start_date_date_obj = datetime.date(year, month, day)
time_split = time.split(' ')
time_amount = int(time_split[0])
days_or_weeks = time_split[1]
time_to_add = datetime.timedelta(0)
if 'day' in days_or_weeks:
time_to_add = datetime.timedelta(days = time_amount)
else:
time_to_add = datetime.timedelta(weeks = time_amount)
end_date_date_obj = start_date_date_obj + time_to_add
end_date = end_date_date_obj.strftime('%Y/%m/%d')
return end_date
以下是驗證測試:
def test():
assert which_date('2016/02/10','35 days') == '2016/03/16'
assert which_date('2016/12/21','3 weeks') == '2017/01/11'
assert which_date('2015/01/17','1 week') == '2015/01/24'
print("All tests completed.")
是否做了一些列表分解以減少某些代碼行。 還刪除了一些新變量的創建,而直接使用了這些值:
import datetime
def which_date(start_date,time):
"""
This function takes as input a string depicting a date in YYYY/mm/dd
format and a string stating a time period in the form of "X day(s)" or
"Y week(s)". Output should be a string in form YYYY/mm/dd with the date
that is X days or Y weeks after the initial date.
"""
# Every thing after this comment and before 'return end_date' is my
# code to solve this problem
year, month, day = [int(each) for each in start_date.split('/')]
start_date_date_obj = datetime.date(year, month, day)
time_amount, days_or_weeks = [int(value) if index==0 else value for index, value in enumerate(time.split(' '))]
time_to_add = datetime.timedelta(days = time_amount) if days_or_weeks=='days' else datetime.timedelta(weeks = time_amount)
return (start_date_date_obj + time_to_add).strftime('%Y/%m/%d')
def test():
assert which_date('2016/02/10','35 days') == '2016/03/16'
assert which_date('2016/12/21','3 weeks') == '2017/01/11'
assert which_date('2015/01/17','1 week') == '2015/01/24'
print("All tests completed.")
test()
您可以使用正則表達式以具有“ cleaner”功能:
import re
import datetime
def which_date(start_date, time):
# Split your date into a list by using non-decimal characters as separators
year, month, day = re.split(r'\D', start_date)
start_date_date_obj = datetime.date(int(year), int(month), int(day))
# Create group captures in order to get the quantity of day(s)/week(s)
# and pass those as generic arguments to timedelta
time_match = re.search(r'(\d+) (day|week)[s]?', time)
args = {time_match.group(2) + 's': int(time_match.group(1))}
time_to_add = datetime.timedelta(**args)
end_date_date_obj = start_date_date_obj + time_to_add
end_date = end_date_date_obj.strftime('%Y/%m/%d')
return end_date
這是先前答案的更簡單版本。 我是一個初學者,所以我在這里看不懂所有高級代碼。
import datetime
def which_date(start_date,time):
"""
This function takes as input a string depicting a date in YYYY/mm/dd
format and a string stating a time period in the form of "X day(s)" or
"Y week(s)". Output should be a string in form YYYY/mm/dd with the date
that is X days or Y weeks after the initial date.
"""
year, month, day = [int(each) for each in start_date.split('/')]
start_date_date_obj = datetime.date(year, month, day)
add = 1
string = time.split()
if string[1] == "days" or string[1] == "day":
add = int(string[0])
else:
add = (int(string[0]))*7
time_to_add = datetime.timedelta(days = add)
end_date = (start_date_date_obj + time_to_add).strftime('%Y/%m/%d')
return end_date
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.