sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.12k stars 372 forks source link

MMM is not case-insensitive like Java #809

Open saorav21994 opened 4 months ago

saorav21994 commented 4 months ago

Feature Request

I came across a weird discrepancy in pendulum compared to other language supports. MMM does not consider case-insensitivity. Ex. - 25-Feb-2024 is correct recognizable, however, 25-feb-2024 is not. This is because months.abbreviated for MMM only corresponds to "Feb" for february. If we consider other language like Java, this is natively supported.

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;
class HelloWorld {
    public static void main(String[] args) {
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yyyy");
        String dateStr = "23-FeB-2024";
        Date date = null;

        try {
            date = formatter.parse(dateStr);
            System.out.println(date);
        } catch (Exception exception) {
            System.out.println("Could not parse ... " + exception);
        }
    }
}

Output: Fri Feb 23 00:00:00 GMT 2024

For pendulum --

import pendulum
file_date = pendulum.from_format("23-feb-2024", "DD-MMM-YYYY")
print(date_from_text)

Output:

Traceback (most recent call last):
  File "/~/pend.py", line 10, in <module>
    file_date = pendulum.from_format("23-feb-2024", "DD-MMM-YYYY")
  File "/~/.pyenv/versions/3.9.17/lib/python3.9/site-packages/pendulum/__init__.py", line 284, in from_format
    parts = _formatter.parse(string, fmt, now(tz=tz), locale=locale)
  File "/~/.pyenv/versions/3.9.17/lib/python3.9/site-packages/pendulum/formatting/formatter.py", line 409, in parse
    raise ValueError(f"String does not match format {fmt}")
ValueError: String does not match format DD-MMM-YYYY

I made some changes but was unable to push to remote repository due it lack of permissions.

Changes:

Inside _get_parsed_locale_value function in file formatter.py on line 630 make, value = value.lower()

In files - _src/pendulum/locales/<en, en_gb, en_us>/locale.py_ --> make , all MMM month to lower case for months.abbreviated.

I believe the above changes should tackle this issue.