import re
# 英文周几对照
dic_week = {
'Mon': 1, 'Tues': 2, 'Wed': 3,
'Thur': 4, 'Fri': 5, 'Sat': 6, 'Sun': 7,
}
# 英文月份对照
dic_month = {
'January': 1, 'jan': 1, 'Jan': 1,
'February': 2, 'feb': 2, 'Feb': 2,
'March': 3, 'mar': 3, 'Mar': 3,
'April': 4, 'apr': 4, 'Apr': 4,
'May': 5, 'may': 5, # 'May': 5,
'June': 6, 'jun': 6, 'Jun': 6,
'July': 7, 'jul': 7, 'Jul': 7,
'August': 8, 'aug': 8, 'Aug': 8,
'September': 9, 'sept': 9, 'Sept': 9,
'October': 10, 'oct': 10, 'Oct': 10,
'November': 11, 'nov': 11, 'Nov': 11,
'December': 12, 'dec': 12, 'Dec': 12,
}
# 时区
li_time_zone = ['+1400', '+1300', '+1200', '+1100', '+1000', '+0900', '+0800', '+0700', '+0600',
'+0500', '+0400', '+0300', '+0200', '+0100', '+0000', '-0100', '-0200', '-0300',
'-0400', '-0500', '-0600', '-0700', '-0800', '-0900', '-1000', '-1100', '-1200']
# 将月份按天数分开
thirty_one_days_mouth = [1, 3, 5, 7, 8, 10, 12]
thirty_days_mouth = [4, 6, 9, 11]
twenty_nine_days_mouth = [2]
# 获取用户输入的日期(按2019/11/01 10:07:25格式检查)
inp_date = input('请输入您需要检查的日期: ')
# 获取日期中数字
find_number = re.compile(r'd+')
get_numbers = find_number.findall(inp_date)
# 获取输入日期里的字母(月份,周几和‘am/pm')
find_letter = re.compile(r'[A-Za-z]+')
get_letter = find_letter.findall(inp_date)
# 其他格式日期转化为‘2019/11/01 10:07:25’格式
def change_format():
# 检查格式为‘2019/11/11’的日期
if len(get_numbers) == 3:
for a in range(0, 3):
get_numbers.append('0')
# 检查格式为‘September 17, 2012 10:09 am’的日期
elif len(get_numbers) == 4:
# 将get_numbers里‘年’和‘天’位置互换,转换为‘2019/11/01 10:07:25’格式
get_numbers[0], get_numbers[1] = get_numbers[1], get_numbers[0]
if get_letter[0] in dic_month:
get_numbers.insert(1, dic_month[get_letter[0]]) # 将月份转化成数字月份并放入get_numbers指定位置
get_numbers.append('0') # 将秒数位置加上,防止格式不正确
if get_letter[1] == 'am'or get_letter[1] == 'pm':
if int(get_numbers[3]) not in range(0, 12):
get_numbers[3] = 25
else:
pass
else:
get_numbers[3] = 25
else:
for a in range(0, 2):
get_numbers.append('100')
# 检查格式为‘"oct 7, 1970 5:57:51 PM"、"12 Feb 2006, 19:17:09"、"Mon, 02 Jan 2006 15:04:05"’的日期
elif len(get_numbers) == 5:
# 将get_numbers里‘年’和‘天’位置互换,转换为‘2019/11/01 10:07:25’格式
get_numbers[0], get_numbers[1] = get_numbers[1], get_numbers[0]
if len(get_letter) == 1: # 对"12 Feb 2006, 19:17:09"格式检查
if get_letter[0] in dic_month:
get_numbers.insert(1, dic_month[get_letter[0]]) # 将月份转化成数字月份并放入get_numbers指定位置
else:
get_numbers.append('100')
elif len(get_letter) == 2:
if get_letter[0] in dic_month: # 对"oct 7, 1970 5:57:51 PM"格式检查
get_numbers.insert(1, dic_month[get_letter[0]]) # 将月份转化成数字月份并放入get_numbers指定位置
if get_letter[1] == 'AM'or get_letter[1] == 'PM':
if int(get_numbers[3]) not in range(0, 12):
get_numbers[3] = 25
else:
pass
else:
get_numbers[3] = 25
elif get_letter[0] in dic_week: # 对"Mon, 02 Jan 2006 15:04:05"格式检查
if get_letter[1] in dic_month:
get_numbers.insert(1, dic_month[get_letter[1]]) # 将月份转化成数字月份并放入get_numbers指定位置
# 检查周几是否正确
def check_week():
sum_days = 0
# 1900年起以年为单位计算天数
for i in range(1900, int(get_numbers[0])):
if (i % 4 == 0 and i % 100 != 0) or i % 400 == 0:
sum_days += 366
else:
sum_days += 365
# 一月起以月为单位计算天数
for i in range(1, int(get_numbers[1])):
if i in thirty_one_days_mouth:
sum_days += 31
elif i in thirty_days_mouth:
sum_days += 30
elif i in twenty_nine_days_mouth:
if (get_numbers[0] % 4 == 0 and get_numbers[0] % 100 != 0) or get_numbers[0] % 400 == 0:
sum_days += 29
else:
sum_days += 28
else:
get_numbers[3] = 25
# 以日为单位计算天数
sum_days += int(get_numbers[2])
# 将日期对应的周几算出来并与输入的周几对比
week_day = 1+(sum_days-1) % 7
if week_day == dic_week[get_letter[0]]:
pass
else:
get_numbers[3] = 25
check_week()
else:
get_numbers.append('100')
else:
get_numbers.append('100')
else:
get_numbers.append('100')
# 检查格式为‘"2019/11/01 10:07:25"、"2019/11/01 10:07:25 AM"、"2014-12-16 06:20:00 UTC、"2014-12-16 06:20:00 GMT’的日期
elif len(get_numbers) == 6:
if len(get_letter) == 0:
pass
elif len(get_letter) == 1:
if get_letter[0] == 'AM'or get_letter[0] == 'PM':
if int(get_numbers[3]) not in range(0, 12):
get_numbers[3] = 25
else:
pass
elif get_letter[0] == 'GMT'or get_letter[0] == 'UTC':
pass
else:
get_numbers[3] = 25
else:
get_numbers[3] = 25
# 检查格式为‘2014-12-16 06:20:00 +0800 UTC’的日期
elif len(get_numbers) == 7:
if get_letter[0] == 'UTC':
find_time_zone_number = re.compile(r'[+|-]d+')
get_time_zone_numbers = find_time_zone_number.findall(inp_date)
if get_time_zone_numbers[2] in li_time_zone:
pass
else:
get_numbers[3] = 25
else:
get_numbers[3] = 25
# 对其他格式进行处理,防止下方代码报错
else:
for a in range(0, 7):
get_numbers.append('100')
change_format()
# 获取日期的年月日时分秒:(按2019/11/01 10:07:25格式)
che_year = int(get_numbers[0])
che_month = int(get_numbers[1])
che_day = int(get_numbers[2])
che_hour = int(get_numbers[3])
che_minute = int(get_numbers[4])
che_second = int(get_numbers[5])
# 对年月日进行检查:
def check_year_mouth_day():
if che_month in thirty_one_days_mouth:
if che_day in range(1, 32):
return True
else:
return False
elif che_month in thirty_days_mouth:
if che_day in range(1, 31):
return True
else:
return False
elif che_month in twenty_nine_days_mouth:
if (che_year % 4 == 0 & che_year % 100 != 0) or (che_year % 400 == 0): # 检查是否为闰年
if che_day in range(1, 30):
return True
else:
return False
else:
if che_day in range(1, 29):
return True
else:
return False
else:
return False
# 对时分秒进行检查:
# 只需要检查0≤小时≤23,0≤分钟(秒)≤59
def check_hour_minute_second():
if (che_hour in range(0, 24)) & (che_minute in range(0, 60)) & (che_second in range(0, 60)):
return True
else:
return False
# 执行检查年月日,时分秒函数并返回结果
check_year_mouth_day()
check_hour_minute_second()
if check_year_mouth_day() & check_hour_minute_second():
print("您要检查的日期是合法的")
else:
print("您要检查的日期是违规的")