LoginSignup
2
1

More than 3 years have passed since last update.

和暦西暦変換を如何に完全に処理するか

Last updated at Posted at 2019-05-21

text値の日付と思われる値を、date型の日付値に変換処理を作る事になり
あーでもないこーでもないと頭を悩ませた結果が以下の処理です。
最大の懸案は、「2桁の数値のみ」の年を如何に正しく変換するか、なのですが
今のところ正解がわかりません(´・ω・`)
19[63]と昭和[63]とかどうしたもんですかね?

想定するtextは
「2015年1月1日」「15年1月1日」「2015年01月01日」
「2015/1/1」「2015/01/01」「15/1/1」「15/01/01」
「2015-1-1」「2015-01-01」「15-1-1」「15-01-01」
「2015.1.1」「2015.01.01」「15.1.1」「15.01.01」
「平成27年1月1日」「平成27年01月01日」「H27年1月1日」「H27年01月01日」
「H27/1/1」「H27/01/01」
「H27-1-1」「H27-01-01」
「H27.1.1」「H27.01.01」
ざっとこんな感じです。

とりあえず作った処理は↓な感じ。

datetextToDate.py
    def datetextToDate(self, date):
        #渡された日付を日付型にして返す
        #和暦対応
        list_wareki = ["昭和", 1925], ["平成", 1988], ["令和", 2018], ["S", 1925], ["H", 1988], ["R", 2018]
        #
        #引数を取得
        date_text = date
        #
        #和暦判定
        for nengou, nensuu in list_wareki:
            result_find_date_text_type = date_text.find(nengou)
            if result_find_date_text_type != -1:
                year_add = nensuu
                break
            #
        #
        #日付区切り毎に処理を行う
        #区切りが「年月日」
        if (date_text.find("年") != -1):
            search_date_regular = "((?<![0-9])[0-9]{1,4})年([0-9]{1,2})月([0-9]{1,2})日"
        #区切りが「/」
        elif (date_text.find("/") != -1):
            search_date_regular = "((?<![0-9])[0-9]{1,4})/([0-9]{1,2})/([0-9]{1,2})"
        #区切りが「-」
        elif (date_text.find("-") != -1):
            search_date_regular = "((?<![0-9])[0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})"
        #区切りが「.」
        elif (date_text.find(".") != -1):
            search_date_regular = "((?<![0-9])[0-9]{1,4})\.([0-9]{1,2})\.([0-9]{1,2})"
        #区切りが見つからなかった場合
        else:
            return "日付の区切り記号が判定出来ませんでした。:" + date, False
        #
        #日付を各要素に分解する
        search_result_date = re.search(search_date_regular, date_text)
        #
        #日付が入っていない場合は処理を抜ける
        if search_result_date == None:
            return "年月日の3つが取得出来ませんでした:" + date, False
        #
        y = int(search_result_date.group(1))
        m = int(search_result_date.group(2))
        d = int(search_result_date.group(3))
        #
        #和暦の場合
        if result_find_date_text_type != -1:
            y = y + year_add
        else:
            #西暦だが2桁の場合
            if len(str(y)) == 2:
                y = y + 2000
        #
        #日付チェック
        try:
            newDataStr="%04d/%02d/%02d"%(y,m,d)
            newDate=datetime.datetime.strptime(newDataStr,"%Y/%m/%d")
            #return True
        except ValueError:
            return "日付が正しい値ではありませんでした:" + date, False
        #
        #日付に変換する
        date_dt = str(datetime.date(y, m, d))
        date_dt = datetime.datetime.strptime(date_dt, "%Y-%m-%d")
        #
        #変換後の値を返す
        return date_dt, True
2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1