苦節半年 4  四柱推命など東洋の占いのPython化

記事
占い
 東洋の占いのプログラム化はエクセルなど簡易版のものなど多数あるのでそれほど珍しくはない。では簡単かというとそうでもない。問題は東洋の60干支の暦にある。
 ChatGPTに依頼するとこのようなプログラムを吐き出すがこのまま使うことはできない。年の区切りと、月の区切りが違っている。したがってここに天体計算を加えて年と月の区切りを正確にしてやらないと使えないことがわかるだろう。結局にここに「苦節半年 1」の天体シュミレーションのプログラムでの節分のはじき出しなどが必要になる。同じことは九星にも言える。ほしい人は何度もGPTと対話して作ってもらうといい。
 私はもう淵海子平や六壬、紫微、奇門遁甲などはもっぱら使う占いからよけたので、特に東洋占いの暦は必要ではない。100年分の東洋暦もどっかやってしまった。作ろうと思えば作れるが、パソコンで使えるようにするには、パワーシェルとPIPが必要になり、かなり口頭やマニュアルでの説明で初心者が設定するのは厳しいのではないかと思う。


(私の入力)
日付から 年月日時間の干支をはじき出すプログラムが欲しい
ChatGPT
(ChatGPTの答え)
日付から年月日時間の干支を計算するプログラムをPythonで作成できます。干支を求めるには、以下のようなステップがあります:

西暦年を干支の年に変換する。
月の干支を計算する。
日の干支を計算する。
時間の干支を計算する。
プログラムの例
python
コードをコピーする
from datetime import datetime

# 干支リスト
heavenly_stems = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
earthly_branches = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]

# 年の干支を求める関数
def get_year_eto(year):
    stem_index = (year + 6) % 10
    branch_index = (year + 8) % 12
    return heavenly_stems[stem_index] + earthly_branches[branch_index]

# 月の干支を求める関数
def get_month_eto(year, month):
    # 立春(2月4日)を基準にするため、年の変わり目を考慮
    lunar_month = (month + 1) % 12
    if lunar_month == 0:
        lunar_month = 12
    lunar_year_stem_index = (year + 6) % 10
    stem_index = (2 * (lunar_year_stem_index % 5) + lunar_month) % 10
    branch_index = (lunar_month + 1) % 12
    return heavenly_stems[stem_index] + earthly_branches[branch_index]

# 日の干支を求める関数
def get_day_eto(year, month, day):
    base_date = datetime(1900, 1, 1)
    target_date = datetime(year, month, day)
    days_diff = (target_date - base_date).days
    stem_index = (days_diff + 9) % 10
    branch_index = (days_diff + 11) % 12
    return heavenly_stems[stem_index] + earthly_branches[branch_index]

# 時間の干支を求める関数
def get_hour_eto(year, month, day, hour):
    day_eto = get_day_eto(year, month, day)
    branch_index = earthly_branches.index(day_eto[1])
    hour_index = (hour + 1) // 2 % 12
    stem_index = (branch_index * 2 + hour_index) % 10
    return heavenly_stems[stem_index] + earthly_branches[hour_index]

# 入力された日付と時間から干支を求める関数
def get_eto(year, month, day, hour):
    year_eto = get_year_eto(year)
    month_eto = get_month_eto(year, month)
    day_eto = get_day_eto(year, month, day)
    hour_eto = get_hour_eto(year, month, day, hour)
    return year_eto, month_eto, day_eto, hour_eto

# 使用例
year = 2024
month = 7
day = 3
hour = 15

year_eto, month_eto, day_eto, hour_eto = get_eto(year, month, day, hour)
print(f"年の干支: {year_eto}")
print(f"月の干支: {month_eto}")
print(f"日の干支: {day_eto}")
print(f"時間の干支: {hour_eto}")
このプログラムは、指定した日付と時間に対応する年、月、日、時間の干支を計算し、結果を表示します。

注意点
干支の月計算には立春(2月4日)を基準とするため、1月と2月の初めは前年の干支が適用される可能性があります。
干支の時間計算は2時間ごとに変わるため、0時から2時、2時から4時、というように2時間単位で計算されます。
もし更にカスタマイズや他の要望があればお知らせください。

今のところ、この会話は役に立ちましたか?
ChatGPT の回答は必ずしも正しいとは限りません。重要な情報は確認するようにしてください。
サービス数40万件のスキルマーケット、あなたにぴったりのサービスを探す ココナラコンテンツマーケット ノウハウ記事・テンプレート・デザイン素材はこちら