Skip to content

Commit

Permalink
Fix strftime method to handle timezone correctly and add comprehensiv…
Browse files Browse the repository at this point in the history
…e tests
  • Loading branch information
majiidd committed Jun 28, 2024
1 parent 80e0e73 commit 5ac1477
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion persiantools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion persiantools/jdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

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

0 comments on commit 5ac1477

Please sign in to comment.