python / cpython

The Python programming language
https://www.python.org
Other
63.72k stars 30.53k forks source link

locale.nl_langinfo(locale.ERA) is broken #125411

Open serhiy-storchaka opened 1 month ago

serhiy-storchaka commented 1 month ago

Bug report

It is broken in the same way as locale.nl_langinfo(locale.ALT_DIGITS) was broken (see #124969), although not to the same degree. It returns the last (current) era.

import locale, subprocess
alllocales = subprocess.check_output(['locale', '-a']).decode().split()
for loc in alllocales:
    if '.' in loc or '@' in loc:
        continue
    try:
        _ = locale.setlocale(locale.LC_ALL, loc)
    except locale.Error:
        continue
    era = locale.nl_langinfo(locale.ERA)
    if era:
        print(loc, era)

Output:

cmn_TW +:2:1913/01/01:+*:民國:%EC%Ey年
hak_TW +:2:1913/01/01:+*:民國:%EC%Ey年
ja_JP +:2:2020/01/01:+*:令和:%EC%Ey年
japanese +:2:2020/01/01:+*:令和:%EC%Ey年
lo_LA +:1:-543/01/01:+*:ພ.ສ.:%EC %Ey
lzh_TW +:2:1913/01/01:+*:民國:%EC%Ey年
nan_TW +:2:1913/01/01:+*:民國:%EC%Ey年
thai +:1:-543/01/01:+*:พ.ศ.:%EC %Ey
th_TH +:1:-543/01/01:+*:พ.ศ.:%EC %Ey
zh_TW +:2:1913/01/01:+*:民國:%EC%Ey年

The result is not completely useless, as it can be used to format and parse the current dates. But dates before the start of the current era (it is 2020 in Japan) cannot be proceeded.

The result should be a tuple, containing information for all eras, but this is a breaking change. So, perhaps such change cannot be backported.

cc @methane

serhiy-storchaka commented 1 month ago

@kulikjak, could you also show the output of the above code on Solaris?

kulikjak commented 1 month ago

Sure, here's the output:

ja_JP.PCK +:2:2020/01/01:+*:令和:%EC%Ey年;+:1:2019/05/01:2019/12/31:令和:%EC元年;+:2:1990/01/01:2019/04/30:平成:%EC%Ey年;+:1:1989/01/08:1989/12/31:平成:%EC元年;+:2:1927/01/01:1989/01/07:昭和:%EC%Ey年
ja_JP.UTF-8 +:2:2020/01/01:+*:令和:%EC%Ey年;+:1:2019/05/01:2019/12/31:令和:%EC元年;+:2:1990/01/01:2019/04/30:平成:%EC%Ey年;+:1:1989/01/08:1989/12/31:平成:%EC元年;+:2:1927/01/01:1989/01/07:昭和:%EC%Ey年
ja_JP.eucJP +:2:2020/01/01:+*:令和:%EC%Ey年;+:1:2019/05/01:2019/12/31:令和:%EC元年;+:2:1990/01/01:2019/04/30:平成:%EC%Ey年;+:1:1989/01/08:1989/12/31:平成:%EC元年;+:2:1927/01/01:1989/01/07:昭和:%EC%Ey年
th_TH.TIS620 +:0:-543/01/01:+*::พ.ศ. %Ey
zh_CN.GB18030 +:0:0000/01/01:+*:公元:%EC%Ey年;+:1:-0001/12/31:-*:公元前:%EC%Ey年

(sorry I didn't notice this yesterday)