mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-26 04:39:26 +01:00
Use stdlib instead of pytz on Python >= 3.9
Python 3.9 introduced the zoneinfo module, which provides the only feature we used pytz for (getting a datetime.tzinfo object from an IANA timezone id); so let's use it instead of a third-party dependency.
This commit is contained in:
parent
a5cd930a4b
commit
8b26b675ba
@ -71,11 +71,6 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
tzlocal = None
|
tzlocal = None
|
||||||
|
|
||||||
try:
|
|
||||||
import pytz
|
|
||||||
except ImportError:
|
|
||||||
pytz = None
|
|
||||||
|
|
||||||
class Time(callbacks.Plugin):
|
class Time(callbacks.Plugin):
|
||||||
"""This plugin allows you to use different time-related functions."""
|
"""This plugin allows you to use different time-related functions."""
|
||||||
@internationalizeDocstring
|
@internationalizeDocstring
|
||||||
@ -200,19 +195,22 @@ class Time(callbacks.Plugin):
|
|||||||
|
|
||||||
@internationalizeDocstring
|
@internationalizeDocstring
|
||||||
def tztime(self, irc, msg, args, timezone):
|
def tztime(self, irc, msg, args, timezone):
|
||||||
"""<region>/<city>
|
"""<region>/<city> (or <region>/<state>/<city>)
|
||||||
|
|
||||||
Takes a city and its region, and returns its local time. This
|
Takes a city and its region, and returns its local time. This
|
||||||
command uses the IANA Time Zone Database."""
|
command uses the IANA Time Zone Database."""
|
||||||
if pytz is None:
|
|
||||||
irc.error(_('Python-tz is required by the command, but is not '
|
|
||||||
'installed on this computer.'), Raise=True)
|
|
||||||
try:
|
try:
|
||||||
timezone = pytz.timezone(timezone)
|
timezone = utils.time.iana_timezone(timezone)
|
||||||
except pytz.UnknownTimeZoneError:
|
except utils.time.UnknownTimeZone:
|
||||||
irc.error(_('Unknown timezone'), Raise=True)
|
irc.error(_('Unknown timezone'))
|
||||||
format = self.registryValue("format", msg.channel, irc.network)
|
except utils.time.MissingTimezoneLibrary:
|
||||||
irc.reply(datetime.now(timezone).strftime(format))
|
irc.error(_('Python-tz is required by the command, but is not '
|
||||||
|
'installed on this computer.'))
|
||||||
|
except utils.time.UnknownTimeZone as e:
|
||||||
|
irc.error(e.args[0])
|
||||||
|
else:
|
||||||
|
format = self.registryValue("format", msg.channel, irc.network)
|
||||||
|
irc.reply(datetime.now(timezone).strftime(format))
|
||||||
tztime = wrap(tztime, ['text'])
|
tztime = wrap(tztime, ['text'])
|
||||||
|
|
||||||
def ddate(self, irc, msg, args, year=None, month=None, day=None):
|
def ddate(self, irc, msg, args, year=None, month=None, day=None):
|
||||||
|
@ -28,14 +28,18 @@
|
|||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
###
|
###
|
||||||
|
|
||||||
|
import sys
|
||||||
from supybot.test import *
|
from supybot.test import *
|
||||||
|
|
||||||
try:
|
if sys.version_info >= (3, 9):
|
||||||
import pytz
|
has_tz_lib = True
|
||||||
except ImportError:
|
|
||||||
has_pytz = False
|
|
||||||
else:
|
else:
|
||||||
has_pytz = True
|
try:
|
||||||
|
import pytz
|
||||||
|
except ImportError:
|
||||||
|
has_tz_lib = False
|
||||||
|
else:
|
||||||
|
has_tz_lib = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import dateutil
|
import dateutil
|
||||||
@ -88,9 +92,10 @@ class TimeTestCase(PluginTestCase):
|
|||||||
self.assertNotError('ctime')
|
self.assertNotError('ctime')
|
||||||
self.assertNotError('time %Y')
|
self.assertNotError('time %Y')
|
||||||
|
|
||||||
@skipIf(not has_pytz, 'pytz is missing')
|
@skipIf(not has_tz_lib, 'python version is older than 3.9 and pytz is missing')
|
||||||
def testTztime(self):
|
def testTztime(self):
|
||||||
self.assertNotError('tztime Europe/Paris')
|
self.assertNotError('tztime Europe/Paris')
|
||||||
|
self.assertNotError('tztime America/Indiana/Knox')
|
||||||
self.assertError('tztime Europe/Gniarf')
|
self.assertError('tztime Europe/Gniarf')
|
||||||
|
|
||||||
@skipIf(not has_dateutil, 'python-dateutil is missing')
|
@skipIf(not has_dateutil, 'python-dateutil is missing')
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
setuptools
|
setuptools
|
||||||
chardet
|
chardet
|
||||||
pytz
|
pytz;python_version<'3.9'
|
||||||
python-dateutil
|
python-dateutil
|
||||||
python-gnupg
|
python-gnupg
|
||||||
feedparser
|
feedparser
|
||||||
|
@ -63,6 +63,6 @@ internationalization = builtins.get('supybotInternationalization', None)
|
|||||||
# These imports need to happen below the block above, so things get put into
|
# These imports need to happen below the block above, so things get put into
|
||||||
# __builtins__ appropriately.
|
# __builtins__ appropriately.
|
||||||
from .gen import *
|
from .gen import *
|
||||||
from . import crypt, error, file, iter, net, python, seq, str, transaction, web
|
from . import crypt, error, file, iter, net, python, seq, str, time, transaction, web
|
||||||
|
|
||||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
||||||
|
73
src/utils/time.py
Normal file
73
src/utils/time.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
###
|
||||||
|
# Copyright (c) 2021, Valentin Lorentz
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer.
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
# this list of conditions, and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
# * Neither the name of the author of this software nor the name of
|
||||||
|
# contributors to this software may be used to endorse or promote products
|
||||||
|
# derived from this software without specific prior written consent.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if sys.version_info >= (3, 9):
|
||||||
|
import zoneinfo
|
||||||
|
else:
|
||||||
|
zoneinfo = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pytz
|
||||||
|
except ImportError:
|
||||||
|
pytz = None
|
||||||
|
|
||||||
|
_IANA_TZ_RE = re.compile("([\w_-]+/)+[\w_-]+")
|
||||||
|
|
||||||
|
class TimezoneException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MissingTimezoneLibrary(TimezoneException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class UnknownTimeZone(TimezoneException):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def iana_timezone(name):
|
||||||
|
if not _IANA_TZ_RE.match(name):
|
||||||
|
raise UnknownTimeZone(name)
|
||||||
|
|
||||||
|
if zoneinfo:
|
||||||
|
try:
|
||||||
|
return zoneinfo.ZoneInfo(name)
|
||||||
|
except zoneinfo.ZoneInfoNotFoundError as e:
|
||||||
|
raise UnknownTimeZone(e.args[0]) from None
|
||||||
|
elif pytz:
|
||||||
|
try:
|
||||||
|
timezone = pytz.timezone(name)
|
||||||
|
except pytz.UnknownTimeZoneError as e:
|
||||||
|
raise UnknownTimeZone(e.args[0]) from None
|
||||||
|
else:
|
||||||
|
raise MissingTimezoneLibrary(
|
||||||
|
"Could not find a timezone library. "
|
||||||
|
"Update Python to version 3.9 or newer, or install pytz."
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user