Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions jdatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ class date:
'بهمن',
'اسفند',
]
j_months_short_fa = [
'فرو',
'ارد',
'خرد',
'تیر',
'مرد',
'شهر',
'مهر',
'آبا',
'آذر',
'دی',
'بهم',
'اسف',
]
j_weekdays_fa = [
'شنبه',
'یک‌شنبه',
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})'

Expand Down Expand Up @@ -625,7 +643,7 @@ def aslocale(self, locale):
'%S': r'(?P<S>\d{1,2})',
'%f': r'(?P<f>\d{1,6})',
'%B': r'(?P<B>[a-zA-Z\u0600-\u06EF\uFB8A\u067E\u0686\u06AF]{2,12})',
'%b': r'(?P<b>[a-zA-Z]{3})',
'%b': r'(?P<b>[a-zA-Z\u0600-\u06EF\uFB8A\u067E\u0686\u06AF]{2,4})',
'%z': r'(?P<z>[+-]\d\d:?[0-5\u06F0-\u06F5]\d(:?[0-5\u06F0-\u06F5]\d(\.\d{1,6})?)?)',
}

Expand Down Expand Up @@ -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:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_jdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down
Loading