Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ZoneInfo support. #93

Merged
merged 1 commit into from
Dec 20, 2021
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Nice idea to keep track of changes 👏

### Add
* Add ZoneInfo support

## [3.6.4] - 2021-09-15
### Add
* Add date.min based on cpython implementation
Expand Down
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ environment:
init:
- "ECHO %PYTHON% %PYTHON_VERSION% %PYTHON_ARCH%"

install:
- py -m pip install tzdata

test_script:
- "%PYTHON%\\python.exe t/test.py"
- py t/test.py
22 changes: 8 additions & 14 deletions jdatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,7 @@ def strftime(self, format):

try:
sign = "+"
try:
diff = self.tzinfo.utcoffset(self)
except TypeError:
diff = self.tzinfo.utcoffset(None)
diff = self.utcoffset()
diff_sec = diff.seconds
if diff.days > 0 or diff.days < -1:
raise ValueError(
Expand All @@ -654,11 +651,9 @@ def strftime(self, format):
except AttributeError:
format = format.replace("%z", '')

try:
format = format.replace("%Z", self.tzinfo.tzname(self))
except TypeError:
format = format.replace("%Z", self.tzinfo.tzname(None))
except AttributeError:
if hasattr(self, 'tzname') and self.tzname() is not None:
format = format.replace("%Z", self.tzname())
else:
format = format.replace("%Z", '')

return format
Expand Down Expand Up @@ -1294,13 +1289,13 @@ def timetz(self):
def tzname(self):
"""Return self.tzinfo.tzname(self)"""
if self.tzinfo:
return self.tzinfo.tzname(self)
return self.tzinfo.tzname(self.togregorian())
return None

def utcoffset(self):
"""Return self.tzinfo.utcoffset(self)."""
if self.tzinfo:
return self.tzinfo.utcoffset(self)
return self.tzinfo.utcoffset(self.togregorian())

def utctimetuple(self):
"""Return UTC time tuple, compatible with time.localtime().
Expand All @@ -1310,11 +1305,10 @@ def utctimetuple(self):
return dt.utctimetuple()

def __str__(self):
mil = self.strftime("%f")
if int(mil) == 0:
if self.microsecond == 0:
mil = ""
else:
mil = "." + mil
mil = "." + str(self.microsecond)
tz = self.strftime("%z")
return self.strftime("%Y-%m-%d %H:%M:%S") + "%s%s" % (mil, tz)

Expand Down
12 changes: 12 additions & 0 deletions t/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
except ImportError:
greenlet_installed = False

try:
import zoneinfo
except ImportError:
zoneinfo = None


BASEDIR = os.path.abspath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
)
Expand Down Expand Up @@ -698,6 +704,12 @@ def test_isoformat_custom_timespec(self):
self.assertEqual(milliseconds, '1398-04-11T11:06:05.123')
self.assertEqual(microseconds, '1398-04-11T11:06:05.123456')

@unittest.skipIf(zoneinfo is None, "ZoneInfo not supported!")
def test_zoneinfo_as_timezone(self):
tzinfo = zoneinfo.ZoneInfo('Asia/Tehran')
jdt = jdatetime.datetime(1398, 4, 11, 11, 6, 5, 123456, tzinfo=tzinfo)
self.assertEqual(str(jdt), '1398-04-11 11:06:05.123456+0430')


class TestJdatetimeGetSetLocale(unittest.TestCase):
@staticmethod
Expand Down