要根据日期计算预订价格,通常需要考虑以下几个基础概念:
以下是一个简单的Python示例,展示如何根据日期计算预订价格:
from datetime import datetime
def calculate_price(check_in_date, base_price=100):
current_year = datetime.now().year
check_in_date = datetime(current_year, check_in_date.month, check_in_date.day)
# 定义旺季和淡季日期范围
peak_season_start = datetime(current_year, 6, 1)
peak_season_end = datetime(current_year, 9, 30)
# 定义节假日
holidays = [
datetime(current_year, 12, 25), # 圣诞节
datetime(current_year, 1, 1) # 元旦
]
# 计算价格
if peak_season_start <= check_in_date <= peak_season_end:
price = base_price * 1.5 # 旺季加价50%
else:
price = base_price
for holiday in holidays:
if check_in_date == holiday:
price *= 2 # 节假日翻倍
return price
# 示例使用
check_in_date = datetime(2023, 7, 15)
price = calculate_price(check_in_date)
print(f"预订日期 {check_in_date.strftime('%Y-%m-%d')} 的价格为: ${price:.2f}")
datetime
模块,方便处理日期和时间。通过以上方法,可以有效地根据日期计算预订价格,并确保系统的稳定性和准确性。
领取专属 10元无门槛券
手把手带您无忧上云