# Kanji to time
日本語の漢字などで書かれた年月日をPythonのdatetime型やtimedelta型に変換するライブラリ。
内部では正規表現ではなくパーサージェネレータを使用しているので複雑なルールに対応しやすいようになっています。
漢数字、旧字漢数字、全角などに対応しています。
## 使い方
```bash
pip install kanji_to_time
```
```python
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の変換
```python
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の変換
```python
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))
```
その他詳細なパターンはこちらのファイルを参照
* [tests/test_to_datetime.py](tests/test_to_datetime.py)
* [tests/test_to_timedelta.py](tests/test_to_timedelta.py)
* [tests/test_to_number.py](tests/test_to_number.py)
対応している文法の構造自体を確認したい場合はLark定義ファイルを参照
* [kanji_to_time/grammer/kanji_to_time.lark](kanji_to_time/grammer/kanji_to_time.lark)
## ユニットテスト
```bash
pip install -r requirements.txt
python -m unittest discover -s tests
```
## 問い合わせ
バグや機能要望についてはissueに報告をお願いします。<br/>
https://github.com/corkborg/kanji_to_time/issues
その他の問い合わせはメールまで(メール転送サービスを利用しています)<br/>
ax4squil8@mozmail.com
## リリースノート
### v0.0.3
* to_timedeltaで接尾辞の「前」、「後」のサポート。 20秒後(seconds-20)、20秒前(seconds=-20 )
* to_timedeltaで「プラス」、「+」などのサポート。 +20分(minutes=20)
Raw data
{
"_id": null,
"home_page": "https://github.com/corkborg/kanji_to_time",
"name": "kanji-to-time",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "kanji, timedelta, datetime, japanese",
"author": "corkborg",
"author_email": "ax4squil8@mozmail.com",
"download_url": "https://files.pythonhosted.org/packages/83/f6/8827a25767e4435e6c721dee22061dd5a9990949327e77f49469ab47a3c3/kanji_to_time-0.0.3.tar.gz",
"platform": null,
"description": "# Kanji to time\n\n\u65e5\u672c\u8a9e\u306e\u6f22\u5b57\u306a\u3069\u3067\u66f8\u304b\u308c\u305f\u5e74\u6708\u65e5\u3092Python\u306edatetime\u578b\u3084timedelta\u578b\u306b\u5909\u63db\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3002\n\u5185\u90e8\u3067\u306f\u6b63\u898f\u8868\u73fe\u3067\u306f\u306a\u304f\u30d1\u30fc\u30b5\u30fc\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u306e\u3067\u8907\u96d1\u306a\u30eb\u30fc\u30eb\u306b\u5bfe\u5fdc\u3057\u3084\u3059\u3044\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\n\n\u6f22\u6570\u5b57\u3001\u65e7\u5b57\u6f22\u6570\u5b57\u3001\u5168\u89d2\u306a\u3069\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u307e\u3059\u3002\n\n## \u4f7f\u3044\u65b9\n\n```bash\npip install kanji_to_time\n```\n\n```python\nimport kanji_to_time as ktt\n\ntext = \"2024\u5e744\u67085\u65e522\u664230\u52064\u79d2\"\ndt = ktt.to_datetime(text)\nprint(dt)\n\ntext = \"\u4e8c\u6642\u9593\u4e09\u5341\u79d2\"\ntd = ktt.to_timedelta(text)\nprint(td)\n```\n\n## \u5bfe\u5fdc\u30d1\u30bf\u30fc\u30f3\u4f8b\n\n### datetime\u306e\u5909\u63db\n\n```python\ndt = ktt.to_datetime(\"2024\u5e744\u67085\u65e522\u664230\u52064\u79d2\")\nself.assertEqual(dt, datetime(year=2024, month=4, day=5, hour=22, minute=30, second=4))\n\ndt = ktt.to_datetime(\"\uff12\uff10\uff12\uff10\u5e74\uff15\u6708\uff17\u65e5\")\nself.assertEqual(dt, datetime(year=2020, month=5, day=7))\n\ndt = ktt.to_datetime(\"\u4e8c\u3007\u4e8c\u4e94\u5e74\u5341\u4e8c\u6708\u4e03\u65e5\")\nself.assertEqual(dt, datetime(year=2025, month=12, day=7))\n\ndt = ktt.to_datetime(\"\u4e8c\u5343\u5e74\u516b\u6708\u4e09\u65e5\")\nself.assertEqual(dt, datetime(year=2000, month=8, day=3))\n\ndt = ktt.to_datetime(\"\u5f10\u96f6\u5f10\u53c2\u5e74\u4f0d\u6708\u8086\u65e5\")\nself.assertEqual(dt, datetime(year=2023, month=5, day=4))\n```\n\n### timedelta\u306e\u5909\u63db\n\n```python\ntd = ktt.to_timedelta(\"\u4e8c\u6642\u9593\u4e09\u5341\u79d2\")\nself.assertEqual(td, timedelta(hours=2, seconds=30))\n\ntd = ktt.to_timedelta(\"\u516d\u65e5\u9593\u4e8c\u6642\u9593\u4e94\u5206\u9593\u4e09\u79d2\u9593\")\nself.assertEqual(td, timedelta(days=6, hours=2, minutes=5, seconds=3))\n\ntd = ktt.to_timedelta(\"\u516d\u65e5\u4e8c\u6642\u4e94\u5206\u4e09\u79d2\")\nself.assertEqual(td, timedelta(days=6, hours=2, minutes=5, seconds=3))\n\ntd = ktt.to_timedelta(\"90\u79d2\")\nself.assertEqual(td, timedelta(seconds=90))\n\ntd = ktt.to_timedelta(\"\u30de\u30a4\u30ca\u30b9\u4e03\u5104\u5206\")\nself.assertEqual(td, timedelta(minutes=-700_000_000))\n```\n\n\u305d\u306e\u4ed6\u8a73\u7d30\u306a\u30d1\u30bf\u30fc\u30f3\u306f\u3053\u3061\u3089\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u53c2\u7167\n* [tests/test_to_datetime.py](tests/test_to_datetime.py)\n* [tests/test_to_timedelta.py](tests/test_to_timedelta.py)\n* [tests/test_to_number.py](tests/test_to_number.py)\n\n\u5bfe\u5fdc\u3057\u3066\u3044\u308b\u6587\u6cd5\u306e\u69cb\u9020\u81ea\u4f53\u3092\u78ba\u8a8d\u3057\u305f\u3044\u5834\u5408\u306fLark\u5b9a\u7fa9\u30d5\u30a1\u30a4\u30eb\u3092\u53c2\u7167\n* [kanji_to_time/grammer/kanji_to_time.lark](kanji_to_time/grammer/kanji_to_time.lark)\n\n## \u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\n\n```bash\npip install -r requirements.txt\npython -m unittest discover -s tests\n```\n\n## \u554f\u3044\u5408\u308f\u305b\n\n\u30d0\u30b0\u3084\u6a5f\u80fd\u8981\u671b\u306b\u3064\u3044\u3066\u306fissue\u306b\u5831\u544a\u3092\u304a\u9858\u3044\u3057\u307e\u3059\u3002<br/>\nhttps://github.com/corkborg/kanji_to_time/issues\n\n\u305d\u306e\u4ed6\u306e\u554f\u3044\u5408\u308f\u305b\u306f\u30e1\u30fc\u30eb\u307e\u3067\uff08\u30e1\u30fc\u30eb\u8ee2\u9001\u30b5\u30fc\u30d3\u30b9\u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059\uff09<br/>\nax4squil8@mozmail.com\n\n## \u30ea\u30ea\u30fc\u30b9\u30ce\u30fc\u30c8\n\n### v0.0.3\n\n* to_timedelta\u3067\u63a5\u5c3e\u8f9e\u306e\u300c\u524d\u300d\u3001\u300c\u5f8c\u300d\u306e\u30b5\u30dd\u30fc\u30c8\u3002 20\u79d2\u5f8c(seconds-20)\u300120\u79d2\u524d\uff08seconds=-20 \uff09\n* to_timedelta\u3067\u300c\u30d7\u30e9\u30b9\u300d\u3001\u300c+\u300d\u306a\u3069\u306e\u30b5\u30dd\u30fc\u30c8\u3002 +20\u5206\uff08minutes=20\uff09\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Convert Japanese time expressions to datetime objects",
"version": "0.0.3",
"project_urls": {
"Homepage": "https://github.com/corkborg/kanji_to_time"
},
"split_keywords": [
"kanji",
" timedelta",
" datetime",
" japanese"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "83f68827a25767e4435e6c721dee22061dd5a9990949327e77f49469ab47a3c3",
"md5": "5fd93baf666934d4b616395b293c4a1a",
"sha256": "7b16654c32026d7239428abcc4a4326f7622899fecbf775c6436bb71cfcb2b25"
},
"downloads": -1,
"filename": "kanji_to_time-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "5fd93baf666934d4b616395b293c4a1a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6938,
"upload_time": "2024-04-23T06:32:33",
"upload_time_iso_8601": "2024-04-23T06:32:33.425960Z",
"url": "https://files.pythonhosted.org/packages/83/f6/8827a25767e4435e6c721dee22061dd5a9990949327e77f49469ab47a3c3/kanji_to_time-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-23 06:32:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "corkborg",
"github_project": "kanji_to_time",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "lark",
"specs": [
[
"==",
"1.1.9"
]
]
},
{
"name": "ruff",
"specs": [
[
"==",
"0.3.7"
]
]
}
],
"lcname": "kanji-to-time"
}