pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

MMM is not case-insensitive like Java

Open saorav21994 opened this issue 1 year ago • 3 comments

  • [Y] I have searched the issues of this repo and believe that this is not a duplicate.
  • [Y] I have searched the documentation and believe that my question is not covered.

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.

saorav21994 avatar Feb 25 '24 16:02 saorav21994

Still no response to this, more than one year later?

moltubakk avatar Jun 20 '25 13:06 moltubakk

Unfortunately, no response from authors.

For my case, I handled this in my application by making any month in pendulum acceptable format.

saorav21994 avatar Jun 20 '25 14:06 saorav21994

My main question is why would you expect Pendulum to work "like Java"?

Secrus avatar Jun 21 '25 10:06 Secrus