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)
Metadata
Release files for kanji-to-time 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kanji_to_time-0.0.4.tar.gz | 7.1 kB | Without provenance | Details for kanji_to_time-0.0.4.tar.gz |
Release files / kanji_to_time-0.0.4.tar.gz
| Download URL | kanji_to_time-0.0.4.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
65111f082157c23d702c4d7da7748ad206bdd8de89ffbb47b80f169ca8c8edda
|
|
BLAKE2b-256 checksum How to use checksums |
17918ac61ed3fb62d8461cbf1687ab54f9fb30204e33a31e0cb6da2707e00246
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.3
|
Security for kanji-to-time 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.