部分已知日期之间的Python差异



我正在尝试计算当前日期和给定(部分已知(日期之间的差异。例如:下面列出了有效的日期格式(YYYY-MM-DD(。

YYYY-MM-DD
YYYY-MM-??
YYYY-??-??
????-??-??

例如,对于当天(2021-07-04(,结果将是

2021-12-02 -> 0 years, 5 months, however many days.
2022-06-?? -> 1 year, however many months
2077-??-?? -> ~56 Years
????-??-?? -> Unknown

所以基本上,当有一个??,该部分被忽略,我们只检查日期的下一个较高部分,例如,当天是否标有??我们只看年份和月份等

到目前为止,我总结了以下内容:

@staticmethod
def calculate_time_left(release_date):
# Only year is known:
if re.match("([0-9]{4})-??-??", release_date):
release_year = int(re.search("([0-9]{4})-??-??", release_date).group(1))
current_year = int(datetime.datetime.now().year)
if release_year == current_year:
return "This year."
else:
return "~{} year(s).".format(release_year - current_year)
elif re.match("([0-9]{4})-([0-9]{2})-??", release_date):
release_year = int(re.search("([0-9]{4})-([0-9]{2})-??", release_date).group(1))
current_year = int(datetime.datetime.now().year)
release_month = int(re.search("([0-9]{4})-([0-9]{2})-??", release_date).group(2))
current_month = int(datetime.datetime.now().month)
if release_year == current_year:
if release_month == current_month:
return "This month"
else:
return "~{} months".format(release_month - current_month)
else:
return "{} year(s) and {} months.".format(release_year - current_year, release_month - current_month)
elif re.match("([0-9]{4})-([0-9]{2})-([0-9]{2})", release_date):
release_year = int(re.search("([0-9]{4})-([0-9]{2})-([0-9]{2})", release_date).group(1))
current_year = int(datetime.datetime.now().year)
release_month = int(re.search("([0-9]{4})-([0-9]{2})-([0-9]{2})", release_date).group(2))
current_month = int(datetime.datetime.now().month)
release_day = int(re.search("([0-9]{4})-([0-9]{2})-([0-9]{2})", release_date).group(3))
current_day = int(datetime.datetime.now().day)
if release_year == current_year:
if release_month == current_month:
if release_day == current_day:
return "Today."
else:
return "{} days.".format(release_day - current_day)
else:
if release_day == current_day:
"~{} months.".format(release_month - current_month)
else:
return "~{} months and {} days.".format(release_month - current_month, release_day - current_day)
else:
if release_month == current_month:
if release_day == current_day:
return "{} year(s).".format(release_year - current_year)
else:
if release_day == current_day:
return "{} year(s) and {} months.".format(release_year - current_year, release_month - current_month)
else:
return "{} year(s), {} months and {} days.".format(release_year - current_year, release_month - current_month, release_day - current_day)
return "Release date unknown."

我在这里遇到了几个问题:

  1. 我得到了月和日差异的负值。例如,2022-01-02将返回1 year(s), -6 months and -2 days
  2. 我觉得我不需要对每种情况都进行所有的正则表达式匹配。也许有更好的方法可以做到这一点

如何正确解决此问题?我觉得我的方法很糟糕,而且容易出错。

对于负值,您可以使用绝对值函数来去除负值。我仍然在看你的代码,试图弄清楚你想要实现什么。这只是为了练习吗?此外,由于您没有将脚本用作任何实际目的的实际日期,因此只需将日期(yyyy((mm((dd(的每个部分视为一个正整数。不,我不认为你需要正则表达式来解决这个问题,但你知道一点正则表达式是很好的。将数字视为整数,并根据您想要显示输出的方式对其进行格式化。我自己还在学蟒蛇,只有我的两毛钱。

我不确定你的整体算法应该返回什么,但这里有一种评估日期部分的方法:

for date_string in ['2021-12-02', '2022-06-??', '2077-??-??', '????-??-??']:
year, month, day = date_string.split('-')
year_delta = month_delta = day_delta = 'unknown'
if year.count('?') == 0:  # if there are no ? in the year string
year_delta = int(year) - datetime.now().year
if month.count('?') == 0:
month_delta = int(month) - datetime.now().month
if day.count('?') == 0:
day_delta = int(day) - datetime.now().day

相关内容

  • 没有找到相关文章