From 5ac1477388f1eaa3594645109adaf00e20c7c291 Mon Sep 17 00:00:00 2001 From: Majid Hajiloo Date: Fri, 28 Jun 2024 16:05:48 +0330 Subject: [PATCH] Fix strftime method to handle timezone correctly and add comprehensive tests --- persiantools/__init__.py | 2 +- persiantools/jdatetime.py | 2 +- tests/test_jalalidatetime.py | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/persiantools/__init__.py b/persiantools/__init__.py index 05f9a40..49049e7 100644 --- a/persiantools/__init__.py +++ b/persiantools/__init__.py @@ -7,7 +7,7 @@ __title__ = "persiantools" __url__ = "https://github.com/majiidd/persiantools" -__version__ = "4.1.0" +__version__ = "4.1.1" __build__ = __version__ __author__ = "Majid Hajiloo" __author_email__ = "majid.hajiloo@gmail.com" diff --git a/persiantools/jdatetime.py b/persiantools/jdatetime.py index b3c7640..8d7d36c 100644 --- a/persiantools/jdatetime.py +++ b/persiantools/jdatetime.py @@ -1428,7 +1428,7 @@ def strftime(self, fmt, locale=None): "%S": "%02d" % self._second, "%f": "%06d" % self._microsecond, "%z": datetime.strftime("%z"), - "%Z": ("" if not self._tzinfo else self._tzinfo.tzname(self)), + "%Z": ("" if not self._tzinfo else self._tzinfo.tzname(datetime)), "%X": "%02d:%02d:%02d" % (self._hour, self._minute, self._second), } diff --git a/tests/test_jalalidatetime.py b/tests/test_jalalidatetime.py index e8e61fe..803ded7 100644 --- a/tests/test_jalalidatetime.py +++ b/tests/test_jalalidatetime.py @@ -546,3 +546,49 @@ def test_subtract_different_dates(self): expected = timedelta(days=1) self.assertEqual(result, expected) + + def test_to_jalali_with_timezone(self): + dt = datetime.now(timezone.utc) + jdate = JalaliDateTime.to_jalali(dt) + self.assertEqual(jdate.tzinfo, timezone.utc) + + def test_strftime_basic(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45) + self.assertEqual(jdate.strftime("%Y-%m-%d %H:%M:%S"), "1400-01-01 15:30:45") + + def test_strftime_locale_fa(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45, locale="fa") + self.assertEqual(jdate.strftime("%Y-%m-%d %H:%M:%S", locale="fa"), "۱۴۰۰-۰۱-۰۱ ۱۵:۳۰:۴۵") + + def test_strftime_with_timezone_utc(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45, tzinfo=timezone.utc) + self.assertEqual(jdate.strftime("%Y-%m-%d %H:%M:%S %Z"), "1400-01-01 15:30:45 UTC") + + def test_strftime_with_timezone_offset(self): + tz = timezone(timedelta(hours=3, minutes=30)) + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45, tzinfo=tz) + self.assertEqual(jdate.strftime("%Y-%m-%d %H:%M:%S %z"), "1400-01-01 15:30:45 +0330") + + def test_strftime_with_abbreviated_month_day(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45) + self.assertEqual(jdate.strftime("%b %a"), "Far Yek") + + def test_strftime_with_full_month_day(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45) + self.assertEqual(jdate.strftime("%B %A"), "Farvardin Yekshanbeh") + + def test_strftime_with_custom_format(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45) + self.assertEqual(jdate.strftime("%d %B %Y - %H:%M"), "01 Farvardin 1400 - 15:30") + + def test_strftime_with_persian_locale(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45, locale="fa") + self.assertEqual(jdate.strftime("%A, %d %B %Y - %H:%M", locale="fa"), "یکشنبه, ۰۱ فروردین ۱۴۰۰ - ۱۵:۳۰") + + def test_strftime_with_periodic_time(self): + jdate = JalaliDateTime(1400, 1, 1, 15, 30, 45) + self.assertEqual(jdate.strftime("%I:%M %p"), "03:30 PM") + + def test_strftime_edge_case_midnight(self): + jdate = JalaliDateTime(1400, 1, 1, 0, 0, 0) + self.assertEqual(jdate.strftime("%Y-%m-%d %H:%M:%S"), "1400-01-01 00:00:00")