Code Monkey home page Code Monkey logo

japanera's Issues

Unable to parse single-digit months or dates

Thank you for making such a helpful library. It does everything I need, but it seems to have a small bug.

When I run the following code, it fails to parse the date.

from japanera import Japanera
janera = Japanera()

janera.strptime('昭和27年3月14日','%-E%-O年%m月%d日')
# => []

The issue appears to be with this line of code:
https://github.com/delta114514/Japanera/blob/042996abc07d46c8790ff98f74a38702cf4ae3b3/japanera/japanera.py#L970

I think the problem is that %m and %d are ambiguous during parsing - they will parse either single-digit values or zero-padded values, but when formatting, they will always output the zero-padded form. So your check probably fails because

"昭和27年3月14日" !=
"昭和27年03月14日"

After commenting out the line I mentioned, it parses the date properly.

1926年12月24日の元号の扱い

初めまして。
EraDateの使用法について質問です。

print(EraDate(1926, 12, 23).strftime("%-E%-o年%m月%d日"))
print(EraDate(1926, 12, 24).strftime("%-E%-o年%m月%d日"))
print(EraDate(1926, 12, 25).strftime("%-E%-o年%m月%d日"))

上記のコードを実行すると出力結果が以下のようになります。

大正15年12月23日
西暦26年12月24日
昭和01年12月25日

これはバグでしょうか?それとも仕様でしょうか?
また、1926年12月24日を「大正」として扱うにはどのようにすればよいでしょうか。
アプリケーション側で対処するしかないのでしょうか。

よろしくお願いします。

Leap year

すごいライブラリですね!もともと生でdatetime.datetime.strptime() を使っていたのですが、困ったことがあったのでライブラリを探しているところでした。 datetime.strptime() は西暦でちゃんと入れないと閏年の扱いに失敗するみたいです。なので、以下の挙動もバグだと思います。

Python 3.8.2 (default, Feb 27 2020, 19:58:50) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import japanera
>>> janera = japanera.Japanera()
>>> janera.strptime("令和02年02月01日", "%-E%-O年%m月%d日")
[datetime.datetime(2020, 2, 1, 0, 0)]
>>> janera.strptime("令和02年02月29日", "%-E%-O年%m月%d日")
[]

datetime はこうなります。

>>> from datetime import datetime
>>> datetime.strptime("R02.02.29", "R%y.%m.%d")              
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/_strptime.py", line 534, in _strptime
    julian = datetime_date(year, month, day).toordinal() - \
ValueError: day is out of range for month
>>> 

parser fails to load on Ubuntu 20.04 (Python3.8)

Parser failed to load on Ubuntu 20.04 (Python3.8).

  • System information
    • Ubuntu version : Ubuntu 20.04.6 LTS
    • Processor architecture: aarch64
    • CPU information : ARM 32-bit, 64-bit
    • Python3 version : Python 3.8.10
Python 3.8.10 (default, May 26 2023, 14:05:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import japanera
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/tnagano/.local/lib/python3.8/site-packages/japanera/__init__.py", line 17, in <module>
    from .japanera import (Era, EraDate, EraDateTime, ERA_DATA_COMMON, ERA_DATA_DAIKAKUJI, ERA_DATA_JIMYOUIN,
  File "/home/tnagano/.local/lib/python3.8/site-packages/japanera/japanera.py", line 10, in <module>
    from .parser import _strptime, find_era_and_date, _set_era_data
  File "/home/tnagano/.local/lib/python3.8/site-packages/japanera/parser.py", line 536, in <module>
    def find_eras_with_year(year: int) -> set["Era"]:
TypeError: 'type' object is not subscriptable

setlocale を Japanera 内で呼ばないようにしたいです

TODO: issue の内容について具体的に書く

課題

具体的には以下のようなエラーが発生した。

>           raise ParserError("Unknown string format: %s", timestr)
E           dateutil.parser._parser.ParserError: Unknown string format: 月, 30  1月 2023 07:39:41 GMT

エラーを見るに、意図しないロケール設定の影響を受けて、文字列形式が変わってしまったことが原因で、実際に影響をもたらしたのが Japanera の ini.py だったので、この副作用を取り除きたい。

対処方針

  • locale.setlocale(locale.LC_ALL, '') をライブラリ内で呼び出さない
  • locale.setlocale(locale.LC_ALL, '') を削除した上で現在実装されているテストが実行されるようにする(既存処理を壊さない

テスト方針

迷い中 🤔

Support for single digit year

すばらしいライブラリありがとうございます!重宝させていただいています。
一点、機能の要望ですが、和暦の 1 桁年にご対応いただけませんでしょうか。

現状、Japanera().strptime('令和2年9月1日', "%-E%-O年%m月%d日") とすると ValueError の例外が飛ぶかと思います。
おそらく strptime 本家の方で 'y': r"(?P<y>\d\d)" となっているためと考えられます。

format_regex = _TimeRE_cache.compile(format)
の辺りで

format_regex = _TimeRE_cache.compile(format)
format_regex = re.compile(format_regex.pattern.replace('(?P<y>\\d\\d)', '(?P<y>\\d|\\d\\d)'), re.IGNORECASE)

等とすれば 1 桁年にも対応できるかと思います。(が、力技できれいでない気がします。あとここで対応するのが適切か不明です。)
一度ご検討いただけると幸いです。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.