diff --git a/jdatetime/__init__.py b/jdatetime/__init__.py index 015452a..a502e6c 100644 --- a/jdatetime/__init__.py +++ b/jdatetime/__init__.py @@ -186,6 +186,20 @@ class date: 'بهمن', 'اسفند', ] + j_months_short_fa = [ + 'فرو', + 'ارد', + 'خرد', + 'تیر', + 'مرد', + 'شهر', + 'مهر', + 'آبا', + 'آذر', + 'دی', + 'بهم', + 'اسف', + ] j_weekdays_fa = [ 'شنبه', 'یک‌شنبه', @@ -257,7 +271,7 @@ def __init__(self, year, month, day, **kwargs): if self._is_fa_locale(): self.j_months = self.j_months_fa - self.j_months_short = self.j_months_fa + self.j_months_short = self.j_months_short_fa self.j_weekdays = self.j_weekdays_fa self.j_weekdays_short = self.j_weekdays_fa self.j_ampm = self.j_ampm_fa @@ -377,6 +391,10 @@ def j_month_short_to_num(month_name): def j_month_fa_to_num(month_name): return date.j_months_fa.index(month_name) + 1 + @staticmethod + def j_month_short_fa_to_num(month_name): + return date.j_months_short_fa.index(month_name) + 1 + def __repr__(self): return f'jdatetime.date({self.year}, {self.month}, {self.day})' @@ -625,7 +643,7 @@ def aslocale(self, locale): '%S': r'(?P\d{1,2})', '%f': r'(?P\d{1,6})', '%B': r'(?P[a-zA-Z\u0600-\u06EF\uFB8A\u067E\u0686\u06AF]{2,12})', - '%b': r'(?P[a-zA-Z]{3})', + '%b': r'(?P[a-zA-Z\u0600-\u06EF\uFB8A\u067E\u0686\u06AF]{2,4})', '%z': r'(?P[+-]\d\d:?[0-5\u06F0-\u06F5]\d(:?[0-5\u06F0-\u06F5]\d(\.\d{1,6})?)?)', } @@ -891,7 +909,10 @@ def strptime(date_string, format): if isinstance(month, str): try: if get('b'): - month = date.j_month_short_to_num(month_name=month) + if month.isascii(): + month = date.j_month_short_to_num(month_name=month) + else: + month = date.j_month_short_fa_to_num(month_name=month) elif month.isascii(): month = date.j_month_to_num(month_name=month) else: diff --git a/tests/test_jdatetime.py b/tests/test_jdatetime.py index 128ddd2..a2050ea 100644 --- a/tests/test_jdatetime.py +++ b/tests/test_jdatetime.py @@ -211,6 +211,27 @@ def dst(self, dt): dt = jdatetime.datetime(1389, 2, 17, 19, 10, 2, tzinfo=teh) self.assertEqual(dt.strftime('%Z %z'), 'IRDT +0330') + def test_strftime_fa_locale_uses_short_month_names_for_b_directive(self): + tests = [ + (1, 'فرو', 'فروردین'), + (2, 'ارد', 'اردیبهشت'), + (3, 'خرد', 'خرداد'), + (4, 'تیر', 'تیر'), + (5, 'مرد', 'مرداد'), + (6, 'شهر', 'شهریور'), + (7, 'مهر', 'مهر'), + (8, 'آبا', 'آبان'), + (9, 'آذر', 'آذر'), + (10, 'دی', 'دی'), + (11, 'بهم', 'بهمن'), + (12, 'اسف', 'اسفند'), + ] + for month, expected_short_month, expected_full_month in tests: + with self.subTest(month=month): + dt = jdatetime.datetime(1400, month, 1, locale=jdatetime.FA_LOCALE) + self.assertEqual(dt.strftime('%b'), expected_short_month) + self.assertEqual(dt.strftime('%B'), expected_full_month) + def test_strftime_unicode(self): s = jdatetime.date(1390, 2, 23) self.assertEqual(s.strftime(b'%a %A'), 'Fri Friday') @@ -384,6 +405,7 @@ def test_strptime_handle_b_B_directive(self): ('۱4 ORD 14۰0', '%d %b %Y', (1400, 2, 14)), ('۱۴ دی ۱۴۰۰', '%d %B %Y', (1400, 10, 14)), ('۱۴ dey ۱۴۰۰', '%d %b %Y', (1400, 10, 14)), + ('۱۴ ارد ۱۴۰۰', '%d %b %Y', (1400, 2, 14)), ] for date_string, date_format, expected_date in tests: with self.subTest(date_string=date_string, date_format=date_format):