小六壬占卜代码
【代码逻辑】
- 将阳历转换成阴历
- 将时间转换为时辰和数字
- 根据这些时间输出结果
import datetime
import lunarcalendar
def solar_to_lunar(year, month, day):
solar_date = datetime.date(year, month, day)
lunar_date = lunarcalendar.SolarDate.from_date(solar_date).get_lunar()
return f"{lunar_date.year}年{lunar_date.month}月{lunar_date.day}日", lunar_date.month, lunar_date.day
def time_to_chinese_hour(hour, minute):
chinese_hours = [
("子时", 23, 1),
("丑时", 1, 2),
("寅时", 3, 3),
("卯时", 5, 4),
("辰时", 7, 5),
("巳时", 9, 6),
("午时", 11, 7),
("未时", 13, 8),
("申时", 15, 9),
("酉时", 17, 10),
("戌时", 19, 11),
("亥时", 21, 12),
]
for chinese_hour, start_hour, number in chinese_hours:
if start_hour <= hour < start_hour + 2:
return f"{hour}:{minute:02d} 属于 {chinese_hour},对应的数字是 {number}", number
return f"{hour}:{minute:02d} 属于 子时,对应的数字是 1", 1
def get_elements(n1, n2, n3):
elements = ["大安", "留连", "速喜", "赤口", "小吉", "空亡", "病符", "桃花", "天德"]
first_index = (n1 - 1) % len(elements)
second_index = (n1 + n2 - 2) % len(elements)
third_index = (n1 + n2 + n3 - 3) % len(elements)
return elements[first_index], elements[second_index], elements[third_index]
def get_result_from_solar_date(year, month, day, hour, minute):
lunar_date_str, lunar_month, lunar_day = solar_to_lunar(year, month, day)
print(f"阴历日期为: {lunar_date_str}")
time_result, hour_number = time_to_chinese_hour(hour, minute)
print(time_result)
result = get_elements(lunar_month, lunar_day, hour_number)
return result
year = 2024
month = 9
day = 24
hour = 19
minute = 42
final_result = get_result_from_solar_date(year, month, day, hour, minute)
print("最终结果为:", final_result)
【使用方法】
- 可以直接复制到 ChatGPT中运行,prompt :“请你运行上面的代码,帮我预测 , 2024.9.22, 19:42 的最终输出” (修改阳历时间)
- 复制代码到 python 中运行,(需要安装对应的库),输入“
year, month, day, hour, minute” - 复制代码到 python 中运行,(需要安装对应的库),使用下面的函数对阳历时间进行解析 输入示例:“
2024.9.22, 19:42” (逗号是英文逗号,逗号后面有一个空格)
def parse_datetime(datetime_str):
date_part, time_part = datetime_str.split(', ')
year, month, day = map(int, date_part.split('.'))
hour, minute = map(int, time_part.split(':'))
return year, month, day, hour, minute
【ChatGPT 直接运行】
""
import datetime
import lunarcalendar
def solar_to_lunar(year, month, day):
solar_date = datetime.date(year, month, day)
lunar_date = lunarcalendar.SolarDate.from_date(solar_date).get_lunar()
return f"{lunar_date.year}年{lunar_date.month}月{lunar_date.day}日", lunar_date.month, lunar_date.day
def time_to_chinese_hour(hour, minute):
chinese_hours = [
("子时", 23, 1),
("丑时", 1, 2),
("寅时", 3, 3),
("卯时", 5, 4),
("辰时", 7, 5),
("巳时", 9, 6),
("午时", 11, 7),
("未时", 13, 8),
("申时", 15, 9),
("酉时", 17, 10),
("戌时", 19, 11),
("亥时", 21, 12),
]
for chinese_hour, start_hour, number in chinese_hours:
if start_hour <= hour < start_hour + 2:
return f"{hour}:{minute:02d} 属于 {chinese_hour},对应的数字是 {number}", number
return f"{hour}:{minute:02d} 属于 子时,对应的数字是 1", 1
def get_elements(n1, n2, n3):
elements = ["大安", "留连", "速喜", "赤口", "小吉", "空亡", "病符", "桃花", "天德"]
first_index = (n1 - 1) % len(elements)
second_index = (n1 + n2 - 2) % len(elements)
third_index = (n1 + n2 + n3 - 3) % len(elements)
return elements[first_index], elements[second_index], elements[third_index]
def get_result_from_solar_date(year, month, day, hour, minute):
lunar_date_str, lunar_month, lunar_day = solar_to_lunar(year, month, day)
print(f"阴历日期为: {lunar_date_str}")
time_result, hour_number = time_to_chinese_hour(hour, minute)
print(time_result)
result = get_elements(lunar_month, lunar_day, hour_number)
return result
year = 2024
month = 9
day = 24
hour = 19
minute = 42
final_result = get_result_from_solar_date(year, month, day, hour, minute)
print("最终结果为:", final_result)
"""
请你详细分析理解上面的代码。请你帮我运行上面的代码,帮我预测, ”2024.9.22, 19:42“ 的最终输出。请确保结果是准确的,不能出现失误。感谢你专业的帮助。
【ChatGPT询问进阶】