Skip to main content

Convert Japanese time expressions to datetime objects

Project description

Kanji to time

日本語の漢字などで書かれた年月日をPythonのdatetime型やtimedelta型に変換するライブラリ。
漢数字、旧字漢数字、全角などに対応しています。

内部では正規表現ではなくパーサージェネレータを使用しているので複雑なルールに対応しやすいようになっています。

使い方

pip install kanji_to_time
import kanji_to_time as ktt

text = "2024年4月5日22時30分4秒"
dt = ktt.to_datetime(text)
print(dt)

text = "二時間三十秒"
td = ktt.to_timedelta(text)
print(td)

対応パターン例

datetimeの変換

dt = ktt.to_datetime("2024年4月5日22時30分4秒")
self.assertEqual(
    dt, datetime(year=2024, month=4, day=5, hour=22, minute=30, second=4)
)

dt = ktt.to_datetime("2020年5月7日")
self.assertEqual(dt, datetime(year=2020, month=5, day=7))

dt = ktt.to_datetime("二〇二五年十二月七日")
self.assertEqual(dt, datetime(year=2025, month=12, day=7))

dt = ktt.to_datetime("二千年八月三日")
self.assertEqual(dt, datetime(year=2000, month=8, day=3))

dt = ktt.to_datetime("弐零弐参年伍月肆日")
self.assertEqual(dt, datetime(year=2023, month=5, day=4))

timedeltaの変換

td = ktt.to_timedelta("二時間三十秒")
self.assertEqual(td, timedelta(hours=2, seconds=30))

td = ktt.to_timedelta("六日間二時間五分間三秒間")
self.assertEqual(td, timedelta(days=6, hours=2, minutes=5, seconds=3))

td = ktt.to_timedelta("六日二時五分三秒")
self.assertEqual(td, timedelta(days=6, hours=2, minutes=5, seconds=3))

td = ktt.to_timedelta("90秒")
self.assertEqual(td, timedelta(seconds=90))

td = ktt.to_timedelta("マイナス七億分")
self.assertEqual(td, timedelta(minutes=-700_000_000))

td = ktt.to_timedelta("45秒前")
self.assertEqual(td, timedelta(seconds=-45))

td = ktt.to_timedelta("45秒後")
self.assertEqual(td, timedelta(seconds=45))

その他詳細なパターンはこちらのファイルを参照

対応している文法の構造自体を確認したい場合はLark定義ファイルを参照

ユニットテスト

pip install -r requirements.txt
python -m unittest discover -s tests

リンター

チェック

ruff check

フォーマット修正

ruff format

問い合わせ

バグや機能要望についてはissueに報告をお願いします。
https://github.com/corkborg/kanji_to_time/issues

その他の問い合わせはメールまで
corkborg@outlook.jp

リリースノート

v0.0.4

  • 萬阡拾のサポート
  • 百の桁周りの対応

v0.0.3

  • to_timedeltaで接尾辞の「前」、「後」のサポート。 (ex: 20秒前は-20秒に、20秒後は20秒に)
  • to_timedeltaで「プラス」、「+」などのサポート。(ex: +20分)
  • カンマで区切られた数値をサポート(ex: 2,000)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kanji_to_time-0.0.4.tar.gz (7.1 kB view details)

Uploaded Source

File details

Details for the file kanji_to_time-0.0.4.tar.gz.

File metadata

  • Download URL: kanji_to_time-0.0.4.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.3

File hashes

Hashes for kanji_to_time-0.0.4.tar.gz
Algorithm Hash digest
SHA256 65111f082157c23d702c4d7da7748ad206bdd8de89ffbb47b80f169ca8c8edda
MD5 f64426ffcf1b262d4b176b97d771ee0e
BLAKE2b-256 17918ac61ed3fb62d8461cbf1687ab54f9fb30204e33a31e0cb6da2707e00246

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page