Python - 日历专家 - 我的算法和逻辑是否正确?
Python - Calendar Experts - Whether My Algorithm And Logics Correct Or Not?
我做了一些研究并在 Python 中编写了一个程序来以这种格式显示回历(伊斯兰历)日期...
weekday_name, date, month, year
我想在我的一个应用程序中使用它。只是想知道我的代码是否正确。我离日历专家还差得很远。我很困惑我的代码是否遗漏了任何东西,最后日期误导了人们!任何人都可以检查代码并让我知道我做错了什么吗?
#Get current date.
import datetime
now = datetime.datetime.now()
year = now.year
month = now.month
day = now.day
weekday_number = now.weekday()
#Convert current date to Julian date.
from calverter import Calverter
cal = Calverter()
julian = cal.gregorian_to_jd(year,month,day)
#Convert Julian date to Islamic date.
hijri = cal.jd_to_islamic(julian)
#Convert Islamic date to weekday_name, day_number, month_name, year format.
hijri_year = hijri[0]
hijri_month = hijri[1]
hijri_day_number = hijri[2]
hijri_date = [weekday_number, hijri_day_number, hijri_month, hijri_year]
#Convert hijri_month (which are in number forms) into month names.
if hijri_month == 1:
hijri_date[2] = "Muharram"
elif hijri_month == 2:
hijri_date[2] = "Safar"
elif hijri_month == 3:
hijri_date[2] = "Rabi al Awwal"
elif hijri_month == 4:
hijri_date[2] = "Rabi ath Thani"
elif hijri_month == 5:
hijri_date[2] = "Jamada al Ula"
elif hijri_month == 6:
hijri_date[2] = "Jumada ath Thaniyah"
elif hijri_month == 7:
hijri_date[2] = "Rajab"
elif hijri_month == 8:
hijri_date[2] = "Shaban"
elif hijri_month == 9:
hijri_date[2] = "Ramadan"
elif hijri_month == 10:
hijri_date[2] = "Shawal"
elif hijri_month == 11:
hijri_date[2] = "Dhu al Qa'dah"
elif hijri_month == 12:
hijri_date[2] = "Dhu al Hijah"
#Convert weekday_numbers into weekday names.
if weekday_number == 0:
hijri_date[0] = "Al Ithnayn"
elif weekday_number == 1:
hijri_date[0] = "Ath Thulatha"
elif weekday_number == 2:
hijri_date[0] = "Al Arbia"
elif weekday_number == 3:
hijri_date[0] = "Al khamis"
elif weekday_number == 4:
hijri_date[0] = "Al Jumuah"
elif weekday_number == 5:
hijri_date[0] = "As Sabt"
elif weekday_number == 6:
hijri_date[0] = "Al Ahad"
print hijri_date
我用 pycalverter 库做了一些事情。如果有人想要,我可以 post pastebin 中的开源 pycalverter 代码。
我有几点建议。
首先,如果您不确定,您的代码是否正确(即使您确定)- 编写 运行 测试。你可以使用一个测试库(比如unittest
and etc.) or just implement some simple tests to make sure everything is working as you expect. If you are new to testing, you can find a lot of articles网上关于它的
其次,您所有的 if
都可以替换为两个列表(一个用于月,一个用于天):
# Just define 12 months names:
islamic_months = ["Muharram", "Safar", "Rabi al Awwal",
"Rabi ath Thani", "Jamada al Ula",
"Jumada ath Thaniyah", "Rajab", "Shaban",
"Ramadan", "Shawal", "Dhu al Qa'dah", "Dhu al Hijah"]
# On the next step get the right name:
hijri_date[2] = islamic_months[hijri_month - 1] # 1 <= hijri_month <= 12
天数也一样:
islamic_day_names = ["Al Ithnayn", "Ath Thulatha", "Al Arbia",
"Al khamis", "Al Jumuah", "As Sabt", "Al Ahad"]
hijri_date[0] = islamic_day_names[weekday_number] # 0 <= weekday_number <= 6
主要思想是您不必检查是哪一天或哪一个月。只需使用它的索引从列表中获取正确的名称。
我做了一些研究并在 Python 中编写了一个程序来以这种格式显示回历(伊斯兰历)日期...
weekday_name, date, month, year
我想在我的一个应用程序中使用它。只是想知道我的代码是否正确。我离日历专家还差得很远。我很困惑我的代码是否遗漏了任何东西,最后日期误导了人们!任何人都可以检查代码并让我知道我做错了什么吗?
#Get current date.
import datetime
now = datetime.datetime.now()
year = now.year
month = now.month
day = now.day
weekday_number = now.weekday()
#Convert current date to Julian date.
from calverter import Calverter
cal = Calverter()
julian = cal.gregorian_to_jd(year,month,day)
#Convert Julian date to Islamic date.
hijri = cal.jd_to_islamic(julian)
#Convert Islamic date to weekday_name, day_number, month_name, year format.
hijri_year = hijri[0]
hijri_month = hijri[1]
hijri_day_number = hijri[2]
hijri_date = [weekday_number, hijri_day_number, hijri_month, hijri_year]
#Convert hijri_month (which are in number forms) into month names.
if hijri_month == 1:
hijri_date[2] = "Muharram"
elif hijri_month == 2:
hijri_date[2] = "Safar"
elif hijri_month == 3:
hijri_date[2] = "Rabi al Awwal"
elif hijri_month == 4:
hijri_date[2] = "Rabi ath Thani"
elif hijri_month == 5:
hijri_date[2] = "Jamada al Ula"
elif hijri_month == 6:
hijri_date[2] = "Jumada ath Thaniyah"
elif hijri_month == 7:
hijri_date[2] = "Rajab"
elif hijri_month == 8:
hijri_date[2] = "Shaban"
elif hijri_month == 9:
hijri_date[2] = "Ramadan"
elif hijri_month == 10:
hijri_date[2] = "Shawal"
elif hijri_month == 11:
hijri_date[2] = "Dhu al Qa'dah"
elif hijri_month == 12:
hijri_date[2] = "Dhu al Hijah"
#Convert weekday_numbers into weekday names.
if weekday_number == 0:
hijri_date[0] = "Al Ithnayn"
elif weekday_number == 1:
hijri_date[0] = "Ath Thulatha"
elif weekday_number == 2:
hijri_date[0] = "Al Arbia"
elif weekday_number == 3:
hijri_date[0] = "Al khamis"
elif weekday_number == 4:
hijri_date[0] = "Al Jumuah"
elif weekday_number == 5:
hijri_date[0] = "As Sabt"
elif weekday_number == 6:
hijri_date[0] = "Al Ahad"
print hijri_date
我用 pycalverter 库做了一些事情。如果有人想要,我可以 post pastebin 中的开源 pycalverter 代码。
我有几点建议。
首先,如果您不确定,您的代码是否正确(即使您确定)- 编写 运行 测试。你可以使用一个测试库(比如unittest
and etc.) or just implement some simple tests to make sure everything is working as you expect. If you are new to testing, you can find a lot of articles网上关于它的
其次,您所有的 if
都可以替换为两个列表(一个用于月,一个用于天):
# Just define 12 months names:
islamic_months = ["Muharram", "Safar", "Rabi al Awwal",
"Rabi ath Thani", "Jamada al Ula",
"Jumada ath Thaniyah", "Rajab", "Shaban",
"Ramadan", "Shawal", "Dhu al Qa'dah", "Dhu al Hijah"]
# On the next step get the right name:
hijri_date[2] = islamic_months[hijri_month - 1] # 1 <= hijri_month <= 12
天数也一样:
islamic_day_names = ["Al Ithnayn", "Ath Thulatha", "Al Arbia",
"Al khamis", "Al Jumuah", "As Sabt", "Al Ahad"]
hijri_date[0] = islamic_day_names[weekday_number] # 0 <= weekday_number <= 6
主要思想是您不必检查是哪一天或哪一个月。只需使用它的索引从列表中获取正确的名称。